forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
420 lines (379 loc) · 14.3 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import org.gradle.kotlin.dsl.support.serviceOf
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import plugins.configureDefaultPublishing
import plugins.configureKotlinPomAttributes
import groovy.util.Node
import groovy.util.NodeList
plugins {
`kotlin-multiplatform` apply false
base
`maven-publish`
signing
}
open class ComponentsFactoryAccess
@javax.inject.Inject
constructor(val factory: SoftwareComponentFactory)
val componentFactory = objects.newInstance<ComponentsFactoryAccess>().factory
val jvmApi by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("java-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
}
}
val jvmRuntime by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("java-runtime"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
}
extendsFrom(jvmApi)
}
val jsApiVariant by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.js)
}
}
val jsRuntimeVariant by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-runtime"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.js)
}
extendsFrom(jsApiVariant)
}
val wasmApiVariant by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.wasm)
}
}
val wasmRuntimeVariant by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-runtime"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.wasm)
}
extendsFrom(wasmApiVariant)
}
val nativeApiVariant by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.native)
}
}
val commonVariant by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.common)
}
}
fun Configuration.sourcesConsumingConfiguration() {
isCanBeConsumed = false
isCanBeResolved = true
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION))
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
}
}
val kotlinTestCommonSources by configurations.creating {
sourcesConsumingConfiguration()
}
val kotlinTestJvmSources by configurations.creating {
sourcesConsumingConfiguration()
}
dependencies {
jvmApi(project(":kotlin-stdlib"))
jsApiVariant("$group:kotlin-test-js:$version")
wasmApiVariant("$group:kotlin-test-wasm:$version")
commonVariant("$group:kotlin-test-common:$version")
commonVariant("$group:kotlin-test-annotations-common:$version")
kotlinTestCommonSources(project(":kotlin-test:kotlin-test-common"))
kotlinTestJvmSources(project(":kotlin-test:kotlin-test-jvm"))
}
artifacts {
val jvmJar = tasks.getByPath(":kotlin-test:kotlin-test-jvm:jar")
add(jvmApi.name, jvmJar)
add(jvmRuntime.name, jvmJar)
}
val combinedSourcesJar by tasks.registering(Jar::class) {
dependsOn(kotlinTestCommonSources)
dependsOn(kotlinTestJvmSources)
archiveClassifier.set("sources")
val archiveOperations = serviceOf<ArchiveOperations>()
into("common") {
from({ archiveOperations.zipTree(kotlinTestCommonSources.singleFile) }) {
exclude("META-INF/**")
}
}
into("jvm") {
from({ archiveOperations.zipTree(kotlinTestJvmSources.singleFile) }) {
exclude("META-INF/**")
}
}
}
val combinedJvmSourcesJar by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
}
artifacts {
add(combinedJvmSourcesJar.name, combinedSourcesJar)
}
val rootComponent = componentFactory.adhoc("root").apply {
addVariantsFromConfiguration(jvmApi) {
mapToMavenScope("compile")
}
addVariantsFromConfiguration(jvmRuntime) {
mapToMavenScope("runtime")
}
addVariantsFromConfiguration(jsApiVariant) { mapToOptional() }
addVariantsFromConfiguration(jsRuntimeVariant) { mapToOptional() }
addVariantsFromConfiguration(wasmApiVariant) { mapToOptional() }
addVariantsFromConfiguration(wasmRuntimeVariant) { mapToOptional() }
addVariantsFromConfiguration(nativeApiVariant) { mapToOptional() }
addVariantsFromConfiguration(commonVariant) { mapToOptional() }
}
val kotlinTestCapability = "$group:kotlin-test:$version" // add to variants with explicit capabilities when the default one is needed, too
val baseCapability = "$group:kotlin-test-framework:$version"
val implCapability = "$group:kotlin-test-framework-impl:$version"
val jvmTestFrameworks = listOf("junit", "junit5", "testng")
val frameworkCapabilities = mutableSetOf<String>()
jvmTestFrameworks.forEach { framework ->
val (apiVariant, runtimeVariant) = listOf("api", "runtime").map { usage ->
configurations.create("${framework}${usage.capitalize()}Variant") {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("java-$usage"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
}
outgoing.capability(baseCapability) // C0
outgoing.capability(
"$group:kotlin-test-framework-$framework:$version".also { frameworkCapabilities.add(it) }
) // C0
}
}
runtimeVariant.extendsFrom(apiVariant)
dependencies {
apiVariant("$group:kotlin-test-$framework:$version")
}
rootComponent.addVariantsFromConfiguration(apiVariant) { mapToOptional() }
rootComponent.addVariantsFromConfiguration(runtimeVariant) { mapToOptional() }
val (apiElements, runtimeElements) = listOf("api", "runtime").map { usage ->
configurations.create("${framework}${usage.capitalize()}") {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("java-$usage"))
}
outgoing.capability(implCapability) // CC
outgoing.capability("$group:kotlin-test-$framework:$version") // CC
}
}
runtimeElements.extendsFrom(apiElements)
dependencies {
apiElements("$group:kotlin-test:$version")
when(framework) {
"junit" -> {
apiElements("junit:junit:4.13.2")
}
"junit5" -> {
apiElements("org.junit.jupiter:junit-jupiter-api:5.6.3")
runtimeElements("org.junit.jupiter:junit-jupiter-engine:5.6.3")
}
"testng" -> {
apiElements("org.testng:testng:6.13.1")
}
}
}
artifacts {
val jar = tasks.getByPath(":kotlin-test:kotlin-test-$framework:jar")
add(apiElements.name, jar)
add(runtimeElements.name, jar)
}
componentFactory.adhoc(framework).apply {
addVariantsFromConfiguration(apiElements) {
mapToMavenScope("compile")
}
addVariantsFromConfiguration(runtimeElements) {
mapToMavenScope("runtime")
}
}.let { components.add(it) }
}
/**
* When a consumer's dependency requires a specific test framework (like with auto framework selection), their configurations requesting
* "common" artifacts (such as `*DependenciesMetadata` in MPP) should choose this variant anyway. Otherwise, choosing this variant
* (from a "pure", capability-less dependency on `kotlin-test` appearing transitively in the dependency graph) along with some
* capability-providing *platform* variant leads to incompatible variants being chosen together, causing dependency resolution errors,
* see KTIJ-6098
*/
commonVariant.apply {
frameworkCapabilities.forEach(outgoing::capability)
outgoing.capability(kotlinTestCapability)
}
val (jsApi, jsRuntime) = listOf("api", "runtime").map { usage ->
configurations.create("js${usage.capitalize()}") {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-$usage"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.js)
}
}
}
jsRuntime.extendsFrom(jsApi)
dependencies {
jsApi(project(":kotlin-stdlib-js"))
}
artifacts {
val jsJar = tasks.getByPath(":kotlin-test:kotlin-test-js:libraryJarWithIr")
add(jsApi.name, jsJar)
add(jsRuntime.name, jsJar)
}
val jsComponent = componentFactory.adhoc("js").apply {
addVariantsFromConfiguration(jsApi) {
mapToMavenScope("compile")
}
addVariantsFromConfiguration(jsRuntime) {
mapToMavenScope("runtime")
}
}
val (wasmApi, wasmRuntime) = listOf("api", "runtime").map { usage ->
configurations.create("wasm${usage.capitalize()}") {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-$usage"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.wasm)
}
}
}
wasmRuntime.extendsFrom(wasmApi)
dependencies {
wasmApi(project(":kotlin-stdlib-wasm"))
}
artifacts {
val wasmKlib = tasks.getByPath(":kotlin-test:kotlin-test-wasm:wasmJar")
add(wasmApi.name, wasmKlib) {
extension = "klib"
}
add(wasmRuntime.name, wasmKlib) {
extension = "klib"
}
}
val wasmComponent = componentFactory.adhoc("wasm").apply {
addVariantsFromConfiguration(wasmApi) {
mapToMavenScope("compile")
}
addVariantsFromConfiguration(wasmRuntime) {
mapToMavenScope("runtime")
}
}
val commonMetadata by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.common)
}
}
val annotationsMetadata by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.common)
}
}
dependencies {
commonMetadata(project(":kotlin-stdlib-common"))
annotationsMetadata(project(":kotlin-stdlib-common"))
}
artifacts {
add(commonMetadata.name, tasks.getByPath(":kotlin-test:kotlin-test-common:jar"))
add(annotationsMetadata.name, tasks.getByPath(":kotlin-test:kotlin-test-annotations-common:jar"))
}
val commonMetadataComponent = componentFactory.adhoc("common").apply {
addVariantsFromConfiguration(commonMetadata) {
mapToMavenScope("compile")
}
}
val annotationsMetadataComponent = componentFactory.adhoc("annotations-common").apply {
addVariantsFromConfiguration(annotationsMetadata) {
mapToMavenScope("compile")
}
}
val emptyJavadocJar by tasks.creating(Jar::class) {
archiveClassifier.set("javadoc")
}
configureDefaultPublishing()
publishing {
publications {
create("main", MavenPublication::class) {
from(rootComponent)
artifact(combinedSourcesJar)
// Remove all optional dependencies from the root pom
pom.withXml {
val dependenciesNode = (asNode().get("dependencies") as NodeList).filterIsInstance<Node>().single()
val optionalDependencies = (dependenciesNode.get("dependency") as NodeList).filterIsInstance<Node>().filter {
((it.get("optional") as NodeList).singleOrNull() as Node?)?.text() == "true"
}
optionalDependencies.forEach { dependenciesNode.remove(it) }
}
configureKotlinPomAttributes(project, "Kotlin Test Multiplatform library")
suppressAllPomMetadataWarnings()
}
jvmTestFrameworks.forEach { framework ->
create(framework, MavenPublication::class) {
artifactId = "kotlin-test-$framework"
from(components[framework])
artifact(tasks.getByPath(":kotlin-test:kotlin-test-$framework:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test Support for $framework")
suppressAllPomMetadataWarnings()
}
}
create("js", MavenPublication::class) {
artifactId = "kotlin-test-js"
from(jsComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-js:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test for JS")
}
create("wasm", MavenPublication::class) {
artifactId = "kotlin-test-wasm"
from(wasmComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-wasm:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test for WASM", packaging = "klib")
}
create("common", MavenPublication::class) {
artifactId = "kotlin-test-common"
from(commonMetadataComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-common:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test Common")
}
create("annotationsCommon", MavenPublication::class) {
artifactId = "kotlin-test-annotations-common"
from(annotationsMetadataComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-annotations-common:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test Common")
}
withType<MavenPublication> {
artifact(emptyJavadocJar)
}
}
}
tasks.withType<GenerateModuleMetadata> {
enabled = "common" !in (publication.get() as MavenPublication).artifactId
}