Skip to content

Commit

Permalink
Improve publishing configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed Oct 6, 2021
1 parent b2fb9f1 commit 2fdec27
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 33 deletions.
7 changes: 4 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.gradle.api.Project.DEFAULT_VERSION
import org.gradle.kotlin.dsl.*

plugins {
Expand All @@ -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 {
Expand Down Expand Up @@ -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")}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
Expand All @@ -32,7 +30,7 @@ abstract class AbstractLibraryPublishPlugin : Plugin<Project> {

project.run {
afterEvaluate {
configureDokka()
configureDependencies()

publishing {
repositories {
Expand Down Expand Up @@ -87,23 +85,28 @@ abstract class AbstractLibraryPublishPlugin : Plugin<Project> {
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")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ 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)
}

create(variant.name, MavenPublication::class.java) {
configure(project)
from(components[variant.name])
artifact(javadocsJar)
artifact(javadocJar)
artifact(sourcesJar)
config.invoke(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class GradlePluginPublishPlugin : AbstractLibraryPublishPlugin() {
) {
publications.run {
val sourceSets = the<SourceSetContainer>()
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<MavenPublication>().configureEach {
artifact(javadocsJar)
artifact(javadocJar)
artifact(sourcesJar)
config.invoke(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ open class JavaLibraryPublishPlugin : AbstractLibraryPublishPlugin() {
publications.run {
register("mavenJava", MavenPublication::class) {
val sourceSets = the<SourceSetContainer>()
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceSetContainer>()
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand All @@ -17,6 +18,10 @@ class SonatypePublishPlugin : Plugin<Project> {
project.run(Project::configureNexusPublish)

project.run {
repositories {
mavenCentral()
}

afterEvaluate {
when {
hasAndroidLibraryPlugin -> plugins.apply(AndroidLibraryPublishPlugin::class.java)
Expand Down

0 comments on commit 2fdec27

Please sign in to comment.