37
37
import java .io .File ;
38
38
import java .util .Comparator ;
39
39
import java .util .HashSet ;
40
- import java .util .List ;
41
40
import java .util .Set ;
42
- import java .util .stream .Collectors ;
43
41
import java .util .stream .Stream ;
44
42
45
43
import static org .gradle .api .attributes .Bundling .BUNDLING_ATTRIBUTE ;
@@ -72,43 +70,8 @@ public void apply(Project project) {
72
70
73
71
// Rewrite module dependencies put here will be available to all rewrite tasks
74
72
Configuration rewriteConf = project .getConfigurations ().maybeCreate ("rewrite" );
75
- // Defer actually evaluating the dependencies until resolution is triggered. This should allow
76
- // the user to set the rewrite version in the extension. `addLater` doesn't work here because
77
- // it operates on a single dependency at a time.
78
- rewriteConf .getIncoming ().beforeResolve (conf -> {
79
- rewriteConf .getDependencies ().addAll (
80
- knownRewriteDependencies (extension , project .getDependencies ())
81
- );
82
- });
83
-
84
- // Because of how this Gradle has no criteria with which to select between variants of
85
- // dependencies which expose differing capabilities. So those must be manually configured
86
- try {
87
- final ObjectFactory objectFactory = project .getObjects ();
88
- rewriteConf .attributes (attributes -> {
89
- // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails
90
- attributes .attribute (
91
- Category .CATEGORY_ATTRIBUTE , objectFactory .named (Category .class , Category .LIBRARY ));
92
- attributes .attribute (Usage .USAGE_ATTRIBUTE , objectFactory .named (Usage .class , Usage .JAVA_RUNTIME ));
93
- attributes .attribute (
94
- LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE ,
95
- objectFactory .named (LibraryElements .class , LibraryElements .JAR ));
96
- attributes .attribute (BUNDLING_ATTRIBUTE , objectFactory .named (Bundling .class , Bundling .EXTERNAL ));
97
- try {
98
- attributes .attribute (
99
- TARGET_JVM_ENVIRONMENT_ATTRIBUTE ,
100
- objectFactory .named (TargetJvmEnvironment .class , TargetJvmEnvironment .STANDARD_JVM ));
101
- } catch (final NoClassDefFoundError ex ) {
102
- // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always
103
- // try this attribute instead
104
- attributes .attribute (Attribute .of ("org.gradle.jvm.environment" , String .class ), "standard-jvm" );
105
- }
106
- });
107
- } catch (final NoClassDefFoundError ex ) {
108
- // Old versions of Gradle don't have all of these attributes and that's OK
109
- }
110
73
111
- Provider <Set <File >> resolvedDependenciesProvider = project .provider (() -> getResolvedDependencies (rewriteConf ));
74
+ Provider <Set <File >> resolvedDependenciesProvider = project .provider (() -> getResolvedDependencies (project , extension , rewriteConf ));
112
75
113
76
TaskProvider <RewriteRunTask > rewriteRun = project .getTasks ().register ("rewriteRun" , RewriteRunTask .class , task -> {
114
77
task .setExtension (extension );
@@ -190,16 +153,43 @@ private static void configureProject(Project project, RewriteExtension extension
190
153
});
191
154
}
192
155
193
- private Set <File > getResolvedDependencies (Configuration rewriteConf ) {
194
- if (resolvedDependencies != null ) {
195
- return resolvedDependencies ;
196
- }
156
+ private Set <File > getResolvedDependencies (Project project , RewriteExtension extension , Configuration rewriteConf ) {
157
+ if (resolvedDependencies == null ) {
158
+ Dependency [] dependencies = Stream .concat (
159
+ knownRewriteDependencies (extension , project .getDependencies ()),
160
+ rewriteConf .getDependencies ().stream ()
161
+ ).toArray (Dependency []::new );
162
+ // By using a detached configuration, we separate this dependency resolution from the rest of the project's
163
+ // configuration. This also means that Gradle has no criteria with which to select between variants of
164
+ // dependencies which expose differing capabilities. So those must be manually configured
165
+ Configuration detachedConf = project .getConfigurations ().detachedConfiguration (dependencies );
166
+
167
+ try {
168
+ ObjectFactory objectFactory = project .getObjects ();
169
+ detachedConf .attributes (attributes -> {
170
+ // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails
171
+ attributes .attribute (Category .CATEGORY_ATTRIBUTE , objectFactory .named (Category .class , Category .LIBRARY ));
172
+ attributes .attribute (Usage .USAGE_ATTRIBUTE , objectFactory .named (Usage .class , Usage .JAVA_RUNTIME ));
173
+ attributes .attribute (LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE , objectFactory .named (LibraryElements .class , LibraryElements .JAR ));
174
+ attributes .attribute (BUNDLING_ATTRIBUTE , objectFactory .named (Bundling .class , Bundling .EXTERNAL ));
175
+ try {
176
+ attributes .attribute (TARGET_JVM_ENVIRONMENT_ATTRIBUTE , objectFactory .named (TargetJvmEnvironment .class , TargetJvmEnvironment .STANDARD_JVM ));
177
+ } catch (NoClassDefFoundError e ) {
178
+ // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always
179
+ // try this attribute instead
180
+ attributes .attribute (Attribute .of ("org.gradle.jvm.environment" , String .class ), "standard-jvm" );
181
+ }
182
+ });
183
+ } catch (NoClassDefFoundError e ) {
184
+ // Old versions of Gradle don't have all of these attributes and that's OK
185
+ }
197
186
198
- resolvedDependencies = rewriteConf .resolve ();
187
+ resolvedDependencies = detachedConf .resolve ();
188
+ }
199
189
return resolvedDependencies ;
200
190
}
201
191
202
- private static List <Dependency > knownRewriteDependencies (RewriteExtension extension , DependencyHandler deps ) {
192
+ private static Stream <Dependency > knownRewriteDependencies (RewriteExtension extension , DependencyHandler deps ) {
203
193
String rewriteVersion = extension .getRewriteVersion ();
204
194
return Stream .of (
205
195
deps .create ("org.openrewrite:rewrite-core:" + rewriteVersion ),
@@ -222,6 +212,6 @@ private static List<Dependency> knownRewriteDependencies(RewriteExtension extens
222
212
deps .create ("org.openrewrite.gradle.tooling:model:" + extension .getRewriteGradleModelVersion ()),
223
213
deps .create ("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension .getJacksonModuleKotlinVersion ()),
224
214
deps .create ("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension .getJacksonModuleKotlinVersion ())
225
- ). collect ( Collectors . toList ()) ;
215
+ );
226
216
}
227
217
}
0 commit comments