diff --git a/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/IntegrationTestPlugin.groovy b/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/IntegrationTestPlugin.groovy index 630fa2a0..702ee519 100644 --- a/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/IntegrationTestPlugin.groovy +++ b/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/IntegrationTestPlugin.groovy @@ -18,7 +18,6 @@ class IntegrationTestPlugin extends BasePlugin { protected void applyPlugins(Project project) { super.applyPlugins(project) project.apply plugin: 'groovy' // this is needed for spock - project.apply plugin: JavaToolchainIntegrationTestPlugin } @Override @@ -51,6 +50,13 @@ class IntegrationTestPlugin extends BasePlugin { project.extensions.add(JavaVersion, 'javaVersion', JavaToolchainIntegrationTestPlugin.getToolchainJavaVersion(project)) + /** + * The caller project integration test would react on -PtoolchainJavaVersion=17 parameter and define appropriate toolchain DSL + **/ + project.ext.defineAsJavaToolchainAwareIntegrationTest = { + JavaToolchainIntegrationTestPlugin.applyPluginConditionally(project) + } + project.ext.defineIntegrationTest = { def integrationTestTask_ = project.tasks.findByName('integrationTest') @@ -66,7 +72,10 @@ class IntegrationTestPlugin extends BasePlugin { testClassesDirs = project.sourceSets.integrationTest.output.classesDirs classpath = project.sourceSets.integrationTest.runtimeClasspath - JavaToolchainIntegrationTestPlugin.forceTaskToUseGradleJvm(it) + JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(it) + JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin -> + plugin.forceTaskToUseGradleJvm(it) + } } integrationTestTask_ @@ -81,6 +90,11 @@ class IntegrationTestPlugin extends BasePlugin { integrationTestAllContainersTask = project.task('integrationTestAllContainers') + JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(integrationTestAllContainersTask) + JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin -> + plugin.forceTaskToUseGradleJvm(integrationTestAllContainersTask) + } + if (!integrationTestContainers) integrationTestContainers = ServletContainerConfig.getConfigNames().collect() // returns immutable and we want to filter later @@ -116,7 +130,10 @@ class IntegrationTestPlugin extends BasePlugin { testClassesDirs = project.sourceSets.integrationTest.output.classesDirs classpath = project.sourceSets.integrationTest.runtimeClasspath - JavaToolchainIntegrationTestPlugin.forceTaskToUseGradleJvm(thisTask) + JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(thisTask) + JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin -> + plugin.forceTaskToUseGradleJvm(thisTask) + } } integrationTestAllContainersTask.dependsOn project.tasks['integrationTest_' + container] @@ -124,10 +141,13 @@ class IntegrationTestPlugin extends BasePlugin { project.task('beforeIntegrationTest_' + container, type: AppBeforeIntegrationTestTask) { servletContainer = container integrationTestTask 'integrationTest_' + container + + JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(it) } project.task('afterIntegrationTest_' + container, type: AppAfterIntegrationTestTask) { integrationTestTask 'integrationTest_' + container + JavaToolchainIntegrationTestPlugin.skipIrrelevantTasks(it) } } @@ -194,8 +214,10 @@ class IntegrationTestPlugin extends BasePlugin { srcDir 'src/integrationTest/resources' } runtimeClasspath += project.rootProject.files('config/gebConfig') - - JavaToolchainIntegrationTestPlugin.forceSourceSetToUseGradleJvm(project, it) + + JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin -> + plugin.forceSourceSetToUseGradleJvm(project, it) + } } } } diff --git a/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/JavaToolchainIntegrationTestPlugin.groovy b/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/JavaToolchainIntegrationTestPlugin.groovy index d69fe34d..9551bfde 100644 --- a/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/JavaToolchainIntegrationTestPlugin.groovy +++ b/integrationTests/buildSrc/gretty-integrationTest/src/main/groovy/org/akhikhl/gretty/internal/integrationTests/JavaToolchainIntegrationTestPlugin.groovy @@ -1,6 +1,8 @@ package org.akhikhl.gretty.internal.integrationTests +import org.gradle.api.Action import org.gradle.api.JavaVersion +import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.tasks.SourceSet @@ -11,14 +13,31 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion import org.slf4j.Logger import org.slf4j.LoggerFactory -class JavaToolchainIntegrationTestPlugin extends BasePlugin { +import java.util.function.Consumer + +class JavaToolchainIntegrationTestPlugin implements Plugin { + public static final String PLUGIN_ID = "org.gretty.internal.integrationTests.JavaToolchainIntegrationTestPlugin" private static final Logger log = LoggerFactory.getLogger(IntegrationTestPlugin) - protected void configureExtensions(Project project) { + static void applyPluginConditionally(Project project) { if (project.findProperty('toolchainJavaVersion')) { - defineToolchainDSL(project, Integer.parseInt("${project.toolchainJavaVersion}")) + project.apply plugin: PLUGIN_ID } } + + static void whenApplied(Project project, Consumer config) { + project.plugins.withId(PLUGIN_ID, new Action() { + @Override + void execute(Plugin plugin) { + config.accept((JavaToolchainIntegrationTestPlugin) plugin) + } + }) + } + + @Override + void apply(Project project) { + defineToolchainDSL(project, Integer.parseInt("${project.toolchainJavaVersion}")) + } private void defineToolchainDSL(Project project, int javaVersion) { project.java { @@ -28,27 +47,23 @@ class JavaToolchainIntegrationTestPlugin extends BasePlugin { } } - public static void forceSourceSetToUseGradleJvm(Project project, SourceSet sourceSet) { - if (isPluginApplied(project)) { - project.tasks.named(sourceSet.getCompileTaskName('java')).configure({ forceTaskToUseGradleJvm(it) }) - project.tasks.named(sourceSet.getCompileTaskName('groovy')).configure({ forceTaskToUseGradleJvm(it) }) - } + public void forceSourceSetToUseGradleJvm(Project project, SourceSet sourceSet) { + project.tasks.named(sourceSet.getCompileTaskName('java')).configure({ forceTaskToUseGradleJvm(it) }) + project.tasks.named(sourceSet.getCompileTaskName('groovy')).configure({ forceTaskToUseGradleJvm(it) }) } - public static void forceTaskToUseGradleJvm(Task task) { + public void forceTaskToUseGradleJvm(Task task) { task.project.with { proj -> - if (isPluginApplied(proj)) { - if (task instanceof JavaCompile) { - task.javaCompiler.value(proj.javaToolchains.compilerFor(gradleJvmSpec)) - } + if (task instanceof JavaCompile) { + task.javaCompiler.value(proj.javaToolchains.compilerFor(gradleJvmSpec)) + } - if (task instanceof GroovyCompile) { - task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec)) - } + if (task instanceof GroovyCompile) { + task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec)) + } - if (task instanceof Test) { - task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec)) - } + if (task instanceof Test) { + task.javaLauncher.value(proj.javaToolchains.launcherFor(gradleJvmSpec)) } } } @@ -64,13 +79,20 @@ class JavaToolchainIntegrationTestPlugin extends BasePlugin { return JavaVersion.current() } + public static void skipIrrelevantTasks(Task task) { + task.project.with { + if (task.project.findProperty('toolchainJavaVersion')) { + task.enabled = false + } + + whenApplied(task.project) { + task.enabled = true + } + } + } + private static def getGradleJvmSpec() { def gradleJvmVerson = Integer.valueOf(getGradleJavaVersion().getMajorVersion()) return { languageVersion = JavaLanguageVersion.of(gradleJvmVerson) } } - - private static boolean isPluginApplied(Project project) { - return project.plugins.hasPlugin(JavaToolchainIntegrationTestPlugin.class) || - project.plugins.hasPlugin(JavaToolchainIntegrationTestPlugin.class.name) - } } diff --git a/integrationTests/gradle-java-toolchain/build.gradle b/integrationTests/gradle-java-toolchain/build.gradle index 467f349b..66971744 100644 --- a/integrationTests/gradle-java-toolchain/build.gradle +++ b/integrationTests/gradle-java-toolchain/build.gradle @@ -14,11 +14,11 @@ gretty { if (project.javaVersion.isJava9Compatible()) jvmArgs "--add-opens", "java.base/java.lang=ALL-UNNAMED" } +defineAsJavaToolchainAwareIntegrationTest() defineIntegrationTest() testAll.dependsOn defineIntegrationTestAllContainers(['jetty8', 'jetty9', 'jetty10', 'tomcat85', 'tomcat9']) tasks.withType(Test).configureEach { -// enabled = project.findProperty('toolchainJavaVersion') as boolean if(project.findProperty('toolchainJavaVersion')) { systemProperty 'toolchainJavaVersion', project.findProperty('toolchainJavaVersion') }