From c98b28b948604bd2f642e28301dff3bc4d1afdd6 Mon Sep 17 00:00:00 2001 From: Christoph Loy Date: Fri, 18 Oct 2024 18:15:50 +0200 Subject: [PATCH 1/4] Gradle: Configure compiler options via build-logic Moving these "uninteresting" options to build-logic cleans up the build.gradle file and makes it more readable --- .../gradle/SpockBasePlugin.groovy | 27 +++++++++++++++++++ build.gradle | 13 +-------- spock-specs/specs.gradle | 4 --- spock-testkit/testkit.gradle | 4 --- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy b/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy index 9de191e2d7..dfa1f4947b 100644 --- a/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy +++ b/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy @@ -19,11 +19,38 @@ package org.spockframework.gradle import groovy.transform.CompileStatic import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.tasks.compile.GroovyCompile +import org.gradle.api.tasks.compile.JavaCompile import org.gradle.api.tasks.testing.Test +import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.jvm.toolchain.JavaToolchainService @CompileStatic class SpockBasePlugin implements Plugin { + void apply(Project project) { + compileTasks(project) + testTasks(project) + } + + private static void compileTasks(Project project) { + project.with { + def javaToolchains = extensions.getByType(JavaToolchainService) + tasks.withType(JavaCompile).configureEach { comp -> + if (comp.name == 'compileJava') { + comp.javaCompiler.set(javaToolchains.compilerFor { + it.languageVersion.set(JavaLanguageVersion.of(8)) + }) + } + comp.options.encoding = 'UTF-8' + } + tasks.withType(GroovyCompile).configureEach { + it.options.encoding = 'UTF-8' + } + } + } + + private static void testTasks(Project project) { project.tasks.withType(Test).configureEach { task -> def taskName = task.name.capitalize() File configFile = project.file("Spock${taskName}Config.groovy") diff --git a/build.gradle b/build.gradle index 1582dfd9f5..dcb4219406 100644 --- a/build.gradle +++ b/build.gradle @@ -102,6 +102,7 @@ subprojects { apply plugin: "java-library" apply plugin: "groovy" apply plugin: "jacoco" + apply plugin: "org.spockframework.base" java { toolchain { @@ -109,18 +110,6 @@ subprojects { } } - tasks.withType(JavaCompile).configureEach { - if (it.name == 'compileJava') { - javaCompiler = javaToolchains.compilerFor { - languageVersion = JavaLanguageVersion.of(8) - } - } - options.encoding = 'UTF-8' - } - tasks.withType(GroovyCompile).configureEach { - options.encoding = 'UTF-8' - } - sourceSets.all { ss -> for (gv in variants.findAll { variant <= it }) { java { diff --git a/spock-specs/specs.gradle b/spock-specs/specs.gradle index 0111b2fc31..c7c86fcd2e 100644 --- a/spock-specs/specs.gradle +++ b/spock-specs/specs.gradle @@ -1,9 +1,5 @@ import org.spockframework.gradle.JacocoJavaagentProvider -plugins { - id 'org.spockframework.base' -} - ext.displayName = "Spock Framework - Specs for Core Module" description = "Spock specifications for the Core Module. Yes, we eat our own dog food." diff --git a/spock-testkit/testkit.gradle b/spock-testkit/testkit.gradle index 20aaa7d3ae..3d4910d066 100644 --- a/spock-testkit/testkit.gradle +++ b/spock-testkit/testkit.gradle @@ -1,7 +1,3 @@ -plugins { - id 'org.spockframework.base' -} - ext.displayName = "Spock Framework - Temp Specs for Core Module" //configurations { From 6be62e7039bd511c9097e5f93d76d2b719ed7e2b Mon Sep 17 00:00:00 2001 From: Christoph Loy Date: Fri, 18 Oct 2024 18:23:32 +0200 Subject: [PATCH 2/4] Gradle: Extract Compiler version to constant --- .../groovy/org/spockframework/gradle/SpockBasePlugin.groovy | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy b/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy index dfa1f4947b..450fa0f6b5 100644 --- a/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy +++ b/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy @@ -28,6 +28,8 @@ import org.gradle.jvm.toolchain.JavaToolchainService @CompileStatic class SpockBasePlugin implements Plugin { + private static final JavaLanguageVersion COMPILER_VERSION = JavaLanguageVersion.of(8) + void apply(Project project) { compileTasks(project) testTasks(project) @@ -39,7 +41,7 @@ class SpockBasePlugin implements Plugin { tasks.withType(JavaCompile).configureEach { comp -> if (comp.name == 'compileJava') { comp.javaCompiler.set(javaToolchains.compilerFor { - it.languageVersion.set(JavaLanguageVersion.of(8)) + it.languageVersion.set(COMPILER_VERSION) }) } comp.options.encoding = 'UTF-8' From 737d549178b95d8a3c2f246f3dbb330953315c11 Mon Sep 17 00:00:00 2001 From: Christoph Loy Date: Fri, 18 Oct 2024 18:42:08 +0200 Subject: [PATCH 3/4] Gradle: Fix deprecation in verifyOSGi task --- spock-core/core.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spock-core/core.gradle b/spock-core/core.gradle index 7db47aa758..0bc05f9684 100644 --- a/spock-core/core.gradle +++ b/spock-core/core.gradle @@ -96,7 +96,7 @@ tasks.named("processResources") { tasks.register("coreConsole", JavaExec) { description = 'Start a groovy Console with Spock Core Classpath, useful for AST-Inspection' mainClass = variant == 2.5 ? "groovy.ui.Console" : "groovy.console.ui.Console" - classpath(sourceSets.named("main").map {it.runtimeClasspath }, configurations.named("coreConsoleRuntime")) + classpath(sourceSets.named("main").map { it.runtimeClasspath }, configurations.named("coreConsoleRuntime")) workingDir = file('build/console') ignoreExitValue true args file('CoreConsole.groovy').absolutePath @@ -119,7 +119,7 @@ def osgiProperties = tasks.register('osgiProperties', WriteProperties) { // that its metadata is valid. If the metadata is invalid this task will // fail. def verifyOSGi = tasks.register('verifyOSGi', Resolve) { - getBndrun().fileProvider(osgiProperties.map { it.outputFile }) + getBndrun().set(osgiProperties.flatMap { it.destinationFile }) getOutputBndrun().set(layout.getBuildDirectory().file("resolvedOSGiProperties.bndrun")) reportOptional = false // By default bnd will use jars found in: From d308cd4bc2a315778fe81b6b5ae5b4f69d97b4b0 Mon Sep 17 00:00:00 2001 From: Christoph Loy Date: Sat, 19 Oct 2024 15:30:52 +0200 Subject: [PATCH 4/4] Gradle: Add testing to SpockBasePlugin --- .../gradle/SpockBasePlugin.groovy | 7 ++-- .../gradle/SpockBasePluginSpec.groovy | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 build-logic/base/src/test/groovy/org/spockframework/gradle/SpockBasePluginSpec.groovy diff --git a/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy b/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy index 450fa0f6b5..191d9e10fa 100644 --- a/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy +++ b/build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy @@ -19,16 +19,19 @@ package org.spockframework.gradle import groovy.transform.CompileStatic import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.compile.GroovyCompile import org.gradle.api.tasks.compile.JavaCompile import org.gradle.api.tasks.testing.Test import org.gradle.jvm.toolchain.JavaLanguageVersion import org.gradle.jvm.toolchain.JavaToolchainService +import org.jetbrains.annotations.VisibleForTesting @CompileStatic class SpockBasePlugin implements Plugin { - private static final JavaLanguageVersion COMPILER_VERSION = JavaLanguageVersion.of(8) + @VisibleForTesting + public static final JavaLanguageVersion COMPILER_VERSION = JavaLanguageVersion.of(8) void apply(Project project) { compileTasks(project) @@ -39,7 +42,7 @@ class SpockBasePlugin implements Plugin { project.with { def javaToolchains = extensions.getByType(JavaToolchainService) tasks.withType(JavaCompile).configureEach { comp -> - if (comp.name == 'compileJava') { + if (comp.name == JavaPlugin.COMPILE_JAVA_TASK_NAME) { comp.javaCompiler.set(javaToolchains.compilerFor { it.languageVersion.set(COMPILER_VERSION) }) diff --git a/build-logic/base/src/test/groovy/org/spockframework/gradle/SpockBasePluginSpec.groovy b/build-logic/base/src/test/groovy/org/spockframework/gradle/SpockBasePluginSpec.groovy new file mode 100644 index 0000000000..28f3d7e56d --- /dev/null +++ b/build-logic/base/src/test/groovy/org/spockframework/gradle/SpockBasePluginSpec.groovy @@ -0,0 +1,32 @@ +package org.spockframework.gradle + +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Specification + +class SpockBasePluginSpec extends Specification { + + def 'Compile settings are configured'() { + setup: + def project = createProject() + + when: + def compileJavaTasks = project.tasks.withType(JavaCompile) + def compileJava = project.tasks.getByName(JavaPlugin.COMPILE_JAVA_TASK_NAME) as JavaCompile + + then: + compileJavaTasks.every { it.options.encoding == "UTF-8" } + compileJava.javaCompiler.get().metadata.languageVersion == SpockBasePlugin.COMPILER_VERSION + } + + private static Project createProject() { + def result = ProjectBuilder.builder() + .build() + result.plugins.apply("java-library") + result.plugins.apply("groovy") + result.plugins.apply(SpockBasePlugin) + return result + } +}