diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index 470345dda..fa20b3d00 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -37,9 +37,7 @@ import java.io.File; import java.util.Comparator; import java.util.HashSet; -import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import java.util.stream.Stream; import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE; @@ -72,43 +70,8 @@ public void apply(Project project) { // Rewrite module dependencies put here will be available to all rewrite tasks Configuration rewriteConf = project.getConfigurations().maybeCreate("rewrite"); - // Defer actually evaluating the dependencies until resolution is triggered. This should allow - // the user to set the rewrite version in the extension. `addLater` doesn't work here because - // it operates on a single dependency at a time. - rewriteConf.getIncoming().beforeResolve(conf -> { - rewriteConf.getDependencies().addAll( - knownRewriteDependencies(extension, project.getDependencies()) - ); - }); - - // Because of how this Gradle has no criteria with which to select between variants of - // dependencies which expose differing capabilities. So those must be manually configured - try { - final ObjectFactory objectFactory = project.getObjects(); - rewriteConf.attributes(attributes -> { - // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails - attributes.attribute( - Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category.class, Category.LIBRARY)); - attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME)); - attributes.attribute( - LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, - objectFactory.named(LibraryElements.class, LibraryElements.JAR)); - attributes.attribute(BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL)); - try { - attributes.attribute( - TARGET_JVM_ENVIRONMENT_ATTRIBUTE, - objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); - } catch (final NoClassDefFoundError ex) { - // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always - // try this attribute instead - attributes.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm"); - } - }); - } catch (final NoClassDefFoundError ex) { - // Old versions of Gradle don't have all of these attributes and that's OK - } - Provider> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(rewriteConf)); + Provider> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(project, extension, rewriteConf)); TaskProvider rewriteRun = project.getTasks().register("rewriteRun", RewriteRunTask.class, task -> { task.setExtension(extension); @@ -190,16 +153,43 @@ private static void configureProject(Project project, RewriteExtension extension }); } - private Set getResolvedDependencies(Configuration rewriteConf) { - if (resolvedDependencies != null) { - return resolvedDependencies; - } + private Set getResolvedDependencies(Project project, RewriteExtension extension, Configuration rewriteConf) { + if (resolvedDependencies == null) { + Dependency[] dependencies = Stream.concat( + knownRewriteDependencies(extension, project.getDependencies()), + rewriteConf.getDependencies().stream() + ).toArray(Dependency[]::new); + // By using a detached configuration, we separate this dependency resolution from the rest of the project's + // configuration. This also means that Gradle has no criteria with which to select between variants of + // dependencies which expose differing capabilities. So those must be manually configured + Configuration detachedConf = project.getConfigurations().detachedConfiguration(dependencies); + + try { + ObjectFactory objectFactory = project.getObjects(); + detachedConf.attributes(attributes -> { + // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails + attributes.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category.class, Category.LIBRARY)); + attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME)); + attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objectFactory.named(LibraryElements.class, LibraryElements.JAR)); + attributes.attribute(BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL)); + try { + attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); + } catch (NoClassDefFoundError e) { + // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always + // try this attribute instead + attributes.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm"); + } + }); + } catch (NoClassDefFoundError e) { + // Old versions of Gradle don't have all of these attributes and that's OK + } - resolvedDependencies = rewriteConf.resolve(); + resolvedDependencies = detachedConf.resolve(); + } return resolvedDependencies; } - private static List knownRewriteDependencies(RewriteExtension extension, DependencyHandler deps) { + private static Stream knownRewriteDependencies(RewriteExtension extension, DependencyHandler deps) { String rewriteVersion = extension.getRewriteVersion(); return Stream.of( deps.create("org.openrewrite:rewrite-core:" + rewriteVersion), @@ -222,6 +212,6 @@ private static List knownRewriteDependencies(RewriteExtension extens deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()), deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()), deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion()) - ).collect(Collectors.toList()); + ); } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt index d0d21a1e8..335175a87 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt @@ -72,7 +72,8 @@ interface RewritePluginTest { @TempDir projectDir: File ) { gradleProject(projectDir) { - buildGradle(""" + buildGradle( + """ plugins { id("org.openrewrite.rewrite") } @@ -84,7 +85,8 @@ interface RewritePluginTest { url = uri("https://oss.sonatype.org/content/repositories/snapshots") } } - """) + """ + ) } val result = runGradle(projectDir, taskName(), "--configuration-cache") val taskResult = result.task(":${taskName()}")!! diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index 94fc3f2e4..24d257650 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -1595,7 +1595,7 @@ class RewriteRunTest : RewritePluginTest { } apply from: 'dependencies.gradle' - """ + """ ) otherGradleScript( "dependencies.gradle", """ @@ -1609,7 +1609,7 @@ class RewriteRunTest : RewritePluginTest { dependencies { implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2") } - """ + """ ) } projectDir.resolve("build").mkdirs()