Skip to content

Commit 7dac219

Browse files
committed
[gradle] extracted common code in root
1 parent ff0cdbe commit 7dac219

File tree

4 files changed

+128
-328
lines changed

4 files changed

+128
-328
lines changed

build.gradle.kts

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,130 @@
1-
allprojects {
1+
import org.gradle.api.attributes.LibraryElements.*
2+
import org.jetbrains.dokka.gradle.DokkaTask
3+
4+
plugins {
5+
java
6+
`java-library`
7+
kotlin("jvm") version "1.3.72"
8+
maven
9+
// id "org.jetbrains.kotlin.kapt" version "1.3.10"
10+
id("org.jetbrains.dokka") version "0.10.1"
11+
id("com.github.johnrengelman.shadow") version "5.2.0"
12+
}
13+
14+
subprojects {
15+
apply(plugin = "java")
16+
apply(plugin = "java-library")
17+
apply(plugin = "org.jetbrains.kotlin.jvm")
18+
apply(plugin = "maven")
19+
apply(plugin = "org.jetbrains.dokka")
20+
apply(plugin = "com.github.johnrengelman.shadow")
21+
22+
// version = "0.9-beta"
23+
24+
java {
25+
modularity.inferModulePath.set(true)
26+
}
27+
28+
dependencies {
29+
30+
implementation(kotlin("stdlib"))
31+
implementation(kotlin("stdlib-jdk8"))
32+
33+
attributesSchema.attribute(LIBRARY_ELEMENTS_ATTRIBUTE).compatibilityRules.add(ModularJarCompatibilityRule::class)
34+
components { withModule<ModularKotlinRule>(kotlin("stdlib")) }
35+
components { withModule<ModularKotlinRule>(kotlin("stdlib-jdk8")) }
36+
37+
listOf("runner-junit5", "assertions-core", "runner-console"/*, "property"*/).forEach {
38+
testImplementation("io.kotest:kotest-$it-jvm:${findProperty("kotestVersion")}")
39+
}
40+
}
41+
242
repositories {
343
mavenCentral()
444
jcenter()
545
maven { url = uri("https://jitpack.io") }
646
}
47+
48+
tasks {
49+
val dokka by getting(DokkaTask::class) {
50+
outputFormat = "html"
51+
outputDirectory = "$buildDir/dokka"
52+
}
53+
54+
compileKotlin {
55+
kotlinOptions {
56+
jvmTarget = "11"
57+
freeCompilerArgs = listOf("-XXLanguage:+InlineClasses", "-Xjvm-default=enable")
58+
}
59+
sourceCompatibility = "11"
60+
destinationDir = compileJava.get().destinationDir
61+
}
62+
jar { duplicatesStrategy = DuplicatesStrategy.EXCLUDE }
63+
64+
compileTestKotlin {
65+
kotlinOptions.jvmTarget = "11"
66+
sourceCompatibility = "11"
67+
}
68+
69+
withType<Test> { useJUnitPlatform() }
70+
}
71+
72+
val dokkaJar by tasks.creating(Jar::class) {
73+
group = JavaBasePlugin.DOCUMENTATION_GROUP
74+
description = "Assembles Kotlin docs with Dokka"
75+
archiveClassifier.set("javadoc")
76+
from(tasks.dokka)
77+
}
78+
79+
val sourceJar = task("sourceJar", Jar::class) {
80+
dependsOn(tasks["classes"])
81+
archiveClassifier.set("sources")
82+
from(sourceSets.main.get().allSource)
83+
}
84+
85+
artifacts {
86+
archives(sourceJar)
87+
archives(dokkaJar)
88+
}
89+
90+
// == Add access to the 'modular' variant of kotlin("stdlib"): Put this into a buildSrc plugin and reuse it in all your subprojects
91+
configurations.all {
92+
attributes.attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11)
93+
val n = name.toLowerCase()
94+
if (n.endsWith("compileclasspath") || n.endsWith("runtimeclasspath"))
95+
attributes.attribute(LIBRARY_ELEMENTS_ATTRIBUTE, objects.named("modular-jar"))
96+
if (n.endsWith("compile") || n.endsWith("runtime"))
97+
isCanBeConsumed = false
98+
}
99+
}
100+
101+
abstract class ModularJarCompatibilityRule : AttributeCompatibilityRule<LibraryElements> {
102+
override fun execute(details: CompatibilityCheckDetails<LibraryElements>): Unit = details.run {
103+
if (producerValue?.name == JAR && consumerValue?.name == "modular-jar")
104+
compatible()
105+
}
106+
}
107+
108+
abstract class ModularKotlinRule : ComponentMetadataRule {
109+
110+
@javax.inject.Inject
111+
abstract fun getObjects(): ObjectFactory
112+
113+
override fun execute(ctx: ComponentMetadataContext) {
114+
val id = ctx.details.id
115+
listOf("compile", "runtime").forEach { baseVariant ->
116+
ctx.details.addVariant("${baseVariant}Modular", baseVariant) {
117+
attributes {
118+
attribute(LIBRARY_ELEMENTS_ATTRIBUTE, getObjects().named("modular-jar"))
119+
}
120+
withFiles {
121+
removeAllFiles()
122+
addFile("${id.name}-${id.version}-modular.jar")
123+
}
124+
withDependencies {
125+
clear() // 'kotlin-stdlib-common' and 'annotations' are not modules and are also not needed
126+
}
127+
}
128+
}
129+
}
7130
}

uno-awt/build.gradle.kts

Lines changed: 3 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
1-
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
2-
import org.gradle.api.attributes.java.TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE
31
import org.gradle.internal.os.OperatingSystem.*
42

5-
plugins {
6-
java
7-
`java-library`
8-
kotlin("jvm") version "1.3.72"
9-
maven
10-
// id "org.jetbrains.kotlin.kapt" version "1.3.10"
11-
id("org.jetbrains.dokka") version "0.10.1"
12-
id("com.github.johnrengelman.shadow") version "5.2.0"
13-
}
143

154
val moduleName = "${group}.uno_awt"
165

176
dependencies {
187

198
implementation(project(":uno-core"))
209

21-
implementation(kotlin("stdlib"))
22-
implementation(kotlin("stdlib-jdk8"))
23-
implementation(kotlin("reflect"))
24-
2510
val kx = "com.github.kotlin-graphics"
2611
// implementation("$kx:kotlin-unsigned:${findProperty("unsignedVersion")}")
2712
implementation("$kx:kool:${findProperty("koolVersion")}")
@@ -39,104 +24,9 @@ dependencies {
3924
if (it != "-jawt")
4025
implementation("org.lwjgl:lwjgl$it:${findProperty("lwjglVersion")}:natives-$lwjglNatives")
4126
}
42-
43-
attributesSchema.attribute(LIBRARY_ELEMENTS_ATTRIBUTE).compatibilityRules.add(ModularJarCompatibilityRule::class)
44-
components { withModule<ModularKotlinRule>(kotlin("stdlib")) }
45-
components { withModule<ModularKotlinRule>(kotlin("stdlib-jdk8")) }
46-
47-
listOf("runner-junit5", "assertions-core", "runner-console"/*, "property"*/).forEach {
48-
testImplementation("io.kotest:kotest-$it-jvm:${findProperty("kotestVersion")}")
49-
}
50-
}
51-
52-
java {
53-
modularity.inferModulePath.set(true)
54-
}
55-
56-
tasks {
57-
val dokka by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
58-
outputFormat = "html"
59-
outputDirectory = "$buildDir/dokka"
60-
}
61-
62-
compileKotlin {
63-
kotlinOptions {
64-
jvmTarget = "11"
65-
freeCompilerArgs = listOf("-XXLanguage:+InlineClasses", "-Xjvm-default=enable")
66-
}
67-
sourceCompatibility = "11"
68-
destinationDir = compileJava.get().destinationDir
69-
}
70-
jar { duplicatesStrategy = DuplicatesStrategy.EXCLUDE }
71-
72-
compileTestKotlin {
73-
kotlinOptions.jvmTarget = "11"
74-
sourceCompatibility = "11"
75-
}
76-
compileJava {
77-
println(sourceSets.main.get().output.asPath)
78-
// 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'
79-
options.compilerArgs = listOf("--patch-module", "$moduleName=${sourceSets.main.get().output.asPath}")
80-
}
81-
82-
withType<Test> { useJUnitPlatform() }
83-
}
84-
85-
val dokkaJar by tasks.creating(Jar::class) {
86-
group = JavaBasePlugin.DOCUMENTATION_GROUP
87-
description = "Assembles Kotlin docs with Dokka"
88-
archiveClassifier.set("javadoc")
89-
from(tasks.dokka)
90-
}
91-
92-
val sourceJar = task("sourceJar", Jar::class) {
93-
dependsOn(tasks["classes"])
94-
archiveClassifier.set("sources")
95-
from(sourceSets.main.get().allSource)
96-
}
97-
98-
artifacts {
99-
archives(sourceJar)
100-
archives(dokkaJar)
10127
}
10228

103-
// == Add access to the 'modular' variant of kotlin("stdlib"): Put this into a buildSrc plugin and reuse it in all your subprojects
104-
configurations.all {
105-
attributes.attribute(TARGET_JVM_VERSION_ATTRIBUTE, 11)
106-
val n = name.toLowerCase()
107-
if (n.endsWith("compileclasspath") || n.endsWith("runtimeclasspath"))
108-
attributes.attribute(LIBRARY_ELEMENTS_ATTRIBUTE, objects.named("modular-jar"))
109-
if (n.endsWith("compile") || n.endsWith("runtime"))
110-
isCanBeConsumed = false
111-
}
112-
113-
abstract class ModularJarCompatibilityRule : AttributeCompatibilityRule<LibraryElements> {
114-
override fun execute(details: CompatibilityCheckDetails<LibraryElements>): Unit = details.run {
115-
if (producerValue?.name == LibraryElements.JAR && consumerValue?.name == "modular-jar")
116-
compatible()
117-
}
118-
}
119-
120-
abstract class ModularKotlinRule : ComponentMetadataRule {
121-
122-
@javax.inject.Inject
123-
abstract fun getObjects(): ObjectFactory
124-
125-
override fun execute(ctx: ComponentMetadataContext) {
126-
val id = ctx.details.id
127-
listOf("compile", "runtime").forEach { baseVariant ->
128-
ctx.details.addVariant("${baseVariant}Modular", baseVariant) {
129-
attributes {
130-
attribute(LIBRARY_ELEMENTS_ATTRIBUTE, getObjects().named("modular-jar"))
131-
}
132-
withFiles {
133-
removeAllFiles()
134-
addFile("${id.name}-${id.version}-modular.jar")
135-
}
136-
withDependencies {
137-
clear() // 'kotlin-stdlib-common' and 'annotations' are not modules and are also not needed
138-
}
139-
}
140-
}
141-
}
29+
tasks.compileJava {
30+
// 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'
31+
options.compilerArgs = listOf("--patch-module", "$moduleName=${sourceSets.main.get().output.asPath}")
14232
}

0 commit comments

Comments
 (0)