[build] Add a more concise way to add and shadow dependencies for JVM-only projects

This commit is contained in:
Him188 2022-11-27 19:56:44 +00:00
parent 6da947430e
commit 146d737b42
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
2 changed files with 43 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import org.gradle.api.artifacts.DependencySubstitutions
import org.gradle.api.artifacts.ResolutionStrategy
import org.gradle.api.artifacts.component.ComponentSelector
import org.gradle.api.plugins.ExtensionAware
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import java.util.*
@ -117,3 +118,9 @@ val Project.kotlinMpp
(this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
}.getOrNull()
val Project.kotlinJvm
get() = runCatching {
(this as ExtensionAware).extensions.getByName("kotlin") as? KotlinJvmProjectExtension
}.getOrNull()

View File

@ -13,8 +13,11 @@ import com.google.gson.GsonBuilder
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.publish.tasks.GenerateModuleMetadata
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.DependencyHandlerScope
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.get
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
@ -215,7 +218,33 @@ private fun Sequence<Task>.dependsOn(
return forEach { it.dependsOn(tasks) }
}
private fun Project.registerRegularShadowTask(target: KotlinTarget, mapTaskNameForMultipleTargets: Boolean): ShadowJar {
/**
* 添加 `implementation` `shadow`
*/
fun DependencyHandlerScope.shadowImplementation(dependencyNotation: Any) {
"implementation"(dependencyNotation)
"shadow"(dependencyNotation)
}
fun Project.registerRegularShadowTaskForJvmProject(
configurations: List<Configuration> = listOfNotNull(
project.configurations.findByName("runtimeClasspath"),
project.configurations.findByName("${kotlinJvm!!.target.name}RuntimeClasspath"),
project.configurations.findByName("runtime")
)
): ShadowJar {
return project.registerRegularShadowTask(kotlinJvm!!.target, mapTaskNameForMultipleTargets = false, configurations)
}
fun Project.registerRegularShadowTask(
target: KotlinTarget,
mapTaskNameForMultipleTargets: Boolean,
configurations: List<Configuration> = listOfNotNull(
project.configurations.findByName("runtimeClasspath"),
project.configurations.findByName("${target.targetName}RuntimeClasspath"),
project.configurations.findByName("runtime")
),
): ShadowJar {
return tasks.create(
if (mapTaskNameForMultipleTargets) "shadow${target.targetName.capitalize()}Jar" else "shadowJar",
ShadowJar::class
@ -223,18 +252,17 @@ private fun Project.registerRegularShadowTask(target: KotlinTarget, mapTaskNameF
group = "mirai"
archiveClassifier.set("all")
(tasks.findByName("jar") as? Jar)?.let {
manifest.inheritFrom(it.manifest)
}
val compilation = target.compilations["main"]
dependsOn(compilation.compileKotlinTask)
from(compilation.output)
// components.findByName("java")?.let { from(it) }
project.sourceSets.findByName("main")?.output?.let { from(it) } // for JVM projects
configurations =
listOfNotNull(
project.configurations.findByName("runtimeClasspath"),
project.configurations.findByName("${target.targetName}RuntimeClasspath"),
project.configurations.findByName("runtime")
)
this.configurations = configurations
// Relocate packages
afterEvaluate {
@ -249,6 +277,7 @@ private fun Project.registerRegularShadowTask(target: KotlinTarget, mapTaskNameF
exclude { file ->
file.name.endsWith(".sf", ignoreCase = true)
}
exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "module-info.class")
}
}