Skip to content

Commit 13bbc11

Browse files
authored
Revert to hidden detached configuration (#353)
* Format RewritePluginTest.kt * Revert "Simplify Plugins approach to adding dependencies so that it no longer uses a hidden detached configuration. (#345)" This reverts commit 147c7ef.
1 parent e675089 commit 13bbc11

File tree

3 files changed

+41
-49
lines changed

3 files changed

+41
-49
lines changed

plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java

+35-45
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
import java.io.File;
3838
import java.util.Comparator;
3939
import java.util.HashSet;
40-
import java.util.List;
4140
import java.util.Set;
42-
import java.util.stream.Collectors;
4341
import java.util.stream.Stream;
4442

4543
import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE;
@@ -72,43 +70,8 @@ public void apply(Project project) {
7270

7371
// Rewrite module dependencies put here will be available to all rewrite tasks
7472
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-
}
11073

111-
Provider<Set<File>> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(rewriteConf));
74+
Provider<Set<File>> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(project, extension, rewriteConf));
11275

11376
TaskProvider<RewriteRunTask> rewriteRun = project.getTasks().register("rewriteRun", RewriteRunTask.class, task -> {
11477
task.setExtension(extension);
@@ -190,16 +153,43 @@ private static void configureProject(Project project, RewriteExtension extension
190153
});
191154
}
192155

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+
}
197186

198-
resolvedDependencies = rewriteConf.resolve();
187+
resolvedDependencies = detachedConf.resolve();
188+
}
199189
return resolvedDependencies;
200190
}
201191

202-
private static List<Dependency> knownRewriteDependencies(RewriteExtension extension, DependencyHandler deps) {
192+
private static Stream<Dependency> knownRewriteDependencies(RewriteExtension extension, DependencyHandler deps) {
203193
String rewriteVersion = extension.getRewriteVersion();
204194
return Stream.of(
205195
deps.create("org.openrewrite:rewrite-core:" + rewriteVersion),
@@ -222,6 +212,6 @@ private static List<Dependency> knownRewriteDependencies(RewriteExtension extens
222212
deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()),
223213
deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()),
224214
deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion())
225-
).collect(Collectors.toList());
215+
);
226216
}
227217
}

plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ interface RewritePluginTest {
7272
@TempDir projectDir: File
7373
) {
7474
gradleProject(projectDir) {
75-
buildGradle("""
75+
buildGradle(
76+
"""
7677
plugins {
7778
id("org.openrewrite.rewrite")
7879
}
@@ -84,7 +85,8 @@ interface RewritePluginTest {
8485
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
8586
}
8687
}
87-
""")
88+
"""
89+
)
8890
}
8991
val result = runGradle(projectDir, taskName(), "--configuration-cache")
9092
val taskResult = result.task(":${taskName()}")!!

plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ class RewriteRunTest : RewritePluginTest {
15951595
}
15961596
15971597
apply from: 'dependencies.gradle'
1598-
"""
1598+
"""
15991599
)
16001600
otherGradleScript(
16011601
"dependencies.gradle", """
@@ -1609,7 +1609,7 @@ class RewriteRunTest : RewritePluginTest {
16091609
dependencies {
16101610
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
16111611
}
1612-
"""
1612+
"""
16131613
)
16141614
}
16151615
projectDir.resolve("build").mkdirs()

0 commit comments

Comments
 (0)