Skip to content

Commit 7fe6780

Browse files
committed
chore: MultiReleaseJar utils
1 parent f48e992 commit 7fe6780

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package tasks
2+
3+
import java.util.*
4+
import org.gradle.api.Project
5+
import org.gradle.api.file.FileCollection
6+
import org.gradle.api.tasks.SourceSetContainer
7+
import org.gradle.api.tasks.compile.JavaCompile
8+
import org.gradle.jvm.toolchain.JavaLanguageVersion
9+
import org.gradle.jvm.toolchain.JavaToolchainService
10+
import org.gradle.kotlin.dsl.get
11+
import org.gradle.process.CommandLineArgumentProvider
12+
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
13+
14+
private val Project.sourceSets: SourceSetContainer
15+
get() = this.extensions.getByName("sourceSets") as SourceSetContainer
16+
17+
private val Project.javaToolchains: JavaToolchainService
18+
get() = this.extensions.getByName("javaToolchains") as JavaToolchainService
19+
20+
/**
21+
* Setup tasks to compile `module-info.java` file for a module named [moduleName] from a source set
22+
* with name [sourceSetName] using Java toolchain with version [toolchainVersion].
23+
*
24+
* It is assumed that source set with [sourceSetName] only extends main JVM source set.
25+
* [parentCompilation] represent a compilation corresponding to such a main source set.
26+
*
27+
* For more details, check this [kotlinx-io](https://github.com/Kotlin/kotlinx-io/pull/406/files) PR
28+
*/
29+
public fun Project.configureJava9ModuleInfoCompilation(
30+
sourceSetName: String,
31+
toolchainVersion: JavaLanguageVersion,
32+
parentCompilation: KotlinJvmCompilation,
33+
moduleName: String
34+
) {
35+
val moduleOutputs = listOf(parentCompilation.output.allOutputs)
36+
val compileClasspathConfiguration =
37+
parentCompilation.configurations.compileDependencyConfiguration
38+
val sourceSetNameCapitalized =
39+
sourceSetName.replaceFirstChar { it.titlecase(Locale.getDefault()) }
40+
val javaCompileClasspath = configurations["${sourceSetName}CompileClasspath"]
41+
javaCompileClasspath.extendsFrom(compileClasspathConfiguration)
42+
43+
tasks.named("compile${sourceSetNameCapitalized}Java", JavaCompile::class.java) {
44+
dependsOn(moduleOutputs)
45+
46+
targetCompatibility = "9"
47+
sourceCompatibility = "9"
48+
49+
javaCompiler.set(javaToolchains.compilerFor { languageVersion.set(toolchainVersion) })
50+
51+
val javaSourceSet = sourceSets[sourceSetName].java
52+
destinationDirectory.set(
53+
javaSourceSet.destinationDirectory.asFile.get().resolve("META-INF/versions/9"))
54+
options.sourcepath = files(javaSourceSet.srcDirs)
55+
val moduleFiles = objects.fileCollection().from(moduleOutputs)
56+
val modulePath = javaCompileClasspath.filter { it !in moduleFiles.files }
57+
dependsOn(modulePath)
58+
classpath = objects.fileCollection().from()
59+
options.compilerArgumentProviders.add(
60+
JigsawArgumentsProvider(moduleName, moduleFiles, modulePath))
61+
}
62+
}
63+
64+
private class JigsawArgumentsProvider(
65+
private val moduleName: String,
66+
private val moduleFiles: FileCollection,
67+
private val modulePath: FileCollection
68+
) : CommandLineArgumentProvider {
69+
override fun asArguments(): Iterable<String> =
70+
listOf(
71+
"--module-path",
72+
modulePath.asPath,
73+
"--patch-module",
74+
"$moduleName=${moduleFiles.asPath}",
75+
"--release",
76+
"9")
77+
}

0 commit comments

Comments
 (0)