@@ -19,23 +19,42 @@ package org.spockframework.gradle
1919import groovy.transform.CompileStatic
2020import org.gradle.api.Plugin
2121import org.gradle.api.Project
22+ import org.gradle.api.artifacts.VersionCatalogsExtension
23+ import org.gradle.api.plugins.ExtraPropertiesExtension
2224import org.gradle.api.plugins.JavaPlugin
25+ import org.gradle.api.tasks.SourceSetContainer
26+ import org.gradle.api.tasks.bundling.Jar
2327import org.gradle.api.tasks.compile.GroovyCompile
2428import org.gradle.api.tasks.compile.JavaCompile
29+ import org.gradle.api.tasks.diagnostics.DependencyInsightReportTask
2530import org.gradle.api.tasks.testing.Test
2631import org.gradle.jvm.toolchain.JavaLanguageVersion
2732import org.gradle.jvm.toolchain.JavaToolchainService
33+ import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
2834import org.jetbrains.annotations.VisibleForTesting
2935
36+ import java.time.Duration
37+
3038@CompileStatic
3139class 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}
0 commit comments