From e7bf588af4c0a4e8451276dcca50db017e0e47d4 Mon Sep 17 00:00:00 2001 From: Christoph Loy Date: Fri, 18 Oct 2024 17:53:09 +0200 Subject: [PATCH] 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..cb8286a1b9 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 (it.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 {