Build: Publish Kotlin artifacts with maven-publish plugin
This commit is contained in:
Generated
+1
@@ -11,6 +11,7 @@
|
|||||||
<w>protobuf</w>
|
<w>protobuf</w>
|
||||||
<w>redirector</w>
|
<w>redirector</w>
|
||||||
<w>remapper</w>
|
<w>remapper</w>
|
||||||
|
<w>sonatype</w>
|
||||||
<w>unpresent</w>
|
<w>unpresent</w>
|
||||||
</words>
|
</words>
|
||||||
</dictionary>
|
</dictionary>
|
||||||
|
|||||||
@@ -8,15 +8,27 @@ import org.gradle.api.artifacts.Configuration
|
|||||||
import org.gradle.api.artifacts.ConfigurationContainer
|
import org.gradle.api.artifacts.ConfigurationContainer
|
||||||
import org.gradle.api.artifacts.PublishArtifact
|
import org.gradle.api.artifacts.PublishArtifact
|
||||||
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
|
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
|
||||||
|
import org.gradle.api.attributes.Bundling
|
||||||
|
import org.gradle.api.attributes.Category
|
||||||
|
import org.gradle.api.attributes.Usage
|
||||||
|
import org.gradle.api.component.AdhocComponentWithVariants
|
||||||
import org.gradle.api.file.DuplicatesStrategy
|
import org.gradle.api.file.DuplicatesStrategy
|
||||||
import org.gradle.api.plugins.BasePluginConvention
|
import org.gradle.api.plugins.BasePluginConvention
|
||||||
import org.gradle.api.plugins.JavaPlugin
|
import org.gradle.api.plugins.JavaPlugin
|
||||||
|
import org.gradle.api.plugins.JavaPluginExtension
|
||||||
|
import org.gradle.api.publish.PublishingExtension
|
||||||
|
import org.gradle.api.publish.maven.MavenPublication
|
||||||
|
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
|
||||||
|
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
|
||||||
|
import org.gradle.api.publish.tasks.GenerateModuleMetadata
|
||||||
import org.gradle.api.tasks.TaskProvider
|
import org.gradle.api.tasks.TaskProvider
|
||||||
import org.gradle.api.tasks.Upload
|
import org.gradle.api.tasks.Upload
|
||||||
import org.gradle.api.tasks.javadoc.Javadoc
|
import org.gradle.api.tasks.javadoc.Javadoc
|
||||||
import org.gradle.jvm.tasks.Jar
|
import org.gradle.jvm.tasks.Jar
|
||||||
import org.gradle.api.artifacts.dsl.DependencyHandler
|
import org.gradle.api.artifacts.dsl.DependencyHandler
|
||||||
import org.gradle.kotlin.dsl.*
|
import org.gradle.kotlin.dsl.*
|
||||||
|
import org.gradle.plugins.signing.SigningExtension
|
||||||
|
import org.gradle.plugins.signing.SigningPlugin
|
||||||
|
|
||||||
|
|
||||||
private const val MAGIC_DO_NOT_CHANGE_TEST_JAR_TASK_NAME = "testJar"
|
private const val MAGIC_DO_NOT_CHANGE_TEST_JAR_TASK_NAME = "testJar"
|
||||||
@@ -29,7 +41,7 @@ fun Project.testsJar(body: Jar.() -> Unit = {}): Jar {
|
|||||||
pluginManager.withPlugin("java") {
|
pluginManager.withPlugin("java") {
|
||||||
from(testSourceSet.output)
|
from(testSourceSet.output)
|
||||||
}
|
}
|
||||||
classifier = "tests"
|
archiveClassifier.set("tests")
|
||||||
body()
|
body()
|
||||||
project.addArtifact(testsJarCfg, this, this)
|
project.addArtifact(testsJarCfg, this, this)
|
||||||
}
|
}
|
||||||
@@ -71,12 +83,14 @@ fun <T : Task> Project.runtimeJarArtifactBy(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Project.runtimeJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> = runtimeJar(getOrCreateTask("jar", body), { })
|
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> {
|
fun <T : Jar> Project.runtimeJar(task: TaskProvider<T>, body: T.() -> Unit = {}): TaskProvider<T> {
|
||||||
|
|
||||||
tasks.named<Jar>("jar").configure {
|
tasks.named<Jar>("jar").configure {
|
||||||
removeArtifacts(configurations.getOrCreate("archives"), this)
|
removeArtifacts(configurations.getOrCreate("archives"), this)
|
||||||
}
|
}
|
||||||
|
|
||||||
task.configure {
|
task.configure {
|
||||||
configurations.findByName("embedded")?.let { embedded ->
|
configurations.findByName("embedded")?.let { embedded ->
|
||||||
dependsOn(embedded)
|
dependsOn(embedded)
|
||||||
@@ -85,15 +99,35 @@ fun <T : Jar> Project.runtimeJar(task: TaskProvider<T>, body: T.() -> Unit = {})
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
setupPublicJar(project.the<BasePluginConvention>().archivesBaseName)
|
setupPublicJar(project.the<BasePluginConvention>().archivesBaseName)
|
||||||
setDuplicatesStrategy(DuplicatesStrategy.EXCLUDE)
|
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||||
body()
|
body()
|
||||||
}
|
}
|
||||||
|
|
||||||
project.runtimeJarArtifactBy(task, task)
|
project.runtimeJarArtifactBy(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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val javaComponent = components.findByName("java") as AdhocComponentWithVariants
|
||||||
|
javaComponent.withVariantsFromConfiguration(configurations["runtimeElements"]) { skip() }
|
||||||
|
javaComponent.addVariantsFromConfiguration(runtimeJar) { }
|
||||||
|
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Project.sourcesJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
fun Project.sourcesJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
||||||
val task = tasks.register<Jar>("sourcesJar") {
|
configure<JavaPluginExtension> {
|
||||||
|
withSourcesJar()
|
||||||
|
}
|
||||||
|
|
||||||
|
val sourcesJar = getOrCreateTask<Jar>("sourcesJar") {
|
||||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||||
archiveClassifier.set("sources")
|
archiveClassifier.set("sources")
|
||||||
|
|
||||||
@@ -117,13 +151,17 @@ fun Project.sourcesJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
|||||||
body()
|
body()
|
||||||
}
|
}
|
||||||
|
|
||||||
addArtifact("archives", task)
|
addArtifact("archives", sourcesJar)
|
||||||
addArtifact("sources", task)
|
addArtifact("sources", sourcesJar)
|
||||||
|
|
||||||
return task
|
return sourcesJar
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Project.javadocJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
fun Project.javadocJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
||||||
|
configure<JavaPluginExtension> {
|
||||||
|
withJavadocJar()
|
||||||
|
}
|
||||||
|
|
||||||
val javadocTask = getOrCreateTask<Jar>("javadocJar") {
|
val javadocTask = getOrCreateTask<Jar>("javadocJar") {
|
||||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||||
archiveClassifier.set("javadoc")
|
archiveClassifier.set("javadoc")
|
||||||
@@ -145,7 +183,119 @@ fun Project.standardPublicJars() {
|
|||||||
javadocJar()
|
javadocJar()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Project.publish(body: Upload.() -> Unit = {}): Upload {
|
fun Project.publish(moduleMetadata: Boolean = false) {
|
||||||
|
apply<MavenPublishPlugin>()
|
||||||
|
apply<SigningPlugin>()
|
||||||
|
|
||||||
|
if (!moduleMetadata) {
|
||||||
|
tasks.withType<GenerateModuleMetadata> {
|
||||||
|
enabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val javaComponent = components.findByName("java") as AdhocComponentWithVariants?
|
||||||
|
if (javaComponent != null) {
|
||||||
|
val runtimeElements by configurations
|
||||||
|
val apiElements by configurations
|
||||||
|
|
||||||
|
val publishedRuntime = configurations.maybeCreate("publishedRuntime").apply {
|
||||||
|
attributes {
|
||||||
|
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
|
||||||
|
}
|
||||||
|
extendsFrom(runtimeElements)
|
||||||
|
}
|
||||||
|
|
||||||
|
val publishedCompile = configurations.maybeCreate("publishedCompile").apply {
|
||||||
|
attributes {
|
||||||
|
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_API))
|
||||||
|
}
|
||||||
|
extendsFrom(apiElements)
|
||||||
|
}
|
||||||
|
|
||||||
|
javaComponent.withVariantsFromConfiguration(apiElements) { skip() }
|
||||||
|
|
||||||
|
javaComponent.addVariantsFromConfiguration(publishedCompile) { mapToMavenScope("compile") }
|
||||||
|
javaComponent.addVariantsFromConfiguration(publishedRuntime) { mapToMavenScope("runtime") }
|
||||||
|
}
|
||||||
|
|
||||||
|
configure<PublishingExtension> {
|
||||||
|
publications {
|
||||||
|
create<MavenPublication>("Main") {
|
||||||
|
if (javaComponent != null) {
|
||||||
|
from(javaComponent)
|
||||||
|
} else {
|
||||||
|
artifact(tasks["jar"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pom {
|
||||||
|
packaging = "jar"
|
||||||
|
description.set(project.description)
|
||||||
|
url.set("https://kotlinlang.org/")
|
||||||
|
licenses {
|
||||||
|
license {
|
||||||
|
name.set("The Apache License, Version 2.0")
|
||||||
|
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scm {
|
||||||
|
url.set("https://github.com/JetBrains/kotlin")
|
||||||
|
connection.set("scm:git:https://github.com/JetBrains/kotlin.git")
|
||||||
|
developerConnection.set("scm:git:https://github.com/JetBrains/kotlin.git")
|
||||||
|
}
|
||||||
|
developers {
|
||||||
|
developer {
|
||||||
|
name.set("Kotlin Team")
|
||||||
|
organization.set("JetBrains")
|
||||||
|
organizationUrl.set("https://www.jetbrains.com")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
maven {
|
||||||
|
name = "Maven"
|
||||||
|
url = file("${project.rootDir}/build/repo").toURI()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
configure<SigningExtension> {
|
||||||
|
setRequired(provider {
|
||||||
|
project.findProperty("signingRequired")?.toString()?.toBoolean()
|
||||||
|
?: project.property("isSonatypeRelease") as Boolean
|
||||||
|
})
|
||||||
|
|
||||||
|
sign(extensions.getByType<PublishingExtension>().publications["Main"])
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.register("install") {
|
||||||
|
dependsOn(tasks.named("publishToMavenLocal"))
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named<PublishToMavenRepository>("publishMainPublicationToMavenRepository") {
|
||||||
|
dependsOn(project.rootProject.tasks.named("preparePublication"))
|
||||||
|
doFirst {
|
||||||
|
val preparePublication = project.rootProject.tasks.named("preparePublication").get()
|
||||||
|
val username: String? by preparePublication.extra
|
||||||
|
val password: String? by preparePublication.extra
|
||||||
|
val repoUrl: String by preparePublication.extra
|
||||||
|
|
||||||
|
repository.apply {
|
||||||
|
url = uri(repoUrl)
|
||||||
|
if (url.scheme != "file" && username != null && password != null) {
|
||||||
|
credentials {
|
||||||
|
this.username = username
|
||||||
|
this.password = password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Project.publishWithLegacyMavenPlugin(body: Upload.() -> Unit = {}): Upload {
|
||||||
apply<plugins.PublishedKotlinModule>()
|
apply<plugins.PublishedKotlinModule>()
|
||||||
|
|
||||||
if (artifactsRemovedDiagnosticFlag) {
|
if (artifactsRemovedDiagnosticFlag) {
|
||||||
@@ -241,8 +391,8 @@ fun ConfigurationContainer.getOrCreate(name: String): Configuration = findByName
|
|||||||
|
|
||||||
fun Jar.setupPublicJar(baseName: String, classifier: String = "") {
|
fun Jar.setupPublicJar(baseName: String, classifier: String = "") {
|
||||||
val buildNumber = project.rootProject.extra["buildNumber"] as String
|
val buildNumber = project.rootProject.extra["buildNumber"] as String
|
||||||
this.baseName = baseName
|
this.archiveBaseName.set(baseName)
|
||||||
this.classifier = classifier
|
this.archiveClassifier.set(classifier)
|
||||||
manifest.attributes.apply {
|
manifest.attributes.apply {
|
||||||
put("Implementation-Vendor", "JetBrains")
|
put("Implementation-Vendor", "JetBrains")
|
||||||
put("Implementation-Title", baseName)
|
put("Implementation-Title", baseName)
|
||||||
@@ -261,9 +411,13 @@ fun Project.addArtifact(configuration: Configuration, task: Task, artifactRef: A
|
|||||||
fun Project.addArtifact(configurationName: String, task: Task, artifactRef: Any, body: ConfigurablePublishArtifact.() -> Unit = {}) =
|
fun Project.addArtifact(configurationName: String, task: Task, artifactRef: Any, body: ConfigurablePublishArtifact.() -> Unit = {}) =
|
||||||
addArtifact(configurations.getOrCreate(configurationName), task, artifactRef, body)
|
addArtifact(configurations.getOrCreate(configurationName), task, artifactRef, body)
|
||||||
|
|
||||||
fun <T : Task> Project.addArtifact(configurationName: String, task: TaskProvider<T>, body: ConfigurablePublishArtifact.() -> Unit = {}) {
|
fun <T : Task> Project.addArtifact(
|
||||||
|
configurationName: String,
|
||||||
|
task: TaskProvider<T>,
|
||||||
|
body: ConfigurablePublishArtifact.() -> Unit = {}
|
||||||
|
): PublishArtifact {
|
||||||
configurations.maybeCreate(configurationName)
|
configurations.maybeCreate(configurationName)
|
||||||
artifacts.add(configurationName, task, body)
|
return artifacts.add(configurationName, task, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Task> Project.addArtifact(
|
fun <T : Task> Project.addArtifact(
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
|
||||||
|
|
||||||
description = "Kotlin Daemon"
|
description = "Kotlin Daemon"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
|||||||
@@ -39,6 +39,12 @@ embedded.isTransitive = false
|
|||||||
configurations.getByName("compileOnly").extendsFrom(embedded)
|
configurations.getByName("compileOnly").extendsFrom(embedded)
|
||||||
val mainJar by configurations.creating
|
val mainJar by configurations.creating
|
||||||
|
|
||||||
|
val modularJar by configurations.creating {
|
||||||
|
attributes {
|
||||||
|
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile(kotlinStdlib())
|
compile(kotlinStdlib())
|
||||||
|
|
||||||
@@ -182,7 +188,11 @@ val relocateCoreSources by task<Copy> {
|
|||||||
|
|
||||||
tasks.getByName("jar").enabled = false
|
tasks.getByName("jar").enabled = false
|
||||||
|
|
||||||
val sourcesJar = tasks.register<Jar>("sourcesJar") {
|
java {
|
||||||
|
withSourcesJar()
|
||||||
|
}
|
||||||
|
|
||||||
|
val sourcesJar = tasks.named<Jar>("sourcesJar") {
|
||||||
archiveClassifier.set("sources")
|
archiveClassifier.set("sources")
|
||||||
|
|
||||||
dependsOn(relocateCoreSources)
|
dependsOn(relocateCoreSources)
|
||||||
@@ -210,7 +220,7 @@ val result by task<Jar> {
|
|||||||
callGroovy("manifestAttributes", manifest, project, "Main", true)
|
callGroovy("manifestAttributes", manifest, project, "Main", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
val modularJar by task<Jar> {
|
val modularJarTask by task<Jar> {
|
||||||
dependsOn(intermediate)
|
dependsOn(intermediate)
|
||||||
archiveClassifier.set("modular")
|
archiveClassifier.set("modular")
|
||||||
from {
|
from {
|
||||||
@@ -235,7 +245,11 @@ artifacts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
add("archives", modularJar)
|
add("archives", modularJarTask)
|
||||||
|
add(modularJar.name, modularJarTask)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val javaComponent = components.findByName("java") as AdhocComponentWithVariants
|
||||||
|
javaComponent.addVariantsFromConfiguration(modularJar) { }
|
||||||
|
|
||||||
javadocJar()
|
javadocJar()
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ tasks {
|
|||||||
failOnWarning = true
|
failOnWarning = true
|
||||||
}
|
}
|
||||||
|
|
||||||
named<Upload>("install") {
|
named("install") {
|
||||||
dependsOn(named("validateTaskProperties"))
|
dependsOn(named("validateTaskProperties"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ val resultJar by task<Jar> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addArtifact("runtime", resultJar)
|
addArtifact("runtime", resultJar)
|
||||||
|
addArtifact("runtimeElements", resultJar)
|
||||||
addArtifact("archives", resultJar)
|
addArtifact("archives", resultJar)
|
||||||
|
|
||||||
sourcesJar()
|
sourcesJar()
|
||||||
|
|||||||
Reference in New Issue
Block a user