Skip to content

Commit

Permalink
improve when signing tasks run, consider all targets for signing (#30)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gabrielittner authored Oct 9, 2018
1 parent 4b908e6 commit 7bed96e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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
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
Expand All @@ -25,21 +27,33 @@ class MavenPublishPlugin implements Plugin<Project> {
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<MavenPublishTarget> 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')) {
Expand Down Expand Up @@ -119,13 +133,13 @@ class MavenPublishPlugin implements Plugin<Project> {
}
}

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")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
/**
Expand Down Expand Up @@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 7bed96e

Please sign in to comment.