19
19
20
20
package net .flintmc .gradle .maven ;
21
21
22
- import com .google .common .base .Objects ;
23
- import java .util .ArrayList ;
24
- import java .util .List ;
22
+ import net .flintmc .gradle .extension .FlintGradleExtension ;
25
23
import org .gradle .api .Project ;
26
24
import org .gradle .api .artifacts .ModuleVersionSelector ;
27
25
26
+ import java .util .ArrayList ;
27
+ import java .util .List ;
28
+ import java .util .Objects ;
29
+
28
30
public class FlintResolutionStrategy {
29
31
30
32
private static final FlintResolutionStrategy instance = new FlintResolutionStrategy ();
@@ -54,8 +56,8 @@ public static FlintResolutionStrategy getInstance() {
54
56
}
55
57
56
58
/**
57
- * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new
58
- * forced modules to be considered when resolving dependencies. It accepts following notations:
59
+ * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to
60
+ * be considered when resolving dependencies. It accepts following notations:
59
61
*
60
62
* <ul>
61
63
* <li>String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
@@ -67,22 +69,22 @@ public static FlintResolutionStrategy getInstance() {
67
69
public void forceDependency (Object moduleVersionSelectorNotation ) {
68
70
ForcedDependency forcedDependency = this .createForcedDependency (moduleVersionSelectorNotation );
69
71
70
- if (forcedDependency == null ) {
72
+ if (forcedDependency == null ) {
71
73
return ;
72
74
}
73
75
74
- for (int index = 0 ; index < this .forcedDependencies .size (); index ++) {
76
+ for (int index = 0 ; index < this .forcedDependencies .size (); index ++) {
75
77
ForcedDependency dependency = this .createForcedDependency (this .forcedDependencies .get (index ));
76
78
77
- if (dependency == null ) {
79
+ if (dependency == null ) {
78
80
return ;
79
81
}
80
82
81
- if (dependency .equals (forcedDependency )) {
83
+ if (dependency .equals (forcedDependency )) {
82
84
return ;
83
85
}
84
86
85
- if (dependency .nonVersionEquals (forcedDependency )) {
87
+ if (dependency .nonVersionEquals (forcedDependency )) {
86
88
this .forcedDependencies .set (index , moduleVersionSelectorNotation );
87
89
return ;
88
90
}
@@ -92,8 +94,8 @@ public void forceDependency(Object moduleVersionSelectorNotation) {
92
94
}
93
95
94
96
/**
95
- * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new
96
- * forced modules to be considered when resolving dependencies. It accepts following notations:
97
+ * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to
98
+ * be considered when resolving dependencies. It accepts following notations:
97
99
*
98
100
* <ul>
99
101
* <li>String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
@@ -104,47 +106,63 @@ public void forceDependency(Object moduleVersionSelectorNotation) {
104
106
* @param moduleVersionSelectorNotations Typically group:name:version notations to append.
105
107
*/
106
108
public void forceDependencies (Object ... moduleVersionSelectorNotations ) {
107
- for (Object moduleVersionSelectorNotation : moduleVersionSelectorNotations ) {
109
+ for (Object moduleVersionSelectorNotation : moduleVersionSelectorNotations ) {
108
110
this .forceDependency (moduleVersionSelectorNotation );
109
111
}
110
112
}
111
113
112
114
/**
113
115
* Forces a collection of dependencies on all configurations of the given {@code project}.
114
116
*
115
- * @param project The project for which the dependencies are to be enforced.
117
+ * @param project The project for which the dependencies are to be enforced.
118
+ * @param extension The flint extension for the project
116
119
*/
117
- public void forceResolutionStrategy (Project project ) {
120
+ public void forceResolutionStrategy (Project project , FlintGradleExtension extension ) {
121
+ String flintVersion = extension .getFlintVersion ();
122
+
118
123
project
119
124
.getConfigurations ()
120
125
.forEach (
121
126
configuration ->
122
127
configuration .resolutionStrategy (
123
128
resolutionStrategy -> {
124
129
forcedDependencies .forEach (resolutionStrategy ::force );
130
+ resolutionStrategy .eachDependency ((details ) -> {
131
+ ModuleVersionSelector selector = details .getRequested ();
132
+
133
+ if (selector .getGroup ().equals ("net.flintmc" ) && !Objects .equals (selector .getVersion (), flintVersion )) {
134
+
135
+ // Force the flint version to what is specified in the project
136
+ details .useVersion (flintVersion );
137
+ details .because (
138
+ "Forcing net.flintmc dependency from " + selector .getVersion () + " to " +
139
+ flintVersion + " to ensure a consistent framework version " +
140
+ "[Automatically done by flint-gradle]" );
141
+ }
142
+ });
125
143
}));
126
144
}
127
145
128
146
private ForcedDependency createForcedDependency (Object moduleVersionSelectorNotation ) {
129
147
130
- if (moduleVersionSelectorNotation instanceof String ) {
148
+ if (moduleVersionSelectorNotation instanceof String ) {
131
149
String notation = (String ) moduleVersionSelectorNotation ;
132
150
133
- if (!notation .contains (":" )) {
151
+ if (!notation .contains (":" )) {
134
152
return null ;
135
153
}
136
154
137
155
String [] split = notation .split (":" );
138
156
139
- if (split .length == 3 ) {
157
+ if (split .length == 3 ) {
140
158
141
159
String group = split [0 ];
142
160
String name = split [1 ];
143
161
String version = split [2 ];
144
162
145
163
return new ForcedDependency (group , name , version );
146
164
}
147
- } else if (moduleVersionSelectorNotation instanceof ModuleVersionSelector ) {
165
+ } else if (moduleVersionSelectorNotation instanceof ModuleVersionSelector ) {
148
166
ModuleVersionSelector moduleVersionSelector =
149
167
(ModuleVersionSelector ) moduleVersionSelectorNotation ;
150
168
return new ForcedDependency (
@@ -180,25 +198,33 @@ public String getVersion() {
180
198
}
181
199
182
200
public boolean nonVersionEquals (Object o ) {
183
- if (this == o ) return true ;
184
- if (o == null || getClass () != o .getClass ()) return false ;
201
+ if (this == o ) {
202
+ return true ;
203
+ }
204
+ if (o == null || getClass () != o .getClass ()) {
205
+ return false ;
206
+ }
185
207
ForcedDependency that = (ForcedDependency ) o ;
186
- return Objects .equal (group , that .group ) && Objects .equal (name , that .name );
208
+ return Objects .equals (group , that .group ) && Objects .equals (name , that .name );
187
209
}
188
210
189
211
@ Override
190
212
public boolean equals (Object o ) {
191
- if (this == o ) return true ;
192
- if (o == null || getClass () != o .getClass ()) return false ;
213
+ if (this == o ) {
214
+ return true ;
215
+ }
216
+ if (o == null || getClass () != o .getClass ()) {
217
+ return false ;
218
+ }
193
219
ForcedDependency that = (ForcedDependency ) o ;
194
- return Objects .equal (group , that .group )
195
- && Objects .equal (name , that .name )
196
- && Objects .equal (version , that .version );
220
+ return Objects .equals (group , that .group )
221
+ && Objects .equals (name , that .name )
222
+ && Objects .equals (version , that .version );
197
223
}
198
224
199
225
@ Override
200
226
public int hashCode () {
201
- return Objects .hashCode (group , name , version );
227
+ return Objects .hash (group , name , version );
202
228
}
203
229
}
204
230
}
0 commit comments