1+ plugins {
2+ kotlin(" jvm" ) version " 1.3.72"
3+ }
4+
5+ val moduleName = " com.github.kotlin_graphics.kotlin_unsigned"
6+ val kot = " org.jetbrains.kotlin:kotlin"
7+ val kotlintest_version = " 3.4.2"
8+
9+ repositories {
10+ mavenCentral()
11+ maven { url = uri(" https://jitpack.io" ) }
12+ }
13+
14+ dependencies {
15+ implementation(kotlin(" stdlib-jdk8" ))
16+ }
17+
18+ // java {
19+ // withJavadocJar()
20+ // withSourcesJar()
21+ // }
22+
23+ // task sourcesJar(type: Jar, dependsOn: classes) {
24+ // archiveClassifier = 'sources'
25+ // from sourceSets.main.allSource
26+ // }
27+ //
28+ // task javadocJar(type: Jar, dependsOn: javadoc) {
29+ // archiveClassifier = 'javadoc'
30+ // from javadoc.destinationDir
31+ // }
32+ //
33+ // artifacts {
34+ // archives sourcesJar
35+ // // archives javadocJar
36+ // }
37+ //
38+
39+ tasks.compileKotlin {
40+ // Enable Kotlin compilation to Java 8 class files with method parameter name metadata
41+ kotlinOptions {
42+ jvmTarget = " 11"
43+ freeCompilerArgs = listOf (" -XXLanguage:+InlineClasses" )
44+ // javaParameters = true
45+ }
46+ // As per https://stackoverflow.com/a/47669720
47+ // See also https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/9
48+ // destinationDir = compileJava.destinationDir
49+ }
50+
51+ tasks.compileTestKotlin {
52+ kotlinOptions {
53+ jvmTarget = " 11"
54+ // javaParameters = true
55+ }
56+ }
57+
58+ tasks.compileJava {
59+ // this is needed because we have a separate compile step in this example with the 'module-info.java' is in 'main/java' and the Kotlin code is in 'main/kotlin'
60+ options.compilerArgs = listOf (" --patch-module" , " $moduleName =${sourceSets.main.get().output.asPath} " )
61+ }
62+
63+ // compileJava {
64+ // // this is needed because we have a separate compile step in this example with the 'module-info.java' is in 'main/java' and the Kotlin code is in 'main/kotlin'
65+ // options.compilerArgs = ["--patch-module", "$moduleName=${sourceSets["main"].output.asPath}"]
66+ // // dependsOn(':compileKotlin')
67+ // // doFirst {
68+ // // options.compilerArgs = [
69+ // // '--module-path', classpath.asPath,
70+ // // '--patch-module', "$moduleName=${sourceSets["main"].output.asPath}"]
71+ // // classpath = files()
72+ // // }
73+ // }
74+ //
75+ // jar {
76+ // inputs.property("moduleName", moduleName)
77+ // // manifest.attributes('Automatic-Module-Name': moduleName)
78+ // // duplicatesStrategy = DuplicatesStrategy.EXCLUDE
79+ // }
80+ //
81+ // test.useJUnitPlatform()
82+ //
83+ // plugins.withType(JavaPlugin).configureEach {
84+ // java {
85+ // modularity.inferModulePath = true
86+ // }
87+ // }
88+
89+ // == Add access to the 'modular' variant of kotlin("stdlib"): Put this into a buildSrc plugin and reuse it in all your subprojects
90+ configurations.all {
91+ if (name.toLowerCase().endsWith(" compileclasspath" ) || name.toLowerCase().endsWith(" runtimeclasspath" )) {
92+ attributes.attribute(LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE , objects.named(" modular-jar" ))
93+ }
94+ if (name.toLowerCase().endsWith(" compile" ) || name.toLowerCase().endsWith(" runtime" )) {
95+ isCanBeConsumed = false
96+ }
97+ }
98+
99+ dependencies {
100+ attributesSchema.attribute(LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE ).compatibilityRules.add(ModularJarCompatibilityRule ::class )
101+ components { withModule<ModularKotlinRule >(kotlin(" stdlib" )) }
102+ }
103+
104+ abstract class ModularJarCompatibilityRule : AttributeCompatibilityRule <LibraryElements > {
105+ override fun execute (details : CompatibilityCheckDetails <LibraryElements >): Unit = details.run {
106+ if (producerValue?.name == LibraryElements .JAR && consumerValue?.name == " modular-jar" )
107+ compatible()
108+ }
109+ }
110+
111+ abstract class ModularKotlinRule : ComponentMetadataRule {
112+
113+ @javax.inject.Inject
114+ abstract fun getObjects (): ObjectFactory
115+
116+ override fun execute (ctx : ComponentMetadataContext ) {
117+ val id = ctx.details.id
118+ listOf (" compile" , " runtime" ).forEach { baseVariant ->
119+ ctx.details.addVariant(" ${baseVariant} Modular" , baseVariant) {
120+ attributes {
121+ attribute(LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE , getObjects().named(" modular-jar" ))
122+ }
123+ withFiles {
124+ removeAllFiles()
125+ addFile(" ${id.name} -${id.version} -modular.jar" )
126+ }
127+ withDependencies {
128+ clear() // 'kotlin-stdlib-common' and 'annotations' are not modules and are also not needed
129+ }
130+ }
131+ }
132+ }
133+ }
0 commit comments