Skip to content

Commit 975044a

Browse files
committed
chore: Handle test sources compilation override
1 parent 39c00ee commit 975044a

File tree

6 files changed

+83
-51
lines changed

6 files changed

+83
-51
lines changed

dd-java-agent/agent-profiling/profiling-controller-ddprof/build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// Set properties before any plugins get loaded
2-
ext {
3-
// By default tests with be compiled for `minJavaVersionForTests` version,
4-
// but in this case we would like to avoid this since we would like to run with ZULU8
5-
skipSettingTestJavaVersion = true
6-
}
7-
81
apply from: "$rootDir/gradle/java.gradle"
92
apply plugin: 'idea'
103

4+
tracerJava {
5+
addSourceSetFor(JavaVersion.VERSION_11) {
6+
// By default tests with be compiled for `minJavaVersionForTests` version,
7+
// but in this case we would like to avoid this since we would like to run with ZULU8
8+
applyForTestSources = false
9+
}
10+
}
11+
1112
testJvmConstraint {
1213
minJavaVersionForTests = JavaVersion.VERSION_11
1314
excludeJdk = ['SEMERU11', 'SEMERU17']

dd-java-agent/agent-profiling/profiling-controller-openjdk/build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// Set properties before any plugins get loaded
2-
ext {
3-
// By default tests with be compiled for `minJavaVersionForTests` version,
4-
// but in this case we would like to avoid this since we would like to run with ZULU8
5-
skipSettingTestJavaVersion = true
6-
}
7-
81
apply from: "$rootDir/gradle/java.gradle"
92
apply plugin: 'idea'
103

4+
tracerJava {
5+
addSourceSetFor(JavaVersion.VERSION_11) {
6+
// By default tests with be compiled for `minJavaVersionForTests` version,
7+
// but in this case we would like to avoid this since we would like to run with ZULU8
8+
applyForTestSources = false
9+
}
10+
}
11+
1112
testJvmConstraint {
1213
minJavaVersionForTests = JavaVersion.VERSION_11
1314
// Zulu has backported profiling support

dd-java-agent/agent-profiling/profiling-controller-oracle/build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
// Set properties before any plugins get loaded
2-
ext {
3-
// By default tests with be compiled for `minJavaVersionForTests` version,
4-
// but in this case we would like to avoid this since we would like to run with ORACLE8
5-
skipSettingTestJavaVersion = true
6-
}
7-
81
apply from: "$rootDir/gradle/java.gradle"
92

3+
tracerJava {
4+
addSourceSetFor(JavaVersion.VERSION_11) {
5+
// By default tests with be compiled for `minJavaVersionForTests` version,
6+
// but in this case we would like to avoid this since we would like to run with ZULU8
7+
applyForTestSources = false
8+
}
9+
}
10+
1011
minimumBranchCoverage = 0.5
1112
minimumInstructionCoverage = 0.7
1213

dd-java-agent/instrumentation/exception-profiling/build.gradle

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
ext {
2-
// By default tests with be compiled for `minJavaVersionForTests` version,
3-
// but in this case we would like to avoid this since we would like to run with ZULU8
4-
skipSettingTestJavaVersion = true
5-
}
6-
71
apply from: "$rootDir/gradle/java.gradle"
82
apply plugin: "idea"
93

104
tracerJava {
11-
addSourceSetFor(JavaVersion.VERSION_11)
5+
addSourceSetFor(JavaVersion.VERSION_11) {
6+
// By default tests with be compiled for `minJavaVersionForTests` version,
7+
// but in this case we would like to avoid this since we would like to run with ZULU8
8+
applyForTestSources = false
9+
}
1210
}
1311

1412
testJvmConstraint {

gradle/java_no_deps.gradle

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,62 @@ ext.configureCompiler = (AbstractCompile it, int toolchainVersion, JavaVersion c
1818
configureCompiler(it, toolchainVersion, compatibilityVersion, unsetReleaseFlagReason)
1919
} as Closure<Void>
2020

21+
@CompileStatic
22+
interface TracerSourceSetConfig {
23+
/**
24+
* Additional sourceSet output is added as `compileOnly` rather than `implementation`.
25+
*
26+
* This is useful to avoid some classes to be added to add classes to the wrog jar prefix.
27+
*/
28+
Property<Boolean> getCompileOnly()
29+
30+
/**
31+
* Whether to apply or not the JavaVersion to tests sources, default to `true`.
32+
*
33+
* In some cases we would like to avoid setting java version to for tests.
34+
* For example we would like to be able to run profiling tests with `ZULU8`,
35+
* but we cannot run it with other JDK8 implementations at the moment
36+
*/
37+
Property<Boolean> getApplyForTestSources()
38+
}
39+
2140
@CompileStatic
2241
class TracerJavaExtension {
2342
public static String NAME = "tracerJava";
43+
private ObjectFactory objects
2444
private Project project
2545

26-
Property<Boolean> skipSettingTestJavaVersion
27-
2846
TracerJavaExtension(ObjectFactory objects, ProviderFactory providers, Project project) {
2947
this.project = project
30-
this.skipSettingTestJavaVersion = objects.property(Boolean).convention(providers.provider { project.findProperty('skipSettingTestJavaVersion') as Boolean })
48+
this.objects = objects
49+
}
50+
51+
/**
52+
* Same as [addSourceSetFor] used for compatibility when used in kotlin scripts within `withGroovyBuilder`.
53+
*
54+
* Once moved to convention plugins this can go away.
55+
*
56+
* @param javaVersion The wanted java version for these source.
57+
* @param sourceSetOptions
58+
*/
59+
def addSourceSetFor(JavaVersion javaVersion, Map<String, Boolean> sourceSetOptions) {
60+
addSourceSetFor(javaVersion) {
61+
it.compileOnly.set(sourceSetOptions.getOrDefault('compileOnly', false))
62+
it.applyForTestSources.set(sourceSetOptions.getOrDefault('applyForTestSources', true))
63+
}
3164
}
3265

3366
/**
3467
* Adds the source set `src/main/javaXX`, configures its compilation and its test jvm constraints.
3568
*
3669
* @param javaVersion The wanted java version for these source.
37-
* @param compileOnly Whether the compilation output is added as compileOnly.
38-
* @return
70+
* @param sourceSetConfigurer Options for this source set.
3971
*/
40-
def addSourceSetFor(JavaVersion javaVersion, Boolean compileOnly = false) {
72+
def addSourceSetFor(JavaVersion javaVersion, Action<? super TracerSourceSetConfig> sourceSetConfigurer = null) {
73+
def sourceSetConfig = objects.newInstance(TracerSourceSetConfig)
74+
if (sourceSetConfigurer != null) {
75+
sourceSetConfigurer.execute(sourceSetConfig)
76+
}
4177
project.extensions.getByType(TestJvmConstraintsExtension).minJavaVersionForTests.set(javaVersion)
4278

4379
def version = javaVersion
@@ -54,9 +90,19 @@ class TracerJavaExtension {
5490
cl.call(it, version.majorVersion.toInteger().intValue(), version)
5591
}
5692

93+
if (sourceSetConfig.applyForTestSources.orElse(true)) {
94+
// configures all test tasks
95+
project.tasks.withType(JavaCompile).configureEach {
96+
if (it.name.toLowerCase().contains("test")) {
97+
it.sourceCompatibility = version
98+
it.targetCompatibility = version
99+
}
100+
}
101+
}
102+
57103
// "socket-utils" is only set to compileOnly because the implementation dependency incorrectly adds Java17 classes to all jar prefixes.
58104
// This causes the AgentJarIndex to search for other non-Java17 classes in the wrong prefix location and fail to resolve class names.
59-
if (compileOnly) {
105+
if (sourceSetConfig.compileOnly.orElse(false).get()) {
60106
project.dependencies.add("compileOnly", mainForJavaVersionSourceSet.output)
61107
} else {
62108
project.dependencies.add("compileOnly", project.files(mainForJavaVersionSourceSet.compileClasspath))
@@ -113,21 +159,6 @@ java {
113159

114160
if (project.hasProperty('minJavaVersionForTests') && project.findProperty('minJavaVersionForTests') != JavaVersion.VERSION_1_7) {
115161
tracerJavaExtension.addSourceSetFor(project.findProperty('minJavaVersionForTests'))
116-
117-
// TODO configure this via extension
118-
// If unset (hasProperty is false), or value is explicitly set to false (case not seen), then
119-
// sets the compiler to `minJavaVersionForTests` version
120-
// In some cases we would like to avoid setting java version to `minJavaVersionForTests`.
121-
// For example we would like to be able to run profiling tests with ZULU8, but we cannot run it with other JDK8 implementations at the moment
122-
def skipSettingTestJavaVersion = project.hasProperty('skipSettingTestJavaVersion') && project.findProperty('skipSettingTestJavaVersion')
123-
if (!skipSettingTestJavaVersion) {
124-
tasks.withType(JavaCompile).configureEach {
125-
if (it.name.toLowerCase().contains("test")) {
126-
sourceCompatibility = version
127-
targetCompatibility = version
128-
}
129-
}
130-
}
131162
}
132163

133164
tasks.named("jar", Jar) {

utils/socket-utils/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
apply(from = "$rootDir/gradle/java.gradle")
1111

1212
extensions.getByName("tracerJava").withGroovyBuilder {
13-
this.invokeMethod("addSourceSetFor", arrayOf(JavaVersion.VERSION_17, true))
13+
invokeMethod("addSourceSetFor", arrayOf(JavaVersion.VERSION_17, mapOf("compileOnly" to true)))
1414
}
1515

1616
dependencies {

0 commit comments

Comments
 (0)