From 1fa7c7119b3b2144182c94684a9316e99ab85087 Mon Sep 17 00:00:00 2001 From: Vyacheslav Gerasimov Date: Thu, 22 Dec 2022 22:04:22 +0100 Subject: [PATCH] Build: Fix caching of some ShadowJar tasks in the project To be cacheable relocated shadow jars should not include manifest with build number. Instead, we repack relocated shadow jar in default jar task with proper manifest. --- buildSrc/src/main/kotlin/artifacts.kt | 26 ++++++++++ .../kotlinx-metadata/jvm/build.gradle.kts | 49 +++++++------------ .../kotlinx-metadata/klib/build.gradle.kts | 47 ++++++------------ plugins/jvm-abi-gen/build.gradle.kts | 20 +++----- 4 files changed, 65 insertions(+), 77 deletions(-) diff --git a/buildSrc/src/main/kotlin/artifacts.kt b/buildSrc/src/main/kotlin/artifacts.kt index 8452ef1fc00..062e216d956 100644 --- a/buildSrc/src/main/kotlin/artifacts.kt +++ b/buildSrc/src/main/kotlin/artifacts.kt @@ -109,6 +109,32 @@ fun Project.runtimeJar(body: Jar.() -> Unit = {}): TaskProvider { return jarTask } +fun Project.runtimeJarWithRelocation(body: ShadowJar.() -> Unit = {}): TaskProvider { + noDefaultJar() + + val shadowJarTask = tasks.register("shadowJar") { + archiveClassifier.set("shadow") + configurations = configurations + listOf(project.configurations["embedded"]) + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + body() + } + + val runtimeJarTask = tasks.register("runtimeJar") { + dependsOn(shadowJarTask) + from { + zipTree(shadowJarTask.get().outputs.files.singleFile) + } + setupPublicJar(project.extensions.getByType().archivesName.get()) + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + } + + project.addArtifact("archives", runtimeJarTask, runtimeJarTask) + project.addArtifact("runtimeElements", runtimeJarTask, runtimeJarTask) + project.addArtifact("apiElements", runtimeJarTask, runtimeJarTask) + + return runtimeJarTask +} + fun Project.runtimeJar(task: TaskProvider, body: ShadowJar.() -> Unit = {}): TaskProvider { noDefaultJar() diff --git a/libraries/kotlinx-metadata/jvm/build.gradle.kts b/libraries/kotlinx-metadata/jvm/build.gradle.kts index f275dd2e68d..72f9814e630 100644 --- a/libraries/kotlinx-metadata/jvm/build.gradle.kts +++ b/libraries/kotlinx-metadata/jvm/build.gradle.kts @@ -32,18 +32,17 @@ sourceSets { "test" { projectDefault() } } -val shadows by configurations.creating { - isTransitive = false -} -configurations.getByName("compileOnly").extendsFrom(shadows) -configurations.getByName("testApi").extendsFrom(shadows) +val embedded by configurations +embedded.isTransitive = false +configurations.getByName("compileOnly").extendsFrom(embedded) +configurations.getByName("testApi").extendsFrom(embedded) dependencies { api(kotlinStdlib()) - shadows(project(":kotlinx-metadata")) - shadows(project(":core:metadata")) - shadows(project(":core:metadata.jvm")) - shadows(protobufLite()) + embedded(project(":kotlinx-metadata")) + embedded(project(":core:metadata")) + embedded(project(":core:metadata.jvm")) + embedded(protobufLite()) testImplementation(project(":kotlin-test:kotlin-test-junit")) testImplementation(commonDependency("junit:junit")) testImplementation(commonDependency("org.jetbrains.intellij.deps:asm-all")) @@ -54,40 +53,26 @@ if (deployVersion != null) { publish() } -val shadowJarTask = runtimeJar(tasks.register("shadowJar")) { - callGroovy("manifestAttributes", manifest, project) - manifest.attributes["Implementation-Version"] = archiveVersion - +val runtimeJar = runtimeJarWithRelocation { from(mainSourceSet.output) exclude("**/*.proto") - configurations = listOf(shadows) relocate("org.jetbrains.kotlin", "kotlinx.metadata.internal") } -val test by tasks -test.dependsOn("shadowJar") - tasks.apiBuild { - inputJar.value(shadowJarTask.flatMap { it.archiveFile }) + inputJar.value(runtimeJar.flatMap { it.archiveFile }) } apiValidation { ignoredPackages.add("kotlinx.metadata.internal") - nonPublicMarkers.addAll(listOf( - "kotlinx.metadata.internal.IgnoreInApiDump", - "kotlinx.metadata.jvm.internal.IgnoreInApiDump" - )) + nonPublicMarkers.addAll( + listOf( + "kotlinx.metadata.internal.IgnoreInApiDump", + "kotlinx.metadata.jvm.internal.IgnoreInApiDump" + ) + ) } -sourcesJar { - for (dependency in shadows.dependencies) { - if (dependency is ProjectDependency) { - val javaPlugin = dependency.dependencyProject.convention.findPlugin(JavaPluginConvention::class.java) - if (javaPlugin != null) { - from(javaPlugin.sourceSets["main"].allSource) - } - } - } -} +sourcesJar() javadocJar() diff --git a/libraries/kotlinx-metadata/klib/build.gradle.kts b/libraries/kotlinx-metadata/klib/build.gradle.kts index a43e24433db..d710ffe0292 100644 --- a/libraries/kotlinx-metadata/klib/build.gradle.kts +++ b/libraries/kotlinx-metadata/klib/build.gradle.kts @@ -16,50 +16,35 @@ sourceSets { "main" { projectDefault() } } -val shadows by configurations.creating { - isTransitive = false -} - -configurations.getByName("compileOnly").extendsFrom(shadows) -configurations.getByName("testApi").extendsFrom(shadows) +val embedded by configurations +embedded.isTransitive = false +configurations.getByName("compileOnly").extendsFrom(embedded) +configurations.getByName("testApi").extendsFrom(embedded) dependencies { api(kotlinStdlib()) - shadows(project(":kotlinx-metadata")) - shadows(project(":core:compiler.common")) - shadows(project(":core:metadata")) - shadows(project(":core:deserialization")) - shadows(project(":core:deserialization.common")) - shadows(project(":compiler:serialization")) - shadows(project(":kotlin-util-klib-metadata")) - shadows(project(":kotlin-util-klib")) - shadows(project(":kotlin-util-io")) - shadows(protobufLite()) + embedded(project(":kotlinx-metadata")) + embedded(project(":core:compiler.common")) + embedded(project(":core:metadata")) + embedded(project(":core:deserialization")) + embedded(project(":core:deserialization.common")) + embedded(project(":compiler:serialization")) + embedded(project(":kotlin-util-klib-metadata")) + embedded(project(":kotlin-util-klib")) + embedded(project(":kotlin-util-io")) + embedded(protobufLite()) } if (deployVersion != null) { publish() } -runtimeJar(tasks.register("shadowJar")) { - callGroovy("manifestAttributes", manifest, project) - manifest.attributes["Implementation-Version"] = archiveVersion - +runtimeJarWithRelocation { from(mainSourceSet.output) exclude("**/*.proto") - configurations = listOf(shadows) relocate("org.jetbrains.kotlin", "kotlinx.metadata.internal") } -sourcesJar { - for (dependency in shadows.dependencies) { - if (dependency is ProjectDependency) { - val javaPlugin = dependency.dependencyProject.convention.findPlugin(JavaPluginConvention::class.java) - if (javaPlugin != null) { - from(javaPlugin.sourceSets["main"].allSource) - } - } - } -} +sourcesJar() javadocJar() diff --git a/plugins/jvm-abi-gen/build.gradle.kts b/plugins/jvm-abi-gen/build.gradle.kts index 43342611a5a..15c660c1c3e 100644 --- a/plugins/jvm-abi-gen/build.gradle.kts +++ b/plugins/jvm-abi-gen/build.gradle.kts @@ -12,11 +12,10 @@ sourceSets { "test" { projectDefault() } } -val shadows: Configuration by configurations.creating { - isTransitive = false -} -configurations.getByName("compileOnly").extendsFrom(shadows) -configurations.getByName("testApi").extendsFrom(shadows) +val embedded by configurations +embedded.isTransitive = false +configurations.getByName("compileOnly").extendsFrom(embedded) +configurations.getByName("testApi").extendsFrom(embedded) dependencies { // Should come before dependency on proguarded compiler because StringUtil methods are deleted from it @@ -36,7 +35,7 @@ dependencies { // Note that kotlinx-metadata-jvm already includes kotlinx-metadata, core:metadata, core:metadata.jvm, // and protobuf-lite, so we only need to include kotlinx-metadata-jvm in the shadow jar. compileOnly(project(":kotlinx-metadata")) - shadows(commonDependency("org.jetbrains.kotlinx:kotlinx-metadata-jvm")) + embedded(commonDependency("org.jetbrains.kotlinx:kotlinx-metadata-jvm")) compileOnly(intellijCore()) compileOnly(commonDependency("org.jetbrains.intellij.deps:asm-all")) @@ -50,14 +49,8 @@ optInToExperimentalCompilerApi() publish() -noDefaultJar() - -val shadowJar = runtimeJar(tasks.register("shadowJar")) { - callGroovy("manifestAttributes", manifest, project) - manifest.attributes["Implementation-Version"] = archiveVersion - +runtimeJarWithRelocation { from(mainSourceSet.output) - configurations = listOf(shadows) relocate("kotlinx.metadata", "org.jetbrains.kotlin.jvm.abi.kotlinx.metadata") mergeServiceFiles() // This is needed to relocate the services files for kotlinx.metadata } @@ -69,7 +62,6 @@ javadocJar() projectTest(parallel = true) { workingDir = rootDir dependsOn(":dist") - dependsOn(shadowJar) } testsJar()