Skip to content

Commit 895b5d3

Browse files
committed
Extract more build logic into Convention Plugin
1 parent 18b4de9 commit 895b5d3

File tree

4 files changed

+85
-53
lines changed

4 files changed

+85
-53
lines changed

build-logic/base/src/main/groovy/org/spockframework/gradle/SpockBasePlugin.groovy

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,42 @@ package org.spockframework.gradle
1919
import groovy.transform.CompileStatic
2020
import org.gradle.api.Plugin
2121
import org.gradle.api.Project
22+
import org.gradle.api.artifacts.VersionCatalogsExtension
23+
import org.gradle.api.plugins.ExtraPropertiesExtension
2224
import org.gradle.api.plugins.JavaPlugin
25+
import org.gradle.api.tasks.SourceSetContainer
26+
import org.gradle.api.tasks.bundling.Jar
2327
import org.gradle.api.tasks.compile.GroovyCompile
2428
import org.gradle.api.tasks.compile.JavaCompile
29+
import org.gradle.api.tasks.diagnostics.DependencyInsightReportTask
2530
import org.gradle.api.tasks.testing.Test
2631
import org.gradle.jvm.toolchain.JavaLanguageVersion
2732
import org.gradle.jvm.toolchain.JavaToolchainService
33+
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
2834
import org.jetbrains.annotations.VisibleForTesting
2935

36+
import java.time.Duration
37+
3038
@CompileStatic
3139
class SpockBasePlugin implements Plugin<Project> {
3240

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

3644
void apply(Project project) {
45+
applyPlugins(project)
3746
compileTasks(project)
47+
jarTasks(project)
3848
testTasks(project)
49+
jacoco(project)
50+
dependencyInsight(project)
51+
}
52+
53+
static void applyPlugins(Project project) {
54+
def plugins = project.plugins
55+
plugins.apply("java-library")
56+
plugins.apply("groovy")
57+
plugins.apply("jacoco")
3958
}
4059

4160
private static void compileTasks(Project project) {
@@ -55,13 +74,63 @@ class SpockBasePlugin implements Plugin<Project> {
5574
}
5675
}
5776

77+
private static void jarTasks(Project project) {
78+
project.tasks.withType(Jar).configureEach { jar ->
79+
/*
80+
* Ensure the jar can be built in a reproducible manner, This shall prevent build cache misses, when different variants are tested.
81+
*/
82+
jar.preserveFileTimestamps = false
83+
jar.reproducibleFileOrder = true
84+
}
85+
86+
def sourceSets = project.extensions.getByType(SourceSetContainer)
87+
project.tasks.register("sourcesJar", Jar) {
88+
it.archiveClassifier.set("sources")
89+
it.from(sourceSets.named("main").map { it.allSource })
90+
}
91+
92+
project.tasks.register("javadocJar", Jar) {
93+
it.archiveClassifier.set("javadoc")
94+
it.from(project.tasks.named("javadoc"))
95+
}
96+
}
97+
5898
private static void testTasks(Project project) {
5999
project.tasks.withType(Test).configureEach { task ->
100+
task.useJUnitPlatform()
60101
def taskName = task.name.capitalize()
102+
def variant = project.rootProject.extensions.getByType(ExtraPropertiesExtension)
103+
.get("variant")
104+
105+
def junitXml = task.reports.junitXml
106+
junitXml.outputLocation.set(project.file("${junitXml.outputLocation.get()}/$taskName-$variant"))
107+
def html = task.reports.html
108+
html.outputLocation.set(project.file("${html.outputLocation.get()}/$taskName-$variant"))
109+
110+
// As a generous general timeout, instead of the 6h of GHA.
111+
// But only on CI or longer needing debug sessions get killed by the timeout.
112+
boolean isCiServer = System.getenv("CI") || System.getenv("GITHUB_ACTIONS")
113+
if (isCiServer) {
114+
task.timeout.set(Duration.ofMinutes(15))
115+
}
116+
61117
File configFile = project.file("Spock${taskName}Config.groovy")
62118
if (configFile.exists()) {
63119
task.jvmArgumentProviders.add(new SpockConfigArgumentProvider(configFile))
64120
}
65121
}
66122
}
123+
124+
private static void jacoco(Project project) {
125+
// For tests, we have to handle the case where version catalogs are not present
126+
project.rootProject.extensions.findByType(VersionCatalogsExtension)?.find("libs")
127+
?.flatMap { it.findVersion("jacoco") }
128+
?.ifPresent { version ->
129+
project.extensions.getByType(JacocoPluginExtension).toolVersion = version.toString()
130+
}
131+
}
132+
133+
private static void dependencyInsight(Project project) {
134+
project.tasks.register("allDependencyInsight", DependencyInsightReportTask)
135+
}
67136
}

build-logic/base/src/test/groovy/org/spockframework/gradle/SpockBasePluginSpec.groovy

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
//file:noinspection ConfigurationAvoidance
12
package org.spockframework.gradle
23

34
import org.gradle.api.Project
45
import org.gradle.api.plugins.JavaPlugin
56
import org.gradle.api.tasks.compile.JavaCompile
7+
import org.gradle.jvm.tasks.Jar
68
import org.gradle.testfixtures.ProjectBuilder
79
import spock.lang.Specification
810

@@ -21,11 +23,23 @@ class SpockBasePluginSpec extends Specification {
2123
compileJava.javaCompiler.get().metadata.languageVersion == SpockBasePlugin.COMPILER_VERSION
2224
}
2325

26+
def "Jar settings are configured"() {
27+
setup:
28+
def project = createProject()
29+
30+
when:
31+
def jarTasks = project.tasks.withType(Jar)
32+
33+
then:
34+
jarTasks.every { it.reproducibleFileOrder }
35+
jarTasks.every { !it.preserveFileTimestamps }
36+
project.tasks.findByName("sourcesJar") != null
37+
project.tasks.findByName("javadocJar") != null
38+
}
39+
2440
private static Project createProject() {
2541
def result = ProjectBuilder.builder()
2642
.build()
27-
result.plugins.apply("java-library")
28-
result.plugins.apply("groovy")
2943
result.plugins.apply(SpockBasePlugin)
3044
return result
3145
}

build.gradle

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ allprojects {
8585
group = "org.spockframework"
8686
version = fullVersion
8787

88-
apply from: script("common")
89-
9088
// ignore mutable data that is irrelevant for compilation output
9189
normalization {
9290
runtimeClasspath {
@@ -99,11 +97,7 @@ allprojects {
9997

10098
apply from: script("ide")
10199

102-
boolean isCiServer = System.env["CI"] || System.env["GITHUB_ACTIONS"]
103100
subprojects {
104-
apply plugin: "java-library"
105-
apply plugin: "groovy"
106-
apply plugin: "jacoco"
107101
apply plugin: "org.spockframework.base"
108102

109103
java {
@@ -165,48 +159,6 @@ subprojects {
165159

166160
configureJavadoc(tasks.named("javadoc"))
167161
configureGroovydoc(tasks.named("groovydoc"))
168-
169-
tasks.register("sourcesJar", Jar) {
170-
archiveClassifier = "sources"
171-
from sourceSets.main.allSource
172-
}
173-
174-
tasks.register("javadocJar", Jar) {
175-
archiveClassifier = "javadoc"
176-
from javadoc
177-
}
178-
179-
tasks.withType(Jar).configureEach {
180-
/*
181-
* Ensure the jar can be built in a reproducible manner, This shall prevent build cache misses, when different variants are tested.
182-
*/
183-
preserveFileTimestamps = false
184-
reproducibleFileOrder = true
185-
}
186-
187-
tasks.withType(Test).configureEach {
188-
useJUnitPlatform()
189-
def taskName = name
190-
reports {
191-
junitXml {
192-
outputLocation = file("${outputLocation.get()}/$taskName-$variant")
193-
}
194-
html {
195-
outputLocation = file("${outputLocation.get()}/$taskName-$variant")
196-
}
197-
}
198-
// As a generous general timeout, instead of the 6h of GHA.
199-
// But only on CI or longer needing debug sessions get killed by the timeout.
200-
if (isCiServer) {
201-
timeout = Duration.ofMinutes(15)
202-
}
203-
}
204-
205-
tasks.register("allDependencyInsight", DependencyInsightReportTask) {}
206-
207-
jacoco {
208-
toolVersion = libs.versions.jacoco.get()
209-
}
210162
}
211163

212164
tasks.register("collectTestXml") {

gradle/common.gradle

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)