Skip to content

Commit

Permalink
deprecate old mavenPublish extension (#340)
Browse files Browse the repository at this point in the history
* deprecate old mavenPublish extension

* ktlint

* Update plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtension.kt
  • Loading branch information
gabrielittner committed May 28, 2022
1 parent 8e1f12d commit c8dd40c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
31 changes: 11 additions & 20 deletions README.md
Expand Up @@ -30,6 +30,9 @@ uses Gradle properties. It's generally recommended to set them in your `gradle.p
file.

```properties
SONATYPE_HOST=DEFAULT
RELEASE_SIGNING_ENABLED=true

GROUP=com.test.mylibrary
POM_ARTIFACT_ID=mylibrary-runtime
VERSION_NAME=3.0.5
Expand Down Expand Up @@ -67,22 +70,19 @@ Without any further configuration the plugin has two tasks. `publish` which will
to Maven Central (through Sonatype OSSRH) by default. To publish to the local maven repository on your
machine (`~/.m2/repository`) there is `publishToMavenLocal`.

In case you are using `s01.oss.sonatype.org` you need to configure that like this:
```groovy
allprojects {
plugins.withId("com.vanniktech.maven.publish") {
mavenPublish {
sonatypeHost = "S01"
}
}
}
In case you are using `s01.oss.sonatype.org` you need to change the `SONATYPE_HOST` property like this:
```properties
SONATYPE_HOST=S01
```

The username and password for Sonatype OSS can be provided as Gradle properties called `mavenCentralUsername`
and `mavenCentralPassword` to avoid having to commit them. You can also supply them as environment variables
called `ORG_GRADLE_PROJECT_mavenCentralUsername` and `ORG_GRADLE_PROJECT_mavenCentralPassword`.

To remove the default repository set `sonatypeHost` to `null`.
To remove the default repository set `SONATYPE_HOST` to an empty string.
```properties
SONATYPE_HOST=
```

You can add additional repositories to publish to using the standard Gradle APIs:

Expand Down Expand Up @@ -128,16 +128,7 @@ signingInMemoryKeyPassword=secret

These properties can also be provided as environment variables by prefixing them with `ORG_GRADLE_PROJECT_`

It is possible to disable signing of release artifacts directly in your build scripts (takes precedence):

```groovy
mavenPublish {
releaseSigningEnabled = false
}
```

Alternatively, you can use a Gradle property which is recommended if you only want to sign certain builds
or only build on certain machines.
It is possible to disable signing of release artifacts by adjusting your gradle.properties like this:

```groovy
RELEASE_SIGNING_ENABLED=false
Expand Down
Expand Up @@ -14,8 +14,8 @@ abstract class MavenPublishBaseExtension(
private val project: Project
) {

private var mavenCentral: Pair<SonatypeHost, String?>? = null
private var signing: Boolean? = null
internal var mavenCentral: Pair<SonatypeHost, String?>? = null
internal var signing: Boolean? = null
private var pomFromProperties: Boolean? = null
private var platform: Platform? = null

Expand All @@ -33,9 +33,8 @@ abstract class MavenPublishBaseExtension(
* @param host the instance of Sonatype OSSRH to use
* @param stagingRepositoryId optional parameter to upload to a specific already created staging repository
*/
@Incubating
@JvmOverloads
fun publishToMavenCentral(host: SonatypeHost, stagingRepositoryId: String? = null) {
fun publishToMavenCentral(host: SonatypeHost = SonatypeHost.DEFAULT, stagingRepositoryId: String? = null) {
val mavenCentral = mavenCentral
if (mavenCentral != null) {
// Ignore subsequent calls with the same arguments.
Expand Down Expand Up @@ -102,7 +101,6 @@ abstract class MavenPublishBaseExtension(
* can be found in the [Gradle documentation](https://docs.gradle.org/current/userguide/signing_plugin.html)
*/
// TODO update in memory set up once https://github.com/gradle/gradle/issues/16056 is implemented
@Incubating
fun signAllPublications() {
if (signing == true) {
// ignore subsequent calls with the same arguments
Expand Down
Expand Up @@ -23,11 +23,45 @@ open class MavenPublishPlugin : Plugin<Project> {

project.afterEvaluate {
val sonatypeHost = extension.sonatypeHost
if (sonatypeHost != null) {
// ignore old extension if new extension was already called
if (sonatypeHost != null && baseExtension.mavenCentral == null) {
// only print warning when sonatypeHost was not set through a gradle property, we will continue supporting this
if (extension.sonatypeHostProperty() == null) {
when (sonatypeHost) {
SonatypeHost.DEFAULT -> project.logger.warn(
"The project is currently configured to be published to " +
"Maven Central. To maintain the current behavior, you need to explicitly add SONATYPE_HOST=DEFAULT to " +
"your gradle.properties or add the following to your build files:\n" +
"mavenPublishing {" +
" publishToMavenCentral()" +
"}"
)
SonatypeHost.S01 -> project.logger.warn(
"Configuring the sonatypeHost through the DSL is deprecated. " +
"Remove the old option and then add either SONATYPE_HOST=S01 to your gradle.properties or add the " +
"following to your build files:\n" +
"mavenPublishing {" +
" publishToMavenCentral(\"S01\")" +
"}"
)
}
}

baseExtension.publishToMavenCentral(sonatypeHost)
}

if (extension.releaseSigningEnabled) {
// ignore old extension if new extension was already called
if (extension.releaseSigningEnabled && baseExtension.signing == null) {
if (extension.releaseSigningProperty() == null) {
project.logger.warn(
"The project is currently configured to be automatically sign release builds before " +
"publishing. To maintain the current behavior you will need to explicitly add " +
"RELEASE_SIGNING_ENABLED=true to your gradle.properties or add the following to your build files:\n" +
"mavenPublishing {" +
" signAllPublications()" +
"}"
)
}
baseExtension.signAllPublications()
}

Expand Down
Expand Up @@ -18,6 +18,7 @@ abstract class MavenPublishPluginExtension(
*
* @Since 0.15.0
*/
@Deprecated("Set the SONATYPE_HOST Gradle property or call mavenPublishing { publishToMavenCentral(\"<VALUE>\") } instead")
var sonatypeHost: SonatypeHost? = defaultSonatypeHost()

/**
Expand All @@ -37,10 +38,19 @@ abstract class MavenPublishPluginExtension(
*
* @Since 0.9.0
*/
var releaseSigningEnabled: Boolean = project.findOptionalProperty("RELEASE_SIGNING_ENABLED")?.toBoolean() ?: true
@Deprecated("Set the RELEASE_SIGNING_ENABLED Gradle property or call mavenPublishing { signAllPublications() } instead")
var releaseSigningEnabled: Boolean = releaseSigningProperty() ?: true

internal fun sonatypeHostProperty(): String? {
return project.findOptionalProperty("SONATYPE_HOST")
}

internal fun releaseSigningProperty(): Boolean? {
return project.findOptionalProperty("RELEASE_SIGNING_ENABLED")?.toBoolean()
}

private fun defaultSonatypeHost(): SonatypeHost? {
val property = project.findOptionalProperty("SONATYPE_HOST")
val property = sonatypeHostProperty()
if (property != null) {
return if (property.isBlank()) {
null
Expand Down

0 comments on commit c8dd40c

Please sign in to comment.