Skip to content

Commit

Permalink
Configure compiler options via build-logic (#2026)
Browse files Browse the repository at this point in the history
Moves some of the "uninteresting" options to build-logic to clean up
`<projectname>.gradle` files.

The basic idea of this PR is that much of the tedious configuration
shall happen in the spockframework.base plugin.
All subprojects apply that plugin and profit from the abstraction.

"Interesting stuff" like declaration of dependencies shall still happen
in `<projectname>.gradle` files.
I'd like to expand this idea , but I want to keep the PRs small and
start with the most simple stuff first.
  • Loading branch information
beatbrot authored Dec 20, 2024
1 parent 2436da9 commit f516604
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,43 @@ 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<Project> {

@VisibleForTesting
public static final JavaLanguageVersion COMPILER_VERSION = JavaLanguageVersion.of(8)

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 == JavaPlugin.COMPILE_JAVA_TASK_NAME) {
comp.javaCompiler.set(javaToolchains.compilerFor {
it.languageVersion.set(COMPILER_VERSION)
})
}
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
Original file line number Diff line number Diff line change
@@ -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
}
}
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: 2 additions & 2 deletions spock-core/core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
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 f516604

Please sign in to comment.