@@ -11,6 +11,11 @@ apply from: "$rootDir/gradle/spotbugs.gradle"
1111apply from : " $rootDir /gradle/repositories.gradle"
1212apply 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
1621class 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}
2770def 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
71113if (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
314334ext. configureGroovyCompiler = (int toolchainVersion, String .. . taskNames) -> {
315335 taskNames. each { taskName ->
0 commit comments