@@ -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
2241class 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
114160if (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
133164tasks. named(" jar" , Jar ) {
0 commit comments