diff --git a/build.gradle.kts b/build.gradle.kts index d89547d..f862724 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,4 @@ +import org.gradle.api.Project.DEFAULT_VERSION import org.gradle.kotlin.dsl.* plugins { @@ -12,7 +13,7 @@ plugins { } group = "io.johnsonlee" -version = project.properties["version"]?.takeUnless { it == "unspecified" } ?: "1.0.0-SNAPSHOT" +version = project.properties["version"]?.takeIf { it != DEFAULT_VERSION } ?: "1.0.0-SNAPSHOT" description = "Gradle plugin for publishing artifacts to Sonatype" repositories { @@ -54,9 +55,9 @@ val sourcesJar by tasks.registering(Jar::class) { } val javadocJar by tasks.registering(Jar::class) { - dependsOn(JavaPlugin.JAVADOC_TASK_NAME) + dependsOn("dokkaHtml") archiveClassifier.set("javadoc") - from(tasks["javadoc"]) + from(tasks["dokkaHtml"]) } val OSSRH_USERNAME = "${project.properties["OSSRH_USERNAME"] ?: System.getenv("OSSRH_USERNAME")}" diff --git a/src/main/kotlin/io/johnsonlee/gradle/publish/AbstractLibraryPublishPlugin.kt b/src/main/kotlin/io/johnsonlee/gradle/publish/AbstractLibraryPublishPlugin.kt index 54fa960..788fecf 100644 --- a/src/main/kotlin/io/johnsonlee/gradle/publish/AbstractLibraryPublishPlugin.kt +++ b/src/main/kotlin/io/johnsonlee/gradle/publish/AbstractLibraryPublishPlugin.kt @@ -7,8 +7,6 @@ import org.gradle.api.Project import org.gradle.api.Project.DEFAULT_VERSION import org.gradle.api.publish.PublicationContainer import org.gradle.api.publish.maven.MavenPublication -import org.gradle.api.tasks.TaskProvider -import org.gradle.api.tasks.bundling.Jar import org.gradle.kotlin.dsl.maven abstract class AbstractLibraryPublishPlugin : Plugin { @@ -32,7 +30,7 @@ abstract class AbstractLibraryPublishPlugin : Plugin { project.run { afterEvaluate { - configureDokka() + configureDependencies() publishing { repositories { @@ -87,23 +85,28 @@ abstract class AbstractLibraryPublishPlugin : Plugin { config: MavenPublication.() -> Unit ) + open fun Project.configureDependencies() { + configureDokka() + } + fun MavenPublication.configure(project: Project) { artifactId = project.name groupId = listOf(project.group, project.rootProject.group) .map(Any::toString) + .filterNot { it == project.rootProject.name || it.startsWith("${project.rootProject.name}.") } .firstOrNull(String::isNotBlank) ?: throw GradleException("group id of $project has not been configured") version = listOf(project.version, project.rootProject.version) - .firstOrNull { it != DEFAULT_VERSION }?.toString() + .map(Any::toString) + .firstOrNull { it != DEFAULT_VERSION } ?: throw GradleException("version of $project has not been configured") } - private fun Project.configureDokka() { + fun Project.configureDokka() { extensions.findByName("kotlin")?.let { plugins.run { apply("org.jetbrains.dokka") } - dependencies.add("dokkaHtmlPlugin", "org.jetbrains.dokka:kotlin-as-java-plugin:1.4.32") } } diff --git a/src/main/kotlin/io/johnsonlee/gradle/publish/AndroidLibraryPublishPlugin.kt b/src/main/kotlin/io/johnsonlee/gradle/publish/AndroidLibraryPublishPlugin.kt index 660e0bd..ef56f11 100644 --- a/src/main/kotlin/io/johnsonlee/gradle/publish/AndroidLibraryPublishPlugin.kt +++ b/src/main/kotlin/io/johnsonlee/gradle/publish/AndroidLibraryPublishPlugin.kt @@ -18,20 +18,20 @@ class AndroidLibraryPublishPlugin : AbstractLibraryPublishPlugin() { publications.run { android.libraryVariants.forEach { variant -> - val javadocs = tasks.register("androidJavadocs${variant.name.capitalize()}", Javadoc::class.java) { + val javadoc = tasks.register("javadocFor${variant.name.capitalize()}", Javadoc::class.java) { dependsOn("dokkaHtml") source(android.sourceSets["main"].java.srcDirs) classpath += files(android.bootClasspath + variant.javaCompileProvider.get().classpath) exclude("**/R.html", "**/R.*.html", "**/index.html") } - val javadocsJar = tasks.register("androidJavadocsJar${variant.name.capitalize()}", Jar::class.java) { - dependsOn(javadocs) + val javadocJar = tasks.register("packageJavadocFor${variant.name.capitalize()}", Jar::class.java) { + dependsOn(javadoc) archiveClassifier.set("javadoc") from(tasks["dokkaHtml"]) } - val sourcesJar = tasks.register("androidSourcesJar${variant.name.capitalize()}", Jar::class.java) { + val sourcesJar = tasks.register("packageSourcesFor${variant.name.capitalize()}", Jar::class.java) { archiveClassifier.set("sources") from(android.sourceSets["main"].java.srcDirs) } @@ -39,7 +39,7 @@ class AndroidLibraryPublishPlugin : AbstractLibraryPublishPlugin() { create(variant.name, MavenPublication::class.java) { configure(project) from(components[variant.name]) - artifact(javadocsJar) + artifact(javadocJar) artifact(sourcesJar) config.invoke(this) } diff --git a/src/main/kotlin/io/johnsonlee/gradle/publish/GradlePluginPublishPlugin.kt b/src/main/kotlin/io/johnsonlee/gradle/publish/GradlePluginPublishPlugin.kt index 133b93c..03ace97 100644 --- a/src/main/kotlin/io/johnsonlee/gradle/publish/GradlePluginPublishPlugin.kt +++ b/src/main/kotlin/io/johnsonlee/gradle/publish/GradlePluginPublishPlugin.kt @@ -17,16 +17,18 @@ class GradlePluginPublishPlugin : AbstractLibraryPublishPlugin() { ) { publications.run { val sourceSets = the() - val javadocsJar = tasks.register("packageJavadoc${project.name.capitalize()}", Jar::class.java) { + val javadocJar = tasks.register("packageJavadocFor${name.capitalize()}", Jar::class.java) { + dependsOn("dokkaHtml") archiveClassifier.set("javadoc") - from(tasks["javadoc"]) + from(tasks["dokkaHtml"]) } - val sourcesJar = tasks.register("packageSources${project.name.capitalize()}", Jar::class.java) { + val sourcesJar = tasks.register("packageSourcesFor${name.capitalize()}", Jar::class.java) { archiveClassifier.set("sources") from(sourceSets["main"].allSource) } + withType().configureEach { - artifact(javadocsJar) + artifact(javadocJar) artifact(sourcesJar) config.invoke(this) } diff --git a/src/main/kotlin/io/johnsonlee/gradle/publish/JavaLibraryPublishPlugin.kt b/src/main/kotlin/io/johnsonlee/gradle/publish/JavaLibraryPublishPlugin.kt index cef5384..7bb4829 100644 --- a/src/main/kotlin/io/johnsonlee/gradle/publish/JavaLibraryPublishPlugin.kt +++ b/src/main/kotlin/io/johnsonlee/gradle/publish/JavaLibraryPublishPlugin.kt @@ -19,21 +19,21 @@ open class JavaLibraryPublishPlugin : AbstractLibraryPublishPlugin() { publications.run { register("mavenJava", MavenPublication::class) { val sourceSets = the() - val sourcesJar = tasks.register("sourcesJar${name.capitalize()}", Jar::class.java) { - dependsOn(JavaPlugin.CLASSES_TASK_NAME) - archiveClassifier.set("sources") - from(sourceSets["main"].allSource) - } - val javadocJar = tasks.register("javadocJar${name.capitalize()}", Jar::class.java) { + val javadocJar = tasks.register("packageJavadocFor${name.capitalize()}", Jar::class.java) { dependsOn(JavaPlugin.JAVADOC_TASK_NAME) archiveClassifier.set("javadoc") from(tasks["javadoc"]) } + val sourcesJar = tasks.register("packageSourcesFor${name.capitalize()}", Jar::class.java) { + dependsOn(JavaPlugin.CLASSES_TASK_NAME) + archiveClassifier.set("sources") + from(sourceSets["main"].allSource) + } configure(project) + from(components["java"]) artifact(javadocJar) artifact(sourcesJar) - from(components["java"]) config.invoke(this) } } diff --git a/src/main/kotlin/io/johnsonlee/gradle/publish/KotlinLibraryPublishPlugin.kt b/src/main/kotlin/io/johnsonlee/gradle/publish/KotlinLibraryPublishPlugin.kt index c7516bb..00d160a 100644 --- a/src/main/kotlin/io/johnsonlee/gradle/publish/KotlinLibraryPublishPlugin.kt +++ b/src/main/kotlin/io/johnsonlee/gradle/publish/KotlinLibraryPublishPlugin.kt @@ -17,23 +17,22 @@ open class KotlinLibraryPublishPlugin : AbstractLibraryPublishPlugin() { config: MavenPublication.() -> Unit ) { publications.run { - register("${project.name}MavenJava", MavenPublication::class) { + register("mavenJava", MavenPublication::class) { val sourceSets = the() - val sourcesJar = tasks.register("sourcesJar${project.name.capitalize()}", Jar::class.java) { + val javadocJar = tasks.register("packageJavadocFor${name.capitalize()}", Jar::class.java) { + archiveClassifier.set("javadoc") + from(tasks["dokkaHtml"]) + } + val sourcesJar = tasks.register("packageSourcesFor${name.capitalize()}", Jar::class.java) { dependsOn(JavaPlugin.CLASSES_TASK_NAME) archiveClassifier.set("sources") from(sourceSets["main"].allSource) } - val javadocJar = tasks.register("javadocJar${project.name.capitalize()}", Jar::class.java) { - dependsOn(JavaPlugin.JAVADOC_TASK_NAME) - archiveClassifier.set("javadoc") - from(tasks["javadoc"]) - } configure(project) from(components["java"]) - artifact(sourcesJar) artifact(javadocJar) + artifact(sourcesJar) config.invoke(this) } } diff --git a/src/main/kotlin/io/johnsonlee/gradle/publish/SonatypePublishPlugin.kt b/src/main/kotlin/io/johnsonlee/gradle/publish/SonatypePublishPlugin.kt index b79f75f..750cc2d 100644 --- a/src/main/kotlin/io/johnsonlee/gradle/publish/SonatypePublishPlugin.kt +++ b/src/main/kotlin/io/johnsonlee/gradle/publish/SonatypePublishPlugin.kt @@ -4,6 +4,7 @@ import de.marcphilipp.gradle.nexus.NexusPublishExtension import io.codearte.gradle.nexus.NexusStagingExtension import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.repositories /** * Gradle plugin for publishing artifacts to [Sonatype](https://oss.sonatype.org/) @@ -17,6 +18,10 @@ class SonatypePublishPlugin : Plugin { project.run(Project::configureNexusPublish) project.run { + repositories { + mavenCentral() + } + afterEvaluate { when { hasAndroidLibraryPlugin -> plugins.apply(AndroidLibraryPublishPlugin::class.java)