diff --git a/README.md b/README.md index 96502fa3..06c13633 100755 --- a/README.md +++ b/README.md @@ -52,4 +52,4 @@ This Gradle plugin is using itself to publish any of the updates. It applies a p Copyright (C) 2018 Vanniktech - Niklas Baudy -Licensed under the Apache License, Version 2.0 +Licensed under the Apache License, Version 2.0 \ No newline at end of file diff --git a/src/main/groovy/com/vanniktech/maven/publish/MavenPublishPlugin.groovy b/src/main/groovy/com/vanniktech/maven/publish/MavenPublishPlugin.groovy index f1c82f9d..c438e475 100755 --- a/src/main/groovy/com/vanniktech/maven/publish/MavenPublishPlugin.groovy +++ b/src/main/groovy/com/vanniktech/maven/publish/MavenPublishPlugin.groovy @@ -3,6 +3,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.artifacts.Dependency import org.gradle.api.artifacts.maven.MavenDeployment import org.gradle.api.artifacts.maven.MavenPom import org.gradle.api.plugins.MavenPlugin @@ -11,9 +12,12 @@ import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.plugins.signing.SigningPlugin +import static com.vanniktech.maven.publish.MavenPublishPluginExtension.DEFAULT_TARGET +import static com.vanniktech.maven.publish.MavenPublishPluginExtension.LOCAL_TARGET + class MavenPublishPlugin implements Plugin { @Override void apply(final Project p) { - p.extensions.create('mavenPublish', MavenPublishPluginExtension.class, p) + def extension = p.extensions.create('mavenPublish', MavenPublishPluginExtension.class, p) p.plugins.apply(MavenPlugin) p.plugins.apply(SigningPlugin) @@ -21,30 +25,19 @@ class MavenPublishPlugin implements Plugin { p.group = p.findProperty("GROUP") p.version = p.findProperty("VERSION_NAME") - def extension = p.mavenPublish - - p.afterEvaluate { project -> - project.uploadArchives { - repositories { - mavenDeployer { - beforeDeployment { MavenDeployment deployment -> project.signing.signPom(deployment) } - - repository(url: extension.releaseRepositoryUrl) { - authentication(userName: extension.repositoryUsername, password: extension.repositoryPassword) - } - - snapshotRepository(url: extension.snapshotRepositoryUrl) { - authentication(userName: extension.repositoryUsername, password: extension.repositoryPassword) - } - - configurePom(p, pom) - } + p.afterEvaluate { Project project -> + extension.targets.each { target -> + if (target.releaseRepositoryUrl == null) { + throw new IllegalStateException("The release repository url of ${target.name} is null or not set") } + + Upload upload = getUploadTask(project, target.name) + configureMavenDeployer(project, upload, target) } project.signing { required { !project.version.contains("SNAPSHOT") && project.gradle.taskGraph.hasTask("uploadArchives") } - sign project.configurations.archives + sign project.configurations[Dependency.ARCHIVES_CONFIGURATION] } def plugins = project.getPlugins() @@ -123,18 +116,45 @@ class MavenPublishPlugin implements Plugin { } } } + } + } - project.tasks.create("installArchives", Upload) { - description = "Installs the artifacts to the local Maven repository." - group = "upload" - configuration = project.configurations['archives'] - repositories { - mavenDeployer { - repository url: project.repositories.mavenLocal().url + private Upload getUploadTask(Project project, String name) { + if (name == DEFAULT_TARGET) { + return project.uploadArchives + } else if (name == LOCAL_TARGET) { + return createUploadTask(project, name, "Installs the artifacts to the local Maven repository.") + } else { + return createUploadTask(project, DEFAULT_TARGET + name.capitalize(), "Upload all artifacts to $name") + } + } - configurePom(p, pom) + private Upload createUploadTask(Project project, String name, String taskDescription) { + return (Upload) project.tasks.create(name, Upload.class) { + group = "upload" + description = taskDescription + configuration = project.configurations[Dependency.ARCHIVES_CONFIGURATION] + } + } + + private void configureMavenDeployer(Project project, Upload upload, MavenPublishTarget target) { + upload.repositories { + mavenDeployer { + if (target.signing) { + beforeDeployment { MavenDeployment deployment -> project.signing.signPom(deployment) } + } + + repository(url: target.releaseRepositoryUrl) { + authentication(userName: target.repositoryUsername, password: target.repositoryPassword) + } + + if (target.snapshotRepositoryUrl != null) { + snapshotRepository(url: target.snapshotRepositoryUrl) { + authentication(userName: target.repositoryUsername, password: target.repositoryPassword) } } + + configurePom(project, pom) } } } diff --git a/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtension.kt b/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtension.kt index 2ba9826c..7846f458 100644 --- a/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtension.kt +++ b/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtension.kt @@ -1,5 +1,7 @@ package com.vanniktech.maven.publish +import groovy.lang.Closure +import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project /** @@ -7,31 +9,41 @@ import org.gradle.api.Project * @since 0.1.0 */ open class MavenPublishPluginExtension(project: Project) { - /** - * The release repository url this should be published to. - * This defaults to either the RELEASE_REPOSITORY_URL Gradle property, the system environment variable or the sonatype url. - * @since 0.1.0 - */ - var releaseRepositoryUrl: String = project.findProperty("RELEASE_REPOSITORY_URL") as String? ?: System.getenv("RELEASE_REPOSITORY_URL") ?: "https://oss.sonatype.org/service/local/staging/deploy/maven2/" - /** - * The snapshot repository url this should be published to. - * This defaults to either the SNAPSHOT_REPOSITORY_URL Gradle property, the system environment variable or the snapshot sonatype url. - * @since 0.1.0 - */ - var snapshotRepositoryUrl: String = project.findProperty("SNAPSHOT_REPOSITORY_URL") as String? ?: System.getenv("SNAPSHOT_REPOSITORY_URL") ?: "https://oss.sonatype.org/content/repositories/snapshots/" + private val defaultTarget = MavenPublishTarget( + DEFAULT_TARGET, + project.findProperty("RELEASE_REPOSITORY_URL") as String? ?: System.getenv("RELEASE_REPOSITORY_URL") ?: "https://oss.sonatype.org/service/local/staging/deploy/maven2/", + project.findProperty("SNAPSHOT_REPOSITORY_URL") as String? ?: System.getenv("SNAPSHOT_REPOSITORY_URL") ?: "https://oss.sonatype.org/content/repositories/snapshots/", + project.findProperty("SONATYPE_NEXUS_USERNAME") as String? ?: System.getenv("SONATYPE_NEXUS_USERNAME"), + project.findProperty("SONATYPE_NEXUS_PASSWORD") as String? ?: System.getenv("SONATYPE_NEXUS_PASSWORD") + ) + + private val localTarget = MavenPublishTarget( + LOCAL_TARGET, + releaseRepositoryUrl = project.repositories.mavenLocal().url.toASCIIString(), + signing = false + ) /** - * The username that should be used for publishing. - * This defaults to either the SONATYPE_NEXUS_USERNAME Gradle property or the system environment variable. - * @since 0.1.0 + * Allows to add additional [MavenPublishTargets][MavenPublishTarget] to publish to multiple repositories. + * @since 0.7.0 */ - var repositoryUsername: String? = project.findProperty("SONATYPE_NEXUS_USERNAME") as String? ?: System.getenv("SONATYPE_NEXUS_USERNAME") + val targets: NamedDomainObjectContainer = + project.container(MavenPublishTarget::class.java) { MavenPublishTarget(it) }.apply { + add(defaultTarget) + add(localTarget) + } /** - * The password that should be used for publishing. - * This defaults to either the SONATYPE_NEXUS_PASSWORD Gradle property or the system environment variable. - * @since 0.1.0 + * Allows to add additional [MavenPublishTargets][MavenPublishTarget] to publish to multiple repositories. + * @since 0.7.0 */ - var repositoryPassword: String? = project.findProperty("SONATYPE_NEXUS_PASSWORD") as String? ?: System.getenv("SONATYPE_NEXUS_PASSWORD") + fun targets(closure: Closure<*>) { + targets.configure(closure) + } + + internal companion object { + internal const val DEFAULT_TARGET = "uploadArchives" + internal const val LOCAL_TARGET = "installArchives" + } } diff --git a/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt b/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt new file mode 100644 index 00000000..48ca5248 --- /dev/null +++ b/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt @@ -0,0 +1,34 @@ +package com.vanniktech.maven.publish + +data class MavenPublishTarget( + internal val name: String, + /** + * The release repository url this should be published to. + * @since 0.7.0 + */ + var releaseRepositoryUrl: String? = null, + + /** + * The snapshot repository url this should be published to. + * @since 0.7.0 + */ + var snapshotRepositoryUrl: String? = null, + + /** + * The username that should be used for publishing. + * @since 0.7.0 + */ + var repositoryUsername: String? = null, + + /** + * The password that should be used for publishing. + * @since 0.7.0 + */ + var repositoryPassword: String? = null, + + /** + * Whether release artifacts should be signed before uploading to this target. + * @since 0.7.0 + */ + var signing: Boolean = true +) diff --git a/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtensionTest.kt b/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtensionTest.kt index 5cdc41be..2db6b2a6 100644 --- a/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtensionTest.kt +++ b/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginExtensionTest.kt @@ -19,40 +19,42 @@ class MavenPublishPluginExtensionTest { } @Test fun defaultReleaseRepositoryUrl() { - assertThat(MavenPublishPluginExtension(project).releaseRepositoryUrl).isEqualTo("https://oss.sonatype.org/service/local/staging/deploy/maven2/") + assertThat(MavenPublishPluginExtension(project).uploadArchives.releaseRepositoryUrl).isEqualTo("https://oss.sonatype.org/service/local/staging/deploy/maven2/") } @Test fun defaultSnapshotRepositoryUrl() { - assertThat(MavenPublishPluginExtension(project).snapshotRepositoryUrl).isEqualTo("https://oss.sonatype.org/content/repositories/snapshots/") + assertThat(MavenPublishPluginExtension(project).uploadArchives.snapshotRepositoryUrl).isEqualTo("https://oss.sonatype.org/content/repositories/snapshots/") } @Test fun defaultReleaseRepositoryUrlEnvironmentVariable() { environmentVariables.set("RELEASE_REPOSITORY_URL", "https://releases.fake/") - assertThat(MavenPublishPluginExtension(project).releaseRepositoryUrl).isEqualTo("https://releases.fake/") + assertThat(MavenPublishPluginExtension(project).uploadArchives.releaseRepositoryUrl).isEqualTo("https://releases.fake/") } @Test fun defaultSnapshotRepositoryUrlEnvironmentVariable() { environmentVariables.set("SNAPSHOT_REPOSITORY_URL", "https://snapshots.fake/") - assertThat(MavenPublishPluginExtension(project).snapshotRepositoryUrl).isEqualTo("https://snapshots.fake/") + assertThat(MavenPublishPluginExtension(project).uploadArchives.snapshotRepositoryUrl).isEqualTo("https://snapshots.fake/") } @Test fun defaultRepositoryUsernameEnvironmentVariable() { environmentVariables.set("SONATYPE_NEXUS_USERNAME", "env") - assertThat(MavenPublishPluginExtension(project).repositoryUsername).isEqualTo("env") + assertThat(MavenPublishPluginExtension(project).uploadArchives.repositoryUsername).isEqualTo("env") } @Test fun defaultRepositoryUsernameNothing() { assumeTrue(System.getenv("TRAVIS") != "true") // Do not run on travis since we inject the same values. - assertThat(MavenPublishPluginExtension(project).repositoryUsername).isNull() + assertThat(MavenPublishPluginExtension(project).uploadArchives.repositoryUsername).isNull() } @Test fun defaultRepositoryPasswordEnvironmentVariable() { environmentVariables.set("SONATYPE_NEXUS_PASSWORD", "env") - assertThat(MavenPublishPluginExtension(project).repositoryPassword).isEqualTo("env") + assertThat(MavenPublishPluginExtension(project).uploadArchives.repositoryPassword).isEqualTo("env") } @Test fun defaultRepositoryPasswordNothing() { assumeTrue(System.getenv("TRAVIS") != "true") // Do not run on travis since we inject the same values. - assertThat(MavenPublishPluginExtension(project).repositoryPassword).isNull() + assertThat(MavenPublishPluginExtension(project).uploadArchives.repositoryPassword).isNull() } + + private val MavenPublishPluginExtension.uploadArchives get() = targets.getByName("uploadArchives") } diff --git a/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginTest.kt b/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginTest.kt index 119f9c9c..f48dd7d9 100644 --- a/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginTest.kt +++ b/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishPluginTest.kt @@ -77,8 +77,8 @@ class MavenPublishPluginTest { project.plugins.apply(MavenPublishPlugin::class.java) val extension = project.extensions.getByType(MavenPublishPluginExtension::class.java) - extension.repositoryUsername = "bar" - extension.repositoryPassword = "foo" + extension.targets.getByName("uploadArchives").repositoryUsername = "bar" + extension.targets.getByName("uploadArchives").repositoryPassword = "foo" (project as DefaultProject).evaluate()