Skip to content

Commit ac40351

Browse files
GooolerCopilot
andcommitted
Prefer Class-File API on Java 24 or above
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent e71a2c1 commit ac40351

4 files changed

Lines changed: 557 additions & 1 deletion

File tree

build.gradle.kts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import org.gradle.api.plugins.JavaPlugin.API_ELEMENTS_CONFIGURATION_NAME
44
import org.gradle.api.plugins.JavaPlugin.JAVADOC_ELEMENTS_CONFIGURATION_NAME
55
import org.gradle.api.plugins.JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME
66
import org.gradle.api.plugins.JavaPlugin.SOURCES_ELEMENTS_CONFIGURATION_NAME
7+
import org.gradle.language.base.plugins.LifecycleBasePlugin.VERIFICATION_GROUP
78
import org.gradle.plugin.compatibility.compatibility
89
import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode
910
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
11+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions
1012
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
1113
import org.jetbrains.kotlin.gradle.dsl.abi.ExperimentalAbiValidation
1214

@@ -46,6 +48,8 @@ kotlin {
4648
}
4749
}
4850

51+
addMultiReleaseSourceSet(24)
52+
4953
lint {
5054
baseline = file("gradle/lint-baseline.xml")
5155
ignoreTestFixturesSources = true
@@ -240,6 +244,10 @@ buildConfig {
240244
}
241245
}
242246

247+
tasks.jar {
248+
manifest.attributes("Multi-Release" to true)
249+
}
250+
243251
tasks.pluginUnderTestMetadata { pluginClasspath.from(testPluginClasspath) }
244252

245253
tasks.check { dependsOn(tasks.withType<Test>()) }
@@ -254,3 +262,35 @@ tasks.clean {
254262
rootDir.resolve("site"),
255263
)
256264
}
265+
266+
fun addMultiReleaseSourceSet(version: Int) {
267+
kotlin.target.compilations.create("java${version}") {
268+
associateWith(kotlin.target.compilations.getByName("main"))
269+
compileTaskProvider {
270+
compilerOptions {
271+
this@compilerOptions as KotlinJvmCompilerOptions
272+
jvmTarget = JvmTarget.fromTarget(version.toString())
273+
freeCompilerArgs.add("-Xjdk-release=$version")
274+
}
275+
}
276+
compileJavaTaskProvider { options.release = version }
277+
278+
tasks.jar { from(output.allOutputs) { into("META-INF/versions/$version") } }
279+
280+
val versionedTest =
281+
tasks.register("testJava${version}", Test::class) {
282+
useJUnitPlatform()
283+
group = VERIFICATION_GROUP
284+
description = "Runs test suite using Java $version toolchain."
285+
val testSourceSet = sourceSets.test.get()
286+
testClassesDirs = testSourceSet.output.classesDirs
287+
val testCpWithoutMainOutput = testSourceSet.runtimeClasspath - sourceSets.main.get().output
288+
// Prefer MR classes on the classpath, so remove main output.
289+
classpath = files(tasks.jar.flatMap { it.archiveFile }) + testCpWithoutMainOutput
290+
javaLauncher = javaToolchains.launcherFor {
291+
languageVersion = JavaLanguageVersion.of(version)
292+
}
293+
}
294+
tasks.check { dependsOn(versionedTest) }
295+
}
296+
}

gradle.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# https://kotlinlang.org/docs/gradle-configure-project.html#dependency-on-the-standard-library
33
kotlin.stdlib.default.dependency=false
44

5+
# Keep associated Kotlin compilations from depending on archive tasks (e.g., jar), which can create circular task graphs in multi-release setups.
6+
# https://kotlinlang.org/docs/gradle-configure-project.html#disable-use-of-artifact-in-compilation-task
7+
# https://kotlinlang.org/docs/whatsnew2020.html#added-task-dependency-for-rare-cases-when-the-compile-task-lacks-one-on-an-artifact
8+
kotlin.build.archivesTaskOutputAsFriendModule=false
9+
510
org.gradle.caching=true
611
org.gradle.configuration-cache=true
712
org.gradle.configuration-cache.parallel=true

settings.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ pluginManagement {
1212
}
1313
}
1414

15-
plugins { id("com.gradle.develocity") version "4.5.0" }
15+
plugins {
16+
id("com.gradle.develocity") version "4.5.0"
17+
id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
18+
}
1619

1720
develocity {
1821
buildScan {

0 commit comments

Comments
 (0)