Skip to content

Commit 1be5c00

Browse files
Introduced groovySpock plugin for explicit Groovy and Spock configuration.
1 parent c8bb444 commit 1be5c00

File tree

52 files changed

+332
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+332
-59
lines changed

build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import com.diffplug.gradle.spotless.SpotlessExtension
22
import datadog.gradle.plugin.ci.testAggregate
33

44
plugins {
5+
kotlin("jvm") version libs.versions.kotlin.plugin apply false
6+
57
id("datadog.gradle-debug")
68
id("datadog.dependency-locking")
79
id("datadog.tracer-version")
810
id("datadog.dump-hanged-test")
11+
id("datadog.groovy-spock")
912
id("config-inversion-linter")
1013
id("datadog.ci-jobs")
1114

@@ -16,7 +19,6 @@ plugins {
1619
id("com.gradleup.shadow") version "8.3.6" apply false
1720
id("me.champeau.jmh") version "0.7.3" apply false
1821
id("org.gradle.playframework") version "0.13" apply false
19-
kotlin("jvm") version libs.versions.kotlin.plugin apply false
2022
}
2123

2224
description = "dd-trace-java"

buildSrc/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ gradlePlugin {
1818
id = "instrument"
1919
implementationClass = "InstrumentPlugin"
2020
}
21+
2122
create("muzzle-plugin") {
2223
id = "muzzle"
2324
implementationClass = "datadog.gradle.plugin.muzzle.MuzzlePlugin"
@@ -26,18 +27,27 @@ gradlePlugin {
2627
id = "call-site-instrumentation"
2728
implementationClass = "datadog.gradle.plugin.csi.CallSiteInstrumentationPlugin"
2829
}
30+
31+
create("groovy-spock-plugin") {
32+
id = "datadog.groovy-spock"
33+
implementationClass = "datadog.gradle.plugin.config.GroovySpockConventionPlugin"
34+
}
35+
2936
create("tracer-version-plugin") {
3037
id = "datadog.tracer-version"
3138
implementationClass = "datadog.gradle.plugin.version.TracerVersionPlugin"
3239
}
40+
3341
create("dump-hanged-test-plugin") {
3442
id = "datadog.dump-hanged-test"
3543
implementationClass = "datadog.gradle.plugin.dump.DumpHangedTestPlugin"
3644
}
45+
3746
create("supported-config-generation") {
3847
id = "supported-config-generator"
3948
implementationClass = "datadog.gradle.plugin.config.SupportedConfigPlugin"
4049
}
50+
4151
create("supported-config-linter") {
4252
id = "config-inversion-linter"
4353
implementationClass = "datadog.gradle.plugin.config.ConfigInversionLinter"
@@ -87,6 +97,7 @@ testing {
8797
suites {
8898
val test by getting(JvmTestSuite::class) {
8999
dependencies {
100+
// TODO: We can refactor `buildSrc` code and tests to not use Groovy and Spock at all.
90101
implementation(libs.groovy)
91102
implementation(libs.spock.core)
92103
}

buildSrc/call-site-instrumentation-plugin/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ dependencies {
3232
implementation("org.freemarker", "freemarker", "2.3.30")
3333
implementation(libs.asm)
3434
implementation(libs.asm.tree)
35-
implementation("com.github.javaparser", "javaparser-symbol-solver-core", "3.24.4")
35+
implementation(libs.javaparser.solver)
3636

3737
testImplementation(libs.bytebuddy)
3838
testImplementation(libs.groovy)
39-
testImplementation(libs.bundles.spock)
39+
testImplementation(libs.spock.core)
40+
testImplementation(libs.objenesis)
4041
testImplementation("javax.servlet", "javax.servlet-api", "3.0.1")
4142
testImplementation("com.github.spotbugs", "spotbugs-annotations", "4.2.0")
4243
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package datadog.gradle.plugin.config
2+
3+
import groovy.lang.Closure
4+
import org.gradle.api.JavaVersion
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
import org.gradle.api.artifacts.VersionCatalogsExtension
8+
import org.gradle.api.model.ObjectFactory
9+
import org.gradle.api.plugins.JavaLibraryPlugin
10+
import org.gradle.api.plugins.JavaPlugin
11+
import org.gradle.api.provider.MapProperty
12+
import org.gradle.api.tasks.compile.GroovyCompile
13+
import org.gradle.kotlin.dsl.extra
14+
import org.gradle.kotlin.dsl.mapProperty
15+
import org.gradle.kotlin.dsl.named
16+
import javax.inject.Inject
17+
18+
/**
19+
* Convention plugin that will configure Groovy and Spock.
20+
*
21+
* If nothing configured, then default configuration will be applied.
22+
*
23+
* Following dependencies will be added to `testImplementation`:
24+
* * groovy-core
25+
* * groovy-json
26+
* * spock-core
27+
* * objenesis
28+
*/
29+
@Suppress("unused")
30+
class GroovySpockConventionPlugin : Plugin<Project> {
31+
abstract class GroovySpockProperties @Inject constructor(objects: ObjectFactory) {
32+
internal val compilerSettings: MapProperty<Int, List<String>> = objects.mapProperty()
33+
34+
internal val configurations: MapProperty<String, List<Any>> = objects.mapProperty()
35+
36+
fun configureGroovyCompiler(toolchainVersion: Int, vararg taskNames: String) {
37+
compilerSettings.put(toolchainVersion, taskNames.toList())
38+
}
39+
40+
fun configureDependencies(taskName: String, vararg dependencies: Any) {
41+
configurations.put(taskName, dependencies.toList())
42+
}
43+
}
44+
45+
override fun apply(project: Project) {
46+
if (project == project.rootProject) {
47+
project.subprojects {
48+
this.pluginManager.apply(GroovySpockConventionPlugin::class.java)
49+
}
50+
return
51+
}
52+
53+
val extension = project.extensions.create("groovySpock", GroovySpockProperties::class.java)
54+
55+
configureCompiler(project, extension)
56+
57+
project.plugins.withType(JavaPlugin::class.java) {
58+
configureDependencies(project, extension)
59+
}
60+
61+
project.plugins.withType(JavaLibraryPlugin::class.java) {
62+
configureDependencies(project, extension)
63+
}
64+
}
65+
66+
private fun configureCompiler(project: Project, properties: GroovySpockProperties) {
67+
val compilerSettings = properties.compilerSettings
68+
69+
if (compilerSettings.isPresent) {
70+
compilerSettings.get().forEach { (toolchainVersion: Int, taskNames: List<String>) ->
71+
taskNames.forEach { taskName ->
72+
project.tasks.named<GroovyCompile>(taskName) {
73+
(project.extra["configureCompiler"] as Closure<*>).call(this, toolchainVersion, JavaVersion.VERSION_1_8, null)
74+
}
75+
}
76+
}
77+
}
78+
}
79+
80+
private fun configureDependencies(project: Project, properties: GroovySpockProperties) {
81+
// TODO: check `afterEvaluate` can be refactored to something else?
82+
project.afterEvaluate {
83+
val catalogs = project.extensions.getByType(VersionCatalogsExtension::class.java).named("libs")
84+
85+
fun findLibrary(alias: String) = catalogs.findLibrary(alias)
86+
.orElseThrow { IllegalArgumentException("Library not found in catalog for alias: $alias") }
87+
88+
val defaultLibs = listOf(
89+
findLibrary("groovy"),
90+
findLibrary("groovy-json"),
91+
findLibrary("spock-core"),
92+
findLibrary("objenesis")
93+
)
94+
95+
if (properties.configurations.isPresent) {
96+
val configurations = properties.configurations.get()
97+
if (configurations.isEmpty()) {
98+
// If nothing configured, setup default configuration, suitable in most cases.
99+
defaultLibs.forEach { project.dependencies.add("testImplementation", it) }
100+
} else {
101+
configurations.forEach { (cfg, deps) ->
102+
defaultLibs.forEach { project.dependencies.add(cfg, it) }
103+
deps.forEach { project.dependencies.add(cfg, it) }
104+
}
105+
}
106+
} else {
107+
// If nothing configured, setup default configuration, suitable in most cases.
108+
defaultLibs.forEach { project.dependencies.add("testImplementation", it) }
109+
}
110+
}
111+
}
112+
}

dd-java-agent/agent-ci-visibility/civisibility-instrumentation-test-fixtures/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
apply from: "$rootDir/gradle/java.gradle"
22
apply from: "$rootDir/gradle/version.gradle"
33

4+
groovySpock {
5+
configureDependencies("compileOnly")
6+
}
7+
48
dependencies {
59
api project(':dd-java-agent:instrumentation-testing')
610
api project(':dd-java-agent:agent-ci-visibility:civisibility-test-fixtures')

dd-java-agent/agent-ci-visibility/civisibility-test-fixtures/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
apply from: "$rootDir/gradle/java.gradle"
22
apply from: "$rootDir/gradle/version.gradle"
33

4+
groovySpock {
5+
configureDependencies("compileOnly")
6+
}
7+
48
dependencies {
59
api project(':dd-java-agent:agent-ci-visibility')
610
api project(':dd-java-agent:testing')
@@ -11,5 +15,7 @@ dependencies {
1115
api libs.jackson.databind
1216
api group: 'org.msgpack', name: 'jackson-dataformat-msgpack', version: '0.9.6'
1317
api group: 'org.xmlunit', name: 'xmlunit-core', version: '2.10.3'
18+
19+
compileOnly libs.junit.jupiter
1420
}
1521

dd-java-agent/agent-iast/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ protobuf {
3434
}
3535
}
3636

37+
groovySpock {
38+
configureDependencies("testImplementation", libs.groovy.yaml)
39+
}
40+
3741
dependencies {
3842
api libs.slf4j
3943

@@ -50,7 +54,6 @@ dependencies {
5054
testImplementation project(':dd-java-agent:agent-bootstrap')
5155
testImplementation libs.bytebuddy
5256
testImplementation('org.skyscreamer:jsonassert:1.5.1')
53-
testImplementation libs.groovy.yaml
5457
testImplementation libs.guava
5558

5659
testImplementation group: 'io.grpc', name: 'grpc-core', version: grpcVersion

dd-java-agent/agent-iast/iast-test-fixtures/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
apply from: "$rootDir/gradle/java.gradle"
22
apply from: "$rootDir/gradle/version.gradle"
33

4+
groovySpock {
5+
configureDependencies("compileOnly")
6+
}
7+
48
dependencies {
59
api project(':dd-java-agent:agent-iast')
610
api project(':dd-java-agent:instrumentation-testing')

dd-java-agent/appsec/appsec-test-fixtures/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
apply from: "$rootDir/gradle/java.gradle"
22
apply from: "$rootDir/gradle/version.gradle"
33

4+
groovySpock {
5+
configureDependencies("compileOnly")
6+
}
7+
48
dependencies {
59
api project(':dd-java-agent:appsec')
610
api project(':dd-java-agent:instrumentation-testing')
11+
12+
compileOnly(libs.junit.jupiter)
713
}
814

915
configurations.named('api') {

dd-java-agent/instrumentation-testing/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
apply from: "$rootDir/gradle/java.gradle"
22

3+
groovySpock {
4+
configureDependencies("compileOnly")
5+
configureDependencies("testImplementation")
6+
}
7+
38
dependencies {
49
api libs.bytebuddy
510
api libs.bytebuddyagent
611
api libs.slf4j
7-
api libs.bundles.spock
812
api libs.bundles.test.logging
913
api libs.guava
1014

1115
api project(':dd-java-agent:testing')
1216

1317
implementation project(':dd-java-agent:agent-debugger')
1418

19+
implementation(libs.junit.jupiter)
1520
implementation "org.junit.platform:junit-platform-runner:${libs.versions.junit.platform.get()}"
1621

1722
testImplementation project(':dd-java-agent:instrumentation:trace-annotation')

0 commit comments

Comments
 (0)