Update Gradle plugins publishing setup
Simplifies and make it aligned with official documentation, which removes some old workarounds. Additionally, it enables publication of Gradle metadata file, which is required for plugin variants feature support. Instead of using 'gradleApi()' as Gradle API dependency, which provides build current Gradle version api, now setup is using "dev.gradleplugins:gradle-api" artifacts with specific Gradle version. This allows to have more fine-grained Gradle support for removed apis. ^KT-49227 In Progress
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.artifacts.ModuleDependency
|
||||
import org.gradle.api.plugins.JavaLibraryPlugin
|
||||
import org.gradle.api.plugins.JavaPlugin.JAVADOC_ELEMENTS_CONFIGURATION_NAME
|
||||
import org.gradle.api.plugins.JavaPlugin.SOURCES_ELEMENTS_CONFIGURATION_NAME
|
||||
import org.gradle.api.plugins.JavaPluginExtension
|
||||
import org.gradle.api.publish.PublishingExtension
|
||||
import org.gradle.api.publish.maven.MavenPublication
|
||||
import org.gradle.api.tasks.SourceSet
|
||||
import org.gradle.jvm.tasks.Jar
|
||||
import org.gradle.kotlin.dsl.*
|
||||
import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin
|
||||
import org.jetbrains.dokka.gradle.DokkaTask
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import plugins.configureDefaultPublishing
|
||||
import plugins.configureKotlinPomAttributes
|
||||
|
||||
/**
|
||||
* Configures common pom configuration parameters
|
||||
*/
|
||||
fun Project.configureCommonPublicationSettingsForGradle() {
|
||||
plugins.withId("maven-publish") {
|
||||
configureDefaultPublishing()
|
||||
|
||||
extensions.configure<PublishingExtension> {
|
||||
publications
|
||||
.withType<MavenPublication>()
|
||||
.configureEach {
|
||||
configureKotlinPomAttributes(project)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* These dependencies will be provided by Gradle, and we should prevent version conflict
|
||||
*/
|
||||
fun Configuration.excludeGradleCommonDependencies() {
|
||||
dependencies
|
||||
.withType<ModuleDependency>()
|
||||
.configureEach {
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk7")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-common")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-reflect")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-script-runtime")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude Gradle runtime from given SourceSet configurations.
|
||||
*/
|
||||
fun Project.excludeGradleCommonDependencies(sourceSet: SourceSet) {
|
||||
configurations[sourceSet.implementationConfigurationName].excludeGradleCommonDependencies()
|
||||
configurations[sourceSet.apiConfigurationName].excludeGradleCommonDependencies()
|
||||
configurations[sourceSet.runtimeOnlyConfigurationName].excludeGradleCommonDependencies()
|
||||
}
|
||||
|
||||
/**
|
||||
* 'main' sources are used for Gradle 6.1-6.9 versions.
|
||||
* Directories are renamed into 'src/gradle61'.
|
||||
*/
|
||||
fun Project.configureGradlePluginCommonSettings() {
|
||||
sourceSets.named(SourceSet.MAIN_SOURCE_SET_NAME) {
|
||||
plugins.withType<JavaGradlePluginPlugin>().configureEach {
|
||||
// Removing Gradle api default dependency added by 'java-gradle-plugin'
|
||||
configurations[apiConfigurationName].dependencies.remove(dependencies.gradleApi())
|
||||
}
|
||||
|
||||
dependencies {
|
||||
"compileOnly"(kotlinStdlib())
|
||||
// Decoupling gradle-api artifact from current project Gradle version. Later would be useful for
|
||||
// gradle plugin variants
|
||||
"compileOnly"("dev.gradleplugins:gradle-api:7.1.1")
|
||||
if (this@configureGradlePluginCommonSettings.name != "kotlin-gradle-plugin-api") {
|
||||
"api"(project(":kotlin-gradle-plugin-api"))
|
||||
}
|
||||
}
|
||||
|
||||
excludeGradleCommonDependencies(this)
|
||||
|
||||
tasks.withType<Jar>().configureEach {
|
||||
if (name == jarTaskName) {
|
||||
setupPublicJar(archiveBaseName.get())
|
||||
addEmbeddedRuntime()
|
||||
} else if (name == sourcesJarTaskName) {
|
||||
addEmbeddedSources()
|
||||
|
||||
configurePublishedComponent {
|
||||
addVariantsFromConfiguration(configurations[SOURCES_ELEMENTS_CONFIGURATION_NAME]) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
plugins.withType<JavaLibraryPlugin>().configureEach {
|
||||
this@configureGradlePluginCommonSettings
|
||||
.extensions
|
||||
.configure<JavaPluginExtension> {
|
||||
withSourcesJar()
|
||||
withJavadocJar()
|
||||
}
|
||||
}
|
||||
|
||||
plugins.withId("org.jetbrains.dokka") {
|
||||
val dokkaTask = tasks.named<DokkaTask>("dokkaJavadoc")
|
||||
|
||||
tasks.withType<Jar>().configureEach {
|
||||
if (name == javadocJarTaskName) {
|
||||
from(dokkaTask.flatMap { it.outputDirectory })
|
||||
|
||||
configurePublishedComponent {
|
||||
addVariantsFromConfiguration(configurations[JAVADOC_ELEMENTS_CONFIGURATION_NAME]) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Project.configureKotlinCompileTasksGradleCompatibility() {
|
||||
tasks.withType<KotlinCompile>().configureEach {
|
||||
kotlinOptions.languageVersion = "1.4"
|
||||
kotlinOptions.apiVersion = "1.4"
|
||||
kotlinOptions.freeCompilerArgs += listOf(
|
||||
"-Xskip-prerelease-check",
|
||||
"-Xsuppress-version-warnings",
|
||||
"-Xuse-ir" // Needed as long as languageVersion is less than 1.5.
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -67,15 +67,19 @@ fun Project.noDefaultJar() {
|
||||
}
|
||||
}
|
||||
|
||||
fun Jar.addEmbeddedRuntime() {
|
||||
project.configurations.findByName("embedded")?.let { embedded ->
|
||||
dependsOn(embedded)
|
||||
from {
|
||||
embedded.map(project::zipTree)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
embedded.map(::zipTree)
|
||||
}
|
||||
}
|
||||
addEmbeddedRuntime()
|
||||
setupPublicJar(project.extensions.getByType<BasePluginExtension>().archivesName.get())
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
body()
|
||||
@@ -102,34 +106,22 @@ fun Project.runtimeJar(task: TaskProvider<ShadowJar>, body: ShadowJar.() -> Unit
|
||||
return task
|
||||
}
|
||||
|
||||
private fun Project.mainJavaPluginSourceSet() = findJavaPluginExtension()?.sourceSets?.findByName("main")
|
||||
private fun Project.mainKotlinSourceSet() =
|
||||
(extensions.findByName("kotlin") as? KotlinSourceSetContainer)?.sourceSets?.findByName("main")
|
||||
private fun Project.sources() = mainJavaPluginSourceSet()?.allSource ?: mainKotlinSourceSet()?.kotlin
|
||||
|
||||
fun Project.sourcesJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
||||
configure<JavaPluginExtension> {
|
||||
withSourcesJar()
|
||||
}
|
||||
|
||||
val sourcesJar = getOrCreateTask<Jar>("sourcesJar") {
|
||||
fun Project.mainJavaPluginSourceSet() = findJavaPluginExtension()?.sourceSets?.findByName("main")
|
||||
fun Project.mainKotlinSourceSet() =
|
||||
(extensions.findByName("kotlin") as? KotlinSourceSetContainer)?.sourceSets?.findByName("main")
|
||||
|
||||
fun Project.sources() = mainJavaPluginSourceSet()?.allSource ?: mainKotlinSourceSet()?.kotlin
|
||||
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
archiveClassifier.set("sources")
|
||||
|
||||
from(project.sources())
|
||||
|
||||
project.configurations.findByName("embedded")?.let { embedded ->
|
||||
from(provider {
|
||||
embedded.resolvedConfiguration
|
||||
.resolvedArtifacts
|
||||
.map { it.id.componentIdentifier }
|
||||
.filterIsInstance<ProjectComponentIdentifier>()
|
||||
.mapNotNull {
|
||||
project(it.projectPath).sources()
|
||||
}
|
||||
})
|
||||
}
|
||||
addEmbeddedSources()
|
||||
|
||||
body()
|
||||
}
|
||||
@@ -144,6 +136,20 @@ fun Project.sourcesJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
||||
return sourcesJar
|
||||
}
|
||||
|
||||
fun Jar.addEmbeddedSources() {
|
||||
project.configurations.findByName("embedded")?.let { embedded ->
|
||||
from(project.provider {
|
||||
embedded.resolvedConfiguration
|
||||
.resolvedArtifacts
|
||||
.map { it.id.componentIdentifier }
|
||||
.filterIsInstance<ProjectComponentIdentifier>()
|
||||
.mapNotNull {
|
||||
project.project(it.projectPath).sources()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fun Project.javadocJar(body: Jar.() -> Unit = {}): TaskProvider<Jar> {
|
||||
configure<JavaPluginExtension> {
|
||||
withJavadocJar()
|
||||
|
||||
@@ -2,50 +2,39 @@
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import com.gradle.publish.PluginBundleExtension
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
`java-gradle-plugin`
|
||||
id("org.jetbrains.dokka")
|
||||
`maven-publish`
|
||||
id("com.gradle.plugin-publish")
|
||||
}
|
||||
|
||||
publishGradlePlugin()
|
||||
standardPublicJars()
|
||||
|
||||
configureCommonPublicationSettingsForGradle()
|
||||
configureKotlinCompileTasksGradleCompatibility()
|
||||
extensions.extraProperties["kotlin.stdlib.default.dependency"] = "false"
|
||||
|
||||
dependencies {
|
||||
compileOnly(kotlinStdlib())
|
||||
compileOnly(gradleApi())
|
||||
// common plugin bundle configuration
|
||||
configure<PluginBundleExtension> {
|
||||
website = "https://kotlinlang.org/"
|
||||
vcsUrl = "https://github.com/jetbrains/kotlin"
|
||||
tags = listOf("kotlin")
|
||||
}
|
||||
|
||||
// These dependencies will be provided by Gradle and we should prevent version conflict
|
||||
fun Configuration.excludeGradleCommonDependencies() {
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk7")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-common")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-reflect")
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-script-runtime")
|
||||
}
|
||||
configurations {
|
||||
"implementation" {
|
||||
excludeGradleCommonDependencies()
|
||||
}
|
||||
"api" {
|
||||
excludeGradleCommonDependencies()
|
||||
configureGradlePluginCommonSettings()
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
withType<MavenPublication>().configureEach {
|
||||
if (name.endsWith("PluginMarkerMaven")) {
|
||||
pom {
|
||||
// https://github.com/gradle/gradle/issues/8754
|
||||
// and https://github.com/gradle/gradle/issues/6155
|
||||
packaging = "pom"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions.languageVersion = "1.4"
|
||||
kotlinOptions.apiVersion = "1.4"
|
||||
kotlinOptions.freeCompilerArgs += listOf(
|
||||
"-Xskip-prerelease-check",
|
||||
"-Xsuppress-version-warnings",
|
||||
"-Xuse-ir" // Needed as long as languageVersion is less than 1.5.
|
||||
)
|
||||
}
|
||||
|
||||
tasks.named<Jar>("jar") {
|
||||
callGroovy("manifestAttributes", manifest, project)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
import plugins.KotlinBuildPublishingPlugin.Companion.DEFAULT_MAIN_PUBLICATION_NAME
|
||||
|
||||
plugins {
|
||||
`java-library`
|
||||
kotlin("jvm")
|
||||
id("org.jetbrains.dokka")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
configureCommonPublicationSettingsForGradle()
|
||||
configureKotlinCompileTasksGradleCompatibility()
|
||||
extensions.extraProperties["kotlin.stdlib.default.dependency"] = "false"
|
||||
|
||||
configureGradlePluginCommonSettings()
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
register<MavenPublication>(DEFAULT_MAIN_PUBLICATION_NAME) {
|
||||
from(components["java"])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user