Build: Rework runtimeJar helper and drop redundant configuration

#KTI-559
This commit is contained in:
Vyacheslav Gerasimov
2021-07-21 17:00:57 +03:00
parent ab146bd6d4
commit f91d6958a8
37 changed files with 104 additions and 185 deletions
+20 -33
View File
@@ -1,5 +1,6 @@
@file:Suppress("unused") // usages in build scripts are not tracked properly
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.gradle.publish.PublishTask
import org.gradle.api.Project
import org.gradle.api.Task
@@ -8,8 +9,6 @@ import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.PublishArtifact
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.attributes.Bundling
import org.gradle.api.attributes.Category
import org.gradle.api.attributes.LibraryElements
import org.gradle.api.attributes.Usage
import org.gradle.api.component.AdhocComponentWithVariants
@@ -68,15 +67,9 @@ fun Project.noDefaultJar() {
}
}
fun Project.runtimeJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> = runtimeJar(getOrCreateTask("jar", body)) { }
fun <T : Jar> Project.runtimeJar(task: TaskProvider<T>, body: T.() -> Unit = {}): TaskProvider<T> {
tasks.named<Jar>("jar").configure {
removeArtifacts(configurations.getOrCreate("archives"), this)
}
task.configure {
fun Project.runtimeJar(body: Jar.() -> Unit = {}): TaskProvider<out Jar> {
val jarTask = tasks.named<Jar>("jar")
jarTask.configure {
configurations.findByName("embedded")?.let { embedded ->
dependsOn(embedded)
from {
@@ -88,29 +81,23 @@ fun <T : Jar> Project.runtimeJar(task: TaskProvider<T>, body: T.() -> Unit = {})
body()
}
return jarTask
}
fun Project.runtimeJar(task: TaskProvider<ShadowJar>, body: ShadowJar.() -> Unit = {}): TaskProvider<out Jar> {
noDefaultJar()
task.configure {
configurations = configurations + listOf(project.configurations["embedded"])
setupPublicJar(project.the<BasePluginConvention>().archivesBaseName)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
body()
}
project.addArtifact("archives", task, task)
project.addArtifact("runtimeJar", task, task)
project.configurations.findByName("runtime")?.let {
project.addArtifact(it.name, task, task)
}
val runtimeJar = configurations.maybeCreate("runtimeJar").apply {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
}
}
configurePublishedComponent {
withVariantsFromConfiguration(configurations[RUNTIME_ELEMENTS_CONFIGURATION_NAME]) { skip() }
addVariantsFromConfiguration(runtimeJar) { }
}
(components.findByName("java") as AdhocComponentWithVariants?)?.addVariantsFromConfiguration(runtimeJar) { }
project.addArtifact("runtimeElements", task, task)
project.addArtifact("apiElements", task, task)
return task
}
-2
View File
@@ -108,8 +108,6 @@ fun Project.kotlinBuiltins(forJvm: Boolean): Any =
else dependencies.project(":core:builtins", configuration = "runtimeElementsJvm".takeIf { forJvm })
fun DependencyHandler.projectTests(name: String): ProjectDependency = project(name, configuration = "tests-jar")
fun DependencyHandler.projectRuntimeJar(name: String): ProjectDependency = project(name, configuration = "runtimeJar")
fun DependencyHandler.projectArchives(name: String): ProjectDependency = project(name, configuration = "archives")
enum class JpsDepScope {
COMPILE, TEST, RUNTIME, PROVIDED
+19 -10
View File
@@ -4,12 +4,13 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.Project
import org.gradle.api.artifacts.DependencySubstitution
import org.gradle.api.artifacts.component.ProjectComponentSelector
import org.gradle.api.attributes.LibraryElements
import org.gradle.api.attributes.Usage
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.tasks.TaskProvider
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.project
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.register
import java.io.File
@@ -69,10 +70,18 @@ private fun ShadowJar.configureEmbeddableCompilerRelocation(withJavaxInject: Boo
}
}
private fun Project.compilerShadowJar(taskName: String, body: ShadowJar.() -> Unit): TaskProvider<out ShadowJar> {
private fun Project.compilerShadowJar(taskName: String, body: ShadowJar.() -> Unit): TaskProvider<ShadowJar> {
val compilerJar = configurations.getOrCreate("compilerJar")
dependencies.add(compilerJar.name, dependencies.project(":kotlin-compiler", configuration = "runtimeJar"))
val compilerJar = configurations.getOrCreate("compilerJar").apply {
isCanBeConsumed = false
isCanBeResolved = true
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
}
}
dependencies.add(compilerJar.name, dependencies.project(":kotlin-compiler")) { isTransitive = false }
return tasks.register<ShadowJar>(taskName) {
destinationDirectory.set(project.file(File(buildDir, "libs")))
@@ -100,7 +109,7 @@ fun Project.configureShadowJarSubstitutionInCompileClasspath() {
}
}
fun Project.embeddableCompiler(taskName: String = "embeddable", body: ShadowJar.() -> Unit = {}): TaskProvider<out ShadowJar> =
fun Project.embeddableCompiler(taskName: String = "embeddable", body: ShadowJar.() -> Unit = {}): TaskProvider<ShadowJar> =
compilerShadowJar(taskName) {
configureEmbeddableCompilerRelocation()
body()
@@ -141,8 +150,8 @@ fun Project.embeddableCompilerDummyForDependenciesRewriting(
}
fun Project.rewriteDepsToShadedJar(
originalJarTask: TaskProvider<out Jar>, shadowJarTask: TaskProvider<out Jar>, body: Jar.() -> Unit = {}
): TaskProvider<out Jar> {
originalJarTask: TaskProvider<out Jar>, shadowJarTask: TaskProvider<ShadowJar>, body: Jar.() -> Unit = {}
): TaskProvider<ShadowJar> {
originalJarTask.configure {
archiveClassifier.set("original")
}
@@ -154,7 +163,7 @@ fun Project.rewriteDepsToShadedJar(
// When Gradle traverses the inputs, reject the shaded compiler JAR,
// which leads to the content of that JAR being excluded as well:
val compilerDummyJarFile = project.provider { configurations.getByName("compilerDummyJar").singleFile }
val compilerDummyJarFile = project.provider { project.configurations.getByName("compilerDummyJar").singleFile }
exclude { it.file == compilerDummyJarFile.get() }
archiveClassifier.set("original")
@@ -163,8 +172,8 @@ fun Project.rewriteDepsToShadedJar(
return shadowJarTask
}
fun Project.rewriteDepsToShadedCompiler(originalJarTask: TaskProvider<out Jar>, body: Jar.() -> Unit = {}): TaskProvider<out Jar> =
fun Project.rewriteDepsToShadedCompiler(originalJarTask: TaskProvider<out Jar>, body: Jar.() -> Unit = {}): TaskProvider<ShadowJar> =
rewriteDepsToShadedJar(originalJarTask, embeddableCompilerDummyForDependenciesRewriting(), body)
fun Project.rewriteDefaultJarDepsToShadedCompiler(body: Jar.() -> Unit = {}): TaskProvider<out Jar> =
fun Project.rewriteDefaultJarDepsToShadedCompiler(body: Jar.() -> Unit = {}): TaskProvider<ShadowJar> =
rewriteDepsToShadedJar(tasks.named<Jar>("jar"), embeddableCompilerDummyForDependenciesRewriting(), body)