Skip to content

Commit 9f643d4

Browse files
committed
chore: configure source set via extension method
1 parent dfefaea commit 9f643d4

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

dd-java-agent/instrumentation/build.gradle

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ subprojects { Project subProj ->
3333
if (!plugins.hasPlugin("java")) {
3434
return
3535
}
36-
String jdkCompile = null
37-
if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests') != JavaVersion.VERSION_1_8) {
38-
def version = JavaVersion.toVersion(project.getProperty('minJavaVersionForTests'))
39-
def name = "java$version.majorVersion"
40-
jdkCompile = "main_${name}Implementation"
41-
}
36+
37+
// Configures base dependencies for additional sourceSet
38+
configurations
39+
.matching { it.name.matches("${SourceSet.MAIN_SOURCE_SET_NAME}_java\\d+${JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME.capitalize()}") }
40+
.configureEach {
41+
it.dependencies.add(project.dependencyFactory.create(project(':dd-trace-api')))
42+
it.dependencies.add(project.dependencyFactory.create(project(':dd-java-agent:agent-tooling')))
43+
it.dependencies.addLater(libs.bytebuddy)
44+
}
45+
4246
configurations.named('muzzleBootstrap') {
4347
exclude group: 'org.snakeyaml', module: 'snakeyaml-engine' // we vendor this in the agent jar
4448
}
@@ -47,11 +51,6 @@ subprojects { Project subProj ->
4751
implementation project(':dd-trace-api')
4852
implementation project(':dd-java-agent:agent-tooling')
4953
implementation libs.bytebuddy
50-
if (jdkCompile) {
51-
"$jdkCompile" project(':dd-trace-api')
52-
"$jdkCompile" project(':dd-java-agent:agent-tooling')
53-
"$jdkCompile" libs.bytebuddy
54-
}
5554

5655
annotationProcessor project(':dd-java-agent:instrumentation-annotation-processor')
5756
annotationProcessor libs.autoservice.processor

gradle/java_no_deps.gradle

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ apply from: "$rootDir/gradle/spotbugs.gradle"
1111
apply from: "$rootDir/gradle/repositories.gradle"
1212
apply from: "$rootDir/gradle/test-suites.gradle"
1313

14+
// Glue code for Groovy DSL.
15+
// TODO this extension need to go away once we move to convention plugins
16+
ext.configureCompiler = (AbstractCompile it, int toolchainVersion, JavaVersion compatibilityVersion = null, String unsetReleaseFlagReason = null) -> {
17+
configureCompiler(it, toolchainVersion, compatibilityVersion, unsetReleaseFlagReason)
18+
} as Closure<Void>
1419

1520
@CompileStatic
1621
class TracerJavaExtension {
@@ -23,6 +28,44 @@ class TracerJavaExtension {
2328
this.project = project
2429
this.skipSettingTestJavaVersion = objects.property(Boolean).convention(providers.provider { project.findProperty('skipSettingTestJavaVersion') as Boolean })
2530
}
31+
32+
/**
33+
* Adds the source set `src/main/javaXX`, configures its compilation and its test jvm constraints.
34+
*
35+
* @param javaVersion The wanted java version for these source.
36+
* @param compileOnly Whether the compilation output is added as compileOnly.
37+
* @return
38+
*/
39+
def addSourceSetFor(JavaVersion javaVersion, Boolean compileOnly = false) {
40+
project.extensions.getByType(TestJvmConstraintsExtension).minJavaVersionForTests.set(javaVersion)
41+
42+
def version = javaVersion
43+
def name = "java${version.majorVersion}"
44+
def sourceSets = project.extensions.getByType(SourceSetContainer)
45+
46+
def mainForJavaVersionSourceSet = sourceSets.create("${SourceSet.MAIN_SOURCE_SET_NAME}_$name") {
47+
(it as SourceSet).java.srcDirs = ["${project.projectDir}/src/${SourceSet.MAIN_SOURCE_SET_NAME}/$name"]
48+
}
49+
50+
project.tasks.named(mainForJavaVersionSourceSet.compileJavaTaskName, JavaCompile) {
51+
// TODO Make 'configureCompiler' a general utility method
52+
def cl = project.extensions.getByType(ExtraPropertiesExtension).get("configureCompiler") as Closure
53+
cl.call(it, version.majorVersion.toInteger().intValue(), version)
54+
}
55+
56+
// "socket-utils" is only set to compileOnly because the implementation dependency incorrectly adds Java17 classes to all jar prefixes.
57+
// This causes the AgentJarIndex to search for other non-Java17 classes in the wrong prefix location and fail to resolve class names.
58+
if (compileOnly) {
59+
project.dependencies.add("compileOnly", mainForJavaVersionSourceSet.output)
60+
} else {
61+
project.dependencies.add("compileOnly", project.files(mainForJavaVersionSourceSet.compileClasspath))
62+
project.dependencies.add("implementation", mainForJavaVersionSourceSet.output)
63+
}
64+
65+
project.tasks.named("jar", Jar) {
66+
it.from(mainForJavaVersionSourceSet.output)
67+
}
68+
}
2669
}
2770
def tracerJavaExtension = extensions.create(TracerJavaExtension.NAME, TracerJavaExtension, objects, providers, project)
2871

@@ -67,33 +110,8 @@ java {
67110
withSourcesJar()
68111
}
69112

70-
// TODO configures the source sets via the extension
71113
if (project.hasProperty('minJavaVersionForTests') && project.findProperty('minJavaVersionForTests') != JavaVersion.VERSION_1_7) {
72-
def version = JavaVersion.toVersion(project.findProperty('minJavaVersionForTests'))
73-
def name = "java${version.majorVersion}"
74-
def mainForJavaVersionSourceSet = sourceSets.create("main_$name") {
75-
java.srcDirs = ["${project.projectDir}/src/main/$name"]
76-
}
77-
78-
// Task name is registered when source set was created
79-
tasks.named(mainForJavaVersionSourceSet.compileJavaTaskName, JavaCompile) {
80-
configureCompiler(it, version.majorVersion.toInteger().intValue(), version)
81-
}
82-
83-
// "socket-utils" is only set to compileOnly because the implementation dependency incorrectly adds Java17 classes to all jar prefixes.
84-
// This causes the AgentJarIndex to search for other non-Java17 classes in the wrong prefix location and fail to resolve class names.
85-
dependencies {
86-
if ("${project.projectDir}".endsWith("socket-utils")) {
87-
compileOnly(files(project.sourceSets."main_$name".output))
88-
} else {
89-
compileOnly(files(project.sourceSets."main_$name".compileClasspath))
90-
implementation(files(project.sourceSets."main_$name".output))
91-
}
92-
}
93-
94-
tasks.named("jar", Jar) {
95-
from sourceSets."main_$name".output
96-
}
114+
tracerJavaExtension.addSourceSetFor(project.findProperty('minJavaVersionForTests'))
97115

98116
// TODO configure this via extension
99117
// If unset (hasProperty is false), or value is explicitly set to false (case not seen), then
@@ -259,8 +277,10 @@ tasks.withType(JavaCompile).configureEach {
259277
* For java changes the compiler from the toolchain, and adapts its configuration.
260278
*
261279
* For Groovy and Scala compile tasks only sets the launcher.
280+
*
281+
* NOTE: This function is exposed via a closure
262282
*/
263-
ext.configureCompiler = (AbstractCompile it, int toolchainVersion, JavaVersion compatibilityVersion = null, String unsetReleaseFlagReason = null) -> {
283+
def configureCompiler(AbstractCompile it, int toolchainVersion, JavaVersion compatibilityVersion = null, String unsetReleaseFlagReason = null) {
264284
Provider<JavaCompiler> compiler = javaToolchains.compilerFor {
265285
languageVersion = JavaLanguageVersion.of(toolchainVersion)
266286
}
@@ -309,7 +329,7 @@ ext.configureCompiler = (AbstractCompile it, int toolchainVersion, JavaVersion c
309329
} catch (NoSuchElementException ignored) {
310330
throw new GradleException("Unable to find compiler for Java $toolchainVersion. Have you set JAVA_${toolchainVersion}_HOME?")
311331
}
312-
} as Closure<Void>
332+
}
313333

314334
ext.configureGroovyCompiler = (int toolchainVersion, String... taskNames) -> {
315335
taskNames.each { taskName ->

0 commit comments

Comments
 (0)