From 7bed96e4dc96f1ccacddd7af10ad4bbc1a99d639 Mon Sep 17 00:00:00 2001 From: Gabriel Ittner Date: Tue, 9 Oct 2018 16:18:17 +0200 Subject: [PATCH] improve when signing tasks run, consider all targets for signing (#30) - signing configuration isn't part of afterEvaluate anymore which makes manually overriding it easier - required (= task fails when signing isn't set up on the local machine) only considers if the task is a snapshot - signing will only be run when also running an `Upload` task (not during things like assemble) - the previous point will now consider all targets, before only `uploadArchives` was considered - added info level logs to make debugging issues easier fixes #29 --- .../maven/publish/MavenPublishPlugin.groovy | 34 +++++++++++++------ .../maven/publish/MavenPublishTarget.kt | 14 +++++++- .../maven/publish/MavenPublishTargetTest.kt | 24 +++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/test/kotlin/com/vanniktech/maven/publish/MavenPublishTargetTest.kt diff --git a/src/main/groovy/com/vanniktech/maven/publish/MavenPublishPlugin.groovy b/src/main/groovy/com/vanniktech/maven/publish/MavenPublishPlugin.groovy index c438e475..fb438e6a 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.Task import org.gradle.api.artifacts.Dependency import org.gradle.api.artifacts.maven.MavenDeployment import org.gradle.api.artifacts.maven.MavenPom @@ -10,6 +11,7 @@ import org.gradle.api.plugins.MavenPlugin import org.gradle.api.tasks.Upload import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.javadoc.Javadoc +import org.gradle.plugins.signing.Sign import org.gradle.plugins.signing.SigningPlugin import static com.vanniktech.maven.publish.MavenPublishPluginExtension.DEFAULT_TARGET @@ -25,21 +27,33 @@ class MavenPublishPlugin implements Plugin { p.group = p.findProperty("GROUP") p.version = p.findProperty("VERSION_NAME") + p.signing { + required { !p.version.contains("SNAPSHOT") } + sign p.configurations[Dependency.ARCHIVES_CONFIGURATION] + } + p.tasks.withType(Sign).each { sign -> + sign.onlyIf { + Set signedTargets = extension.targets.findAll { it.signing } + sign.logger.info("Targets that should be signed: ${signedTargets.collect { it.name }}") + signedTargets.any { target -> + Task task = p.tasks.getByName(target.taskName) + boolean taskInGraph = p.gradle.taskGraph.hasTask(task) + sign.logger.info("Task for ${target.name} will be executed: $taskInGraph") + taskInGraph + } + } + } + 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) + Upload upload = getUploadTask(project, target.name, target.taskName) configureMavenDeployer(project, upload, target) } - project.signing { - required { !project.version.contains("SNAPSHOT") && project.gradle.taskGraph.hasTask("uploadArchives") } - sign project.configurations[Dependency.ARCHIVES_CONFIGURATION] - } - def plugins = project.getPlugins() if (plugins.hasPlugin('com.android.library')) { @@ -119,13 +133,13 @@ class MavenPublishPlugin implements Plugin { } } - private Upload getUploadTask(Project project, String name) { + private Upload getUploadTask(Project project, String name, String taskName) { if (name == DEFAULT_TARGET) { - return project.uploadArchives + return (Upload) project.tasks.getByName(taskName) } else if (name == LOCAL_TARGET) { - return createUploadTask(project, name, "Installs the artifacts to the local Maven repository.") + return createUploadTask(project, taskName, "Installs the artifacts to the local Maven repository.") } else { - return createUploadTask(project, DEFAULT_TARGET + name.capitalize(), "Upload all artifacts to $name") + return createUploadTask(project, taskName, "Upload all artifacts to $name") } } diff --git a/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt b/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt index 48ca5248..7d2b7916 100644 --- a/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt +++ b/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishTarget.kt @@ -1,5 +1,8 @@ package com.vanniktech.maven.publish +import com.vanniktech.maven.publish.MavenPublishPluginExtension.Companion.DEFAULT_TARGET +import com.vanniktech.maven.publish.MavenPublishPluginExtension.Companion.LOCAL_TARGET + data class MavenPublishTarget( internal val name: String, /** @@ -31,4 +34,13 @@ data class MavenPublishTarget( * @since 0.7.0 */ var signing: Boolean = true -) +) { + + val taskName get(): String { + if (name == DEFAULT_TARGET || name == LOCAL_TARGET) { + return name + } else { + return DEFAULT_TARGET + name.capitalize() + } + } +} diff --git a/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishTargetTest.kt b/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishTargetTest.kt new file mode 100644 index 00000000..5b7c787c --- /dev/null +++ b/src/test/kotlin/com/vanniktech/maven/publish/MavenPublishTargetTest.kt @@ -0,0 +1,24 @@ +package com.vanniktech.maven.publish + +import com.vanniktech.maven.publish.MavenPublishPluginExtension.Companion.DEFAULT_TARGET +import com.vanniktech.maven.publish.MavenPublishPluginExtension.Companion.LOCAL_TARGET +import org.assertj.core.api.Java6Assertions.assertThat +import org.junit.Test + +class MavenPublishTargetTest { + + @Test + fun uploadArchivesTaskName() { + assertThat(MavenPublishTarget(DEFAULT_TARGET).taskName).isEqualTo("uploadArchives") + } + + @Test + fun installArchivesTaskName() { + assertThat(MavenPublishTarget(LOCAL_TARGET).taskName).isEqualTo("installArchives") + } + + @Test + fun customTaskName() { + assertThat(MavenPublishTarget("myRepo").taskName).isEqualTo("uploadArchivesMyRepo") + } +}