Skip to content

Commit

Permalink
Gradle: Configure compiler options via build-logic
Browse files Browse the repository at this point in the history
Moving these "uninteresting" options to build-logic cleans up
the build.gradle file and makes it more readable
  • Loading branch information
beatbrot committed Oct 18, 2024
1 parent 2b87cff commit 1436d62
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {

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")
Expand Down
13 changes: 1 addition & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,14 @@ subprojects {
apply plugin: "java-library"
apply plugin: "groovy"
apply plugin: "jacoco"
apply plugin: "org.spockframework.base"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(javaVersion)
}
}

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 {
Expand Down
4 changes: 0 additions & 4 deletions spock-specs/specs.gradle
Original file line number Diff line number Diff line change
@@ -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."
Expand Down
4 changes: 0 additions & 4 deletions spock-testkit/testkit.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
plugins {
id 'org.spockframework.base'
}

ext.displayName = "Spock Framework - Temp Specs for Core Module"

//configurations {
Expand Down

0 comments on commit 1436d62

Please sign in to comment.