From 0b43d7f742f15030d1041db0db689d5182acdacf Mon Sep 17 00:00:00 2001 From: Gabriel Ittner Date: Sun, 10 Dec 2023 15:44:56 +0100 Subject: [PATCH] make it possible to use configure with the main plugin (#680) --- README.md | 8 +- docs/base.md | 371 +---------------- docs/central.md | 28 ++ docs/other.md | 28 ++ docs/what.md | 376 ++++++++++++++++++ .../maven/publish/ProjectSpecRunner.kt | 1 - .../publish/MavenPublishBaseExtension.kt | 67 +++- .../maven/publish/MavenPublishPlugin.kt | 93 +---- .../maven/publish/ProjectExtensions.kt | 16 + 9 files changed, 544 insertions(+), 444 deletions(-) create mode 100644 docs/what.md diff --git a/README.md b/README.md index f0f1028a..d2216449 100755 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ and has been enhanced to add Kotlin support and keep up with the latest changes. - [Publishing open source projects to Maven Central](https://vanniktech.github.io/gradle-maven-publish-plugin/central/) - [Publishing to other Maven repositories](https://vanniktech.github.io/gradle-maven-publish-plugin/other/) +For modifying what is getting published see [configuring what to publish](https://vanniktech.github.io/gradle-maven-publish-plugin/what/). + +There is also a [base plugin](https://vanniktech.github.io/gradle-maven-publish-plugin/base/) that doesn't apply any +default configuration and allows the most customization. + # Supported plugins The output of the following Gradle plugins is supported to be published with this plugin: @@ -44,9 +49,6 @@ plugin directly integrate with, so why should you use this plugin? - **Gradle property based config**. Easily configure the plugin with Gradle properties that will apply to all subprojects -There is also a [base plugin](https://vanniktech.github.io/gradle-maven-publish-plugin/base/) which removes any automatic configuration and allows for a more manual -configuration of what should be published. - # License Copyright (C) 2018 Vanniktech - Niklas Baudy diff --git a/docs/base.md b/docs/base.md index ff56a889..7229099e 100644 --- a/docs/base.md +++ b/docs/base.md @@ -1,8 +1,8 @@ # Base plugin -Starting with version 0.15.0 there is a base plugin. This new plugin has the same capabilities as the main -plugin but does not configure anything automatically. In the current stage the APIs are still marked with `@Incubating` -so they might change. +The base plugin generally provides the same functionality as the main plugin, +but it doesn't configure anything automatically and instead relies on manual +configuration. ## Applying the plugin @@ -26,10 +26,11 @@ Add the plugin to any Gradle project that should be published ## General configuration +Follow the steps of the [Maven Central](central.md) or [other Maven repositories](other.md) guides. +Note that the `SONATYPE_HOST`, `SONATYPE_AUTOMATIC_RELEASE` and `RELEASE_SIGNING_ENABLED` are not +considered by the base plugin and the appropriate DSL methods need to be called. -Follow the steps of the [Maven Central](central.md) or [other Maven repositories](other.md) guides. Note that the -configuration of where to publish to with Gradle properties won't work in the base plugin. For the pom configuration -via Gradle properties the following needs to be enabled in the DSL: +For the pom configuration via Gradle properties the following needs to be enabled in the DSL: === "build.gradle" @@ -49,367 +50,29 @@ via Gradle properties the following needs to be enabled in the DSL: ## Configuring what to publish -The biggest difference between the regular and the base plugin is that it won't setup what to publish by default. The -base plugin offers various APIs depending on the type of project that should be published +To define what should be published, the `configure` method in the `mavenPublishing` +block needs to be called. Check out the [what to publish page](what.md) which +contains a detailed description of available options for each project type. -### Android Library (multiple variants) - -For projects using the `com.android.library` plugin. This will publish all variants of the project (e.g. both -`debug` and `release`) or a subset of specified variants. +It is also possible to get the automatic behavior of the main plugin by calling +`configureBasedOnAppliedPlugins()` instead of `configure` === "build.gradle" ```groovy - import com.vanniktech.maven.publish.AndroidMultiVariantLibrary - mavenPublishing { - // the first parameter represents whether to publish a sources jar - // the second whether to publish a javadoc jar - configure(new AndroidMultiVariantLibrary(true, true)) - // or to limit which build types to include - configure(new AndroidMultiVariantLibrary(true, true, ["beta", "release"])) - // or to limit which flavors to include, the map key is a flavor dimension, the set contains the flavors - configure(new AndroidMultiVariantLibrary(true, true, ["beta", "release"], ["store": ["google", "samsung"]])) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.AndroidMultiVariantLibrary - - mavenPublishing { - configure(AndroidMultiVariantLibrary( - // whether to publish a sources jar - sourcesJar = true, - // whether to publish a javadoc jar - publishJavadocJar = true, - )) - // or - configure(AndroidMultiVariantLibrary( - // whether to publish a sources jar - sourcesJar = true, - // whether to publish a javadoc jar - publishJavadocJar = true, - // limit which build types to include - includedBuildTypeValues = setOf("beta", "release"), - )) + configure(...) // or - configure(AndroidMultiVariantLibrary( - // whether to publish a sources jar - sourcesJar = true, - // whether to publish a javadoc jar - publishJavadocJar = true, - // limit which build types to include - includedBuildTypeValues = setOf("beta", "release"), - // limit which flavors to include, the map key is a flavor dimension, the set contains the flavors - includedFlavorDimensionsAndValues = mapOf("store" to setOf("google", "samsung")), - )) - } - ``` - -### Android Library (single variant) - -For projects using the `com.android.library` plugin. Compared to the multi variant version above this will only publish -the specified variant instead of publishing all of them. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.AndroidSingleVariantLibrary - - mavenPublishing { - // the first parameter represennts which variant is published - // the second whether to publish a sources jar - // the third whether to publish a javadoc jar - configure(new AndroidSingleVariantLibrary("release", true, true)) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.AndroidSingleVariantLibrary - - mavenPublishing { - configure(AndroidSingleVariantLibrary( - // the published variant - variant = "release", - // whether to publish a sources jar - sourcesJar = true, - // whether to publish a javadoc jar - publishJavadocJar = true, - )) - } - ``` - -### Java Library - -For projects using the `java-library` plugin. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.JavaLibrary - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - // the first parameter configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Javadoc()` to publish standard javadocs - // the second whether to publish a sources jar - configure(new JavaLibrary(new JavadocJar.Javadoc(), true)) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.JavaLibrary - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - configure(JavaLibrary( - // configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Javadoc()` to publish standard javadocs - javadocJar = JavadocJar.Javadoc(), - // whether to publish a sources jar - sourcesJar = true, - )) - } - ``` - -### Kotlin JVM Library - -For projects using the `org.jetbrains.kotlin.jvm` plugin. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.KotlinJvm - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - // the first parameter configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - // the second whether to publish a sources jar - configure(new KotlinJvm(new JavadocJar.Dokka("dokkaHtml"), true)) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.KotlinJvm - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - configure(KotlinJvm( - // configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - javadocJar = JavadocJar.Dokka("dokkaHtml"), - // whether to publish a sources jar - sourcesJar = true, - )) - } - ``` - -### Kotlin JS Library - -For projects using the `org.jetbrains.kotlin.js` plugin. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.KotlinJs - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - // the first parameter configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - // sources publishing is always enabled by the Kotlin/JS plugin - configure(new KotlinJs(new JavadocJar.Dokka("dokkaHtml"))) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.KotlinJs - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - // sources publishing is always enabled by the Kotlin/JS plugin - configure(KotlinJs( - // configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - javadocJar = JavadocJar.Dokka("dokkaHtml"), - )) - } - ``` - -### Kotlin Multiplatform Library - -For projects using the `org.jetbrains.kotlin.multiplatform` plugin. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.KotlinMultiplatform - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - // the parameter configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - // sources publishing is always enabled by the Kotlin Multiplatform plugin - configure(new KotlinMultiplatform(new JavadocJar.Dokka("dokkaHtml"))) + configureBasedOnAppliedPlugins() } ``` === "build.gradle.kts" ```kotlin - import com.vanniktech.maven.publish.KotlinMultiplatform - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - // sources publishing is always enabled by the Kotlin Multiplatform plugin - configure(KotlinMultiplatform( - // configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - javadocJar = JavadocJar.Dokka("dokkaHtml"), - )) - } - ``` - -### Gradle Plugin - -For projects using the `java-gradle-plugin` plugin. When also using `com.gradle.plugin-publish` please -use [GradlePublishPlugin](#gradle-publish-plugin) - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.GradlePlugin - import com.vanniktech.maven.publish.JavadocJar - mavenPublishing { - // the first parameter configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Javadoc()` to publish standard javadocs - // the second whether to publish a sources jar - configure(new GradlePlugin(new JavadocJar.Javadoc(), true)) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.GradlePlugin - import com.vanniktech.maven.publish.JavadocJar - - mavenPublishing { - configure(GradlePlugin( - // configures the -javadoc artifact, possible values: - // - `JavadocJar.None()` don't publish this artifact - // - `JavadocJar.Empty()` publish an emprt jar - // - `JavadocJar.Javadoc()` to publish standard javadocs - // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input - javadocJar = JavadocJar.Javadoc(), - // whether to publish a sources jar - sourcesJar = true, - )) - } - ``` - -### Gradle Publish Plugin - -For projects using the `com.gradle.plugin-publish` plugin. This will always publish a sources jar -and a javadoc jar. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.GradlePublishPlugin - - mavenPublishing { - configure(new GradlePublishPlugin()) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.GradlePublishPlugin - - mavenPublishing { - configure(GradlePublishPlugin()) - } - ``` - -### Java Platform - - -For projects using the `java-platform` plugin. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.JavaPlatform - - mavenPublishing { - configure(new JavaPlatform()) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.JavaPlatform - - mavenPublishing { - configure(JavaPlatform()) - } - ``` - - -### Version Catalog - - -For projects using the `version-catalog` plugin. - -=== "build.gradle" - - ```groovy - import com.vanniktech.maven.publish.VersionCatalog - - mavenPublishing { - configure(new VersionCatalog()) - } - ``` - -=== "build.gradle.kts" - - ```kotlin - import com.vanniktech.maven.publish.VersionCatalog - - mavenPublishing { - configure(VersionCatalog()) + configure(...) + // or + configureBasedOnAppliedPlugins() } ``` diff --git a/docs/central.md b/docs/central.md index e3544eae..c57db8ef 100755 --- a/docs/central.md +++ b/docs/central.md @@ -34,6 +34,34 @@ Add the plugin to any Gradle project that should be published } ``` +## Configuring what to publish + +By default, the plugin will automatically detect other applied plugins like the +Android Gradle plugin or Kotlin Gradle plugin and set up what to publish automatically. +This automatic configuration includes publishing a sources and a javadoc jar. The +javadoc jar content is either created from the default javadoc task or from Dokka if +applied. + +To modify these defaults it is possible to call `configure` in the DSL. For +more check out the [what to publish page](what.md) which contains a detailed +description of available options for each project type. + +=== "build.gradle" + + ```groovy + mavenPublishing { + configure(...) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + mavenPublishing { + configure(...) + } + ``` + ## Configuring Maven Central After applying the plugin the first step is to enable publishing to Maven Central diff --git a/docs/other.md b/docs/other.md index 04f60f25..45cfe888 100755 --- a/docs/other.md +++ b/docs/other.md @@ -23,6 +23,34 @@ Add the plugin to any Gradle project that should be published } ``` +## Configuring what to publish + +By default, the plugin will automatically detect other applied plugins like the +Android Gradle plugin or Kotlin Gradle plugin and set up what to publish automatically. +This automatic configuration includes publishing a sources and a javadoc jar. The +javadoc jar content is either created from the default javadoc task or from Dokka if +applied. + +To modify these defaults it is possible to call `configure` in the DSL. For +more check out the [what to publish page](what.md) which contains a detailed +description of available options for each project type. + +=== "build.gradle" + + ```groovy + mavenPublishing { + configure(...) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + mavenPublishing { + configure(...) + } + ``` + ## Configuring the repository A new repository to publish to can be added like this diff --git a/docs/what.md b/docs/what.md new file mode 100644 index 00000000..08ee83bf --- /dev/null +++ b/docs/what.md @@ -0,0 +1,376 @@ +# Configuring what to publish + +It is possible to configure publishing for the following Gradle plugins: +- `com.android.library` as [single variant library](#android-library-single-variant) or + as [multi variant library](#android-library-multiple-variants) +- [`org.jetbrains.kotlin.jvm`](#kotlin-jvm-library) +- [`org.jetbrains.kotlin.js`](#kotlin-js-library) +- [`org.jetbrains.kotlin.multiplatform`](#kotlin-multiplatform-library) +- [`java`](#java-library) +- [`java-library`](#java-library) +- [`java-gradle-plugin`](#gradle-plugin) +- [`com.gradle.plugin-publish`](#gradle-publish-plugin) +- [`java-platform`](#java-platform) +- [`version-catalog`](#version-catalog) + +## Android Library (multiple variants) + +For projects using the `com.android.library` plugin. This will publish all variants of the project (e.g. both +`debug` and `release`) or a subset of specified variants. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.AndroidMultiVariantLibrary + + mavenPublishing { + // the first parameter represents whether to publish a sources jar + // the second whether to publish a javadoc jar + configure(new AndroidMultiVariantLibrary(true, true)) + // or to limit which build types to include + configure(new AndroidMultiVariantLibrary(true, true, ["beta", "release"])) + // or to limit which flavors to include, the map key is a flavor dimension, the set contains the flavors + configure(new AndroidMultiVariantLibrary(true, true, ["beta", "release"], ["store": ["google", "samsung"]])) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.AndroidMultiVariantLibrary + + mavenPublishing { + configure(AndroidMultiVariantLibrary( + // whether to publish a sources jar + sourcesJar = true, + // whether to publish a javadoc jar + publishJavadocJar = true, + )) + // or + configure(AndroidMultiVariantLibrary( + // whether to publish a sources jar + sourcesJar = true, + // whether to publish a javadoc jar + publishJavadocJar = true, + // limit which build types to include + includedBuildTypeValues = setOf("beta", "release"), + )) + // or + configure(AndroidMultiVariantLibrary( + // whether to publish a sources jar + sourcesJar = true, + // whether to publish a javadoc jar + publishJavadocJar = true, + // limit which build types to include + includedBuildTypeValues = setOf("beta", "release"), + // limit which flavors to include, the map key is a flavor dimension, the set contains the flavors + includedFlavorDimensionsAndValues = mapOf("store" to setOf("google", "samsung")), + )) + } + ``` + +## Android Library (single variant) + +For projects using the `com.android.library` plugin. Compared to the multi variant version above this will only publish +the specified variant instead of publishing all of them. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.AndroidSingleVariantLibrary + + mavenPublishing { + // the first parameter represennts which variant is published + // the second whether to publish a sources jar + // the third whether to publish a javadoc jar + configure(new AndroidSingleVariantLibrary("release", true, true)) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.AndroidSingleVariantLibrary + + mavenPublishing { + configure(AndroidSingleVariantLibrary( + // the published variant + variant = "release", + // whether to publish a sources jar + sourcesJar = true, + // whether to publish a javadoc jar + publishJavadocJar = true, + )) + } + ``` + +## Java Library + +For projects using the `java-library` plugin. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.JavaLibrary + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // the first parameter configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Javadoc()` to publish standard javadocs + // the second whether to publish a sources jar + configure(new JavaLibrary(new JavadocJar.Javadoc(), true)) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.JavaLibrary + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + configure(JavaLibrary( + // configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Javadoc()` to publish standard javadocs + javadocJar = JavadocJar.Javadoc(), + // whether to publish a sources jar + sourcesJar = true, + )) + } + ``` + +## Kotlin JVM Library + +For projects using the `org.jetbrains.kotlin.jvm` plugin. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.KotlinJvm + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // the first parameter configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + // the second whether to publish a sources jar + configure(new KotlinJvm(new JavadocJar.Dokka("dokkaHtml"), true)) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.KotlinJvm + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + configure(KotlinJvm( + // configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + javadocJar = JavadocJar.Dokka("dokkaHtml"), + // whether to publish a sources jar + sourcesJar = true, + )) + } + ``` + +## Kotlin JS Library + +For projects using the `org.jetbrains.kotlin.js` plugin. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.KotlinJs + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // the first parameter configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + // sources publishing is always enabled by the Kotlin/JS plugin + configure(new KotlinJs(new JavadocJar.Dokka("dokkaHtml"))) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.KotlinJs + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // sources publishing is always enabled by the Kotlin/JS plugin + configure(KotlinJs( + // configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + javadocJar = JavadocJar.Dokka("dokkaHtml"), + )) + } + ``` + +## Kotlin Multiplatform Library + +For projects using the `org.jetbrains.kotlin.multiplatform` plugin. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.KotlinMultiplatform + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // the parameter configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + // sources publishing is always enabled by the Kotlin Multiplatform plugin + configure(new KotlinMultiplatform(new JavadocJar.Dokka("dokkaHtml"))) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.KotlinMultiplatform + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // sources publishing is always enabled by the Kotlin Multiplatform plugin + configure(KotlinMultiplatform( + // configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + javadocJar = JavadocJar.Dokka("dokkaHtml"), + )) + } + ``` + +## Gradle Plugin + +For projects using the `java-gradle-plugin` plugin. When also using `com.gradle.plugin-publish` please +use [GradlePublishPlugin](#gradle-publish-plugin) + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.GradlePlugin + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + // the first parameter configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Javadoc()` to publish standard javadocs + // the second whether to publish a sources jar + configure(new GradlePlugin(new JavadocJar.Javadoc(), true)) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.GradlePlugin + import com.vanniktech.maven.publish.JavadocJar + + mavenPublishing { + configure(GradlePlugin( + // configures the -javadoc artifact, possible values: + // - `JavadocJar.None()` don't publish this artifact + // - `JavadocJar.Empty()` publish an emprt jar + // - `JavadocJar.Javadoc()` to publish standard javadocs + // - `JavadocJar.Dokka("dokkaHtml")` when using Kotlin with Dokka, where `dokkaHtml` is the name of the Dokka task that should be used as input + javadocJar = JavadocJar.Javadoc(), + // whether to publish a sources jar + sourcesJar = true, + )) + } + ``` + +## Gradle Publish Plugin + +For projects using the `com.gradle.plugin-publish` plugin. This will always publish a sources jar +and a javadoc jar. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.GradlePublishPlugin + + mavenPublishing { + configure(new GradlePublishPlugin()) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.GradlePublishPlugin + + mavenPublishing { + configure(GradlePublishPlugin()) + } + ``` + +## Java Platform + + +For projects using the `java-platform` plugin. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.JavaPlatform + + mavenPublishing { + configure(new JavaPlatform()) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.JavaPlatform + + mavenPublishing { + configure(JavaPlatform()) + } + ``` + + +## Version Catalog + + +For projects using the `version-catalog` plugin. + +=== "build.gradle" + + ```groovy + import com.vanniktech.maven.publish.VersionCatalog + + mavenPublishing { + configure(new VersionCatalog()) + } + ``` + +=== "build.gradle.kts" + + ```kotlin + import com.vanniktech.maven.publish.VersionCatalog + + mavenPublishing { + configure(VersionCatalog()) + } + ``` diff --git a/plugin/src/integrationTest/kotlin/com/vanniktech/maven/publish/ProjectSpecRunner.kt b/plugin/src/integrationTest/kotlin/com/vanniktech/maven/publish/ProjectSpecRunner.kt index 9b8d756a..315bec80 100644 --- a/plugin/src/integrationTest/kotlin/com/vanniktech/maven/publish/ProjectSpecRunner.kt +++ b/plugin/src/integrationTest/kotlin/com/vanniktech/maven/publish/ProjectSpecRunner.kt @@ -178,7 +178,6 @@ private fun writeSettingFile(path: Path) { dependencyResolutionManagement { repositories { - mavenLocal() mavenCentral() google() } diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt index e12776d7..956fbc72 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt @@ -14,10 +14,13 @@ import org.gradle.api.publish.maven.MavenPom import org.gradle.api.publish.maven.MavenPublication import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven import org.gradle.api.publish.maven.tasks.PublishToMavenRepository +import org.gradle.api.tasks.javadoc.Javadoc +import org.gradle.external.javadoc.StandardJavadocDocletOptions import org.gradle.plugins.signing.Sign import org.gradle.plugins.signing.SigningPlugin import org.gradle.plugins.signing.type.pgp.ArmoredSignatureType import org.gradle.util.GradleVersion +import org.jetbrains.dokka.gradle.DokkaTask abstract class MavenPublishBaseExtension( private val project: Project, @@ -30,7 +33,7 @@ abstract class MavenPublishBaseExtension( internal val version: Property = project.objects.property(String::class.java) .convention(project.provider { project.version.toString() }) private val pomFromProperties: Property = project.objects.property(Boolean::class.java) - private val platform: Property = project.objects.property(Platform::class.java) + internal val platform: Property = project.objects.property(Platform::class.java) /** * Sets up Maven Central publishing through Sonatype OSSRH by configuring the target repository. Gradle will then @@ -349,4 +352,66 @@ abstract class MavenPublishBaseExtension( platform.configure(project) } + + /** + * Calls [configure] with a [Platform] chosen based on other applied Gradle plugins. + */ + @Incubating + fun configureBasedOnAppliedPlugins() { + // has already been called before by the user or from finalizeDsl + if (platform.isPresent) { + return + } + + when { + project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform") -> + configure(KotlinMultiplatform(defaultJavaDocOption(plainJavadocSupported = false))) + project.plugins.hasPlugin("com.android.library") -> { + val variant = project.findOptionalProperty("ANDROID_VARIANT_TO_PUBLISH") ?: "release" + configure(AndroidSingleVariantLibrary(variant)) + } + project.plugins.hasPlugin("com.gradle.plugin-publish") -> + configure(GradlePublishPlugin()) + project.plugins.hasPlugin("java-gradle-plugin") -> + configure(GradlePlugin(defaultJavaDocOption(plainJavadocSupported = true))) + project.plugins.hasPlugin("org.jetbrains.kotlin.jvm") -> + configure(KotlinJvm(defaultJavaDocOption(plainJavadocSupported = true))) + project.plugins.hasPlugin("org.jetbrains.kotlin.js") -> + @Suppress("DEPRECATION") + configure(KotlinJs(defaultJavaDocOption(plainJavadocSupported = false))) + project.plugins.hasPlugin("java-library") -> + configure(JavaLibrary(defaultJavaDocOption(plainJavadocSupported = true))) + project.plugins.hasPlugin("java") -> + configure(JavaLibrary(defaultJavaDocOption(plainJavadocSupported = true))) + project.plugins.hasPlugin("java-platform") -> + configure(JavaPlatform()) + project.plugins.hasPlugin("version-catalog") -> + configure(VersionCatalog()) + else -> project.logger.warn("No compatible plugin found in project ${project.path} for publishing") + } + } + + private fun defaultJavaDocOption(plainJavadocSupported: Boolean): JavadocJar { + return if (project.plugins.hasPlugin("org.jetbrains.dokka") || project.plugins.hasPlugin("org.jetbrains.dokka-android")) { + val dokkaTask = project.provider { + val tasks = project.tasks.withType(DokkaTask::class.java) + tasks.singleOrNull()?.name ?: "dokkaHtml" + } + JavadocJar.Dokka(dokkaTask) + } else if (plainJavadocSupported) { + project.tasks.withType(Javadoc::class.java).configureEach { + val options = it.options as StandardJavadocDocletOptions + val javaVersion = project.javaVersion() + if (javaVersion.isJava9Compatible) { + options.addBooleanOption("html5", true) + } + if (javaVersion.isJava8Compatible) { + options.addStringOption("Xdoclint:none", "-quiet") + } + } + return JavadocJar.Javadoc() + } else { + JavadocJar.Empty() + } + } } diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPlugin.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPlugin.kt index 0ac365ff..279ce260 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPlugin.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPlugin.kt @@ -1,13 +1,7 @@ package com.vanniktech.maven.publish -import org.gradle.api.JavaVersion import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.plugins.JavaPluginExtension -import org.gradle.api.provider.Provider -import org.gradle.api.tasks.javadoc.Javadoc -import org.gradle.external.javadoc.StandardJavadocDocletOptions -import org.jetbrains.dokka.gradle.DokkaTask open class MavenPublishPlugin : Plugin { @@ -27,88 +21,17 @@ open class MavenPublishPlugin : Plugin { baseExtension.pomFromGradleProperties() - project.configurePlatform() - } -} - -private fun Project.configurePlatform() { - plugins.withId("org.jetbrains.kotlin.multiplatform") { - baseExtension.configure(KotlinMultiplatform(defaultJavaDocOption() ?: JavadocJar.Empty())) - } - - plugins.withId("com.android.library") { - // afterEvaluate is too late, but we can't run this synchronously because we shouldn't call the APIs for - // multiplatform projects that use Android - androidComponents.finalizeDsl { - if (!plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) { - val variant = project.findOptionalProperty("ANDROID_VARIANT_TO_PUBLISH") ?: "release" - baseExtension.configure(AndroidSingleVariantLibrary(variant)) + // afterEvaluate is too late for AGP which doesn't allow configuration after finalizeDsl + project.plugins.withId("com.android.library") { + @Suppress("UnstableApiUsage") + project.androidComponents.finalizeDsl { + baseExtension.configureBasedOnAppliedPlugins() } } - } - - afterEvaluate { - when { - plugins.hasPlugin("org.jetbrains.kotlin.multiplatform") -> {} // Handled above. - plugins.hasPlugin("com.android.library") -> {} // Handled above. - plugins.hasPlugin("com.gradle.plugin-publish") -> - baseExtension.configure(GradlePublishPlugin()) - plugins.hasPlugin("java-gradle-plugin") -> - baseExtension.configure(GradlePlugin(defaultJavaDocOption() ?: javadoc())) - plugins.hasPlugin("org.jetbrains.kotlin.jvm") -> - baseExtension.configure(KotlinJvm(defaultJavaDocOption() ?: javadoc())) - plugins.hasPlugin("org.jetbrains.kotlin.js") -> - baseExtension.configure(KotlinJs(defaultJavaDocOption() ?: JavadocJar.Empty())) - plugins.hasPlugin("java-library") -> - baseExtension.configure(JavaLibrary(defaultJavaDocOption() ?: javadoc())) - plugins.hasPlugin("java") -> - baseExtension.configure(JavaLibrary(defaultJavaDocOption() ?: javadoc())) - plugins.hasPlugin("java-platform") -> - baseExtension.configure(JavaPlatform()) - plugins.hasPlugin("version-catalog") -> - baseExtension.configure(VersionCatalog()) - else -> logger.warn("No compatible plugin found in project $name for publishing") - } - } -} - -private fun Project.defaultJavaDocOption(): JavadocJar? { - return if (plugins.hasPlugin("org.jetbrains.dokka") || plugins.hasPlugin("org.jetbrains.dokka-android")) { - JavadocJar.Dokka(findDokkaTask()) - } else { - null - } -} - -private fun Project.javadoc(): JavadocJar { - tasks.withType(Javadoc::class.java).configureEach { - val options = it.options as StandardJavadocDocletOptions - val javaVersion = javaVersion() - if (javaVersion.isJava9Compatible) { - options.addBooleanOption("html5", true) - } - if (javaVersion.isJava8Compatible) { - options.addStringOption("Xdoclint:none", "-quiet") - } - } - return JavadocJar.Javadoc() -} -private fun Project.javaVersion(): JavaVersion { - try { - val extension = project.extensions.findByType(JavaPluginExtension::class.java) - if (extension != null) { - val toolchain = extension.toolchain - val version = toolchain.languageVersion.get().asInt() - return JavaVersion.toVersion(version) + project.afterEvaluate { + // will no-op if it was already called + baseExtension.configureBasedOnAppliedPlugins() } - } catch (t: Throwable) { - // ignore failures and fallback to java version in which Gradle is running } - return JavaVersion.current() -} - -private fun Project.findDokkaTask(): Provider = provider { - val tasks = project.tasks.withType(DokkaTask::class.java) - tasks.singleOrNull()?.name ?: "dokkaHtml" } diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/ProjectExtensions.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/ProjectExtensions.kt index 22ad95e3..a768248d 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/ProjectExtensions.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/ProjectExtensions.kt @@ -3,7 +3,9 @@ package com.vanniktech.maven.publish import com.android.build.api.AndroidPluginVersion import com.android.build.api.variant.AndroidComponentsExtension import org.gradle.api.Action +import org.gradle.api.JavaVersion import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.publish.PublishingExtension import org.gradle.api.publish.maven.MavenPublication import org.gradle.plugins.signing.SigningExtension @@ -35,6 +37,20 @@ internal fun Project.mavenPublicationsWithoutPluginMarker(action: Action