Minor changes to signing and build logging cleanup

This commit is contained in:
Steve Ebersole 2022-05-11 20:36:48 -05:00 committed by Christian Beikov
parent 8494be4fe4
commit 3caa5aa44f
2 changed files with 59 additions and 18 deletions

View File

@ -122,33 +122,37 @@ String resolveSigningKey() {
String findSigningProperty(String propName) { String findSigningProperty(String propName) {
if ( System.getProperty( propName ) != null ) { if ( System.getProperty( propName ) != null ) {
logger.lifecycle "Found `{}` as a system property", propName logger.debug "Found `{}` as a system property", propName
return System.getProperty(propName ) return System.getProperty(propName )
} }
else if ( System.getenv().get( propName ) != null ) { else if ( System.getenv().get( propName ) != null ) {
logger.lifecycle "Found `{}` as an env-var property", propName logger.debug "Found `{}` as an env-var property", propName
return System.getenv().get( propName ); return System.getenv().get( propName )
} }
else if ( project.hasProperty( propName ) ) { else if ( project.hasProperty( propName ) ) {
logger.lifecycle "Found `{}` as a project property", propName logger.debug "Found `{}` as a project property", propName
return project.hasProperty( propName ) return project.hasProperty( propName )
} }
else { else {
logger.lifecycle "Did not find `{}`", propName logger.debug "Did not find `{}`", propName
return null return null
} }
} }
final publishTaskName = "publishPublishedArtifactsPublicationToSonatypeRepository" var signingTask = project.tasks.getByName( "signPublishedArtifactsPublication" ) as Sign
final signingTaskName = "signPublishedArtifactsPublication"
final signingTaskPath = project.path + ":" + signingTaskName
final signingExplicitlyRequested = gradle.startParameter.taskNames.contains( signingTaskName )
|| gradle.startParameter.taskNames.contains( signingTaskPath )
var signingTask = project.tasks.getByName( signingTaskName ) as Sign
var signingExtension = project.getExtensions().getByType(SigningExtension) as SigningExtension var signingExtension = project.getExtensions().getByType(SigningExtension) as SigningExtension
task sign {
dependsOn "signPublications"
}
task signPublications { t ->
tasks.withType( Sign ).all { s ->
t.dependsOn s
}
}
signingTask.doFirst { signingTask.doFirst {
if ( signingKey == null || signingPassword == null ) { if ( signingKey == null || signingPassword == null ) {
throw new GradleException( throw new GradleException(
@ -157,25 +161,61 @@ signingTask.doFirst {
} }
} }
if ( signingExplicitlyRequested ) {
boolean wasSigningExplicitlyRequested() {
// check whether signing task was explicitly requested when running the build
//
// NOTE: due to https://discuss.gradle.org/t/how-to-tell-if-a-task-was-explicitly-asked-for-on-the-command-line/42853/3
// we cannot definitively know whether the task was requested. Gradle really just does not expose this information.
// so we make a convention - we check the "start parameters" object to see which task-names were requested;
// the problem is that these are the raw names directly from the command line. e.g. it is perfectly legal to
// say `gradlew signPubArtPub` in place of `gradlew signPublishedArtifactsPublication` - Gradle will simply
// "expand" the name it finds. However, it does not make that available.
//
// so the convention is that we will check for the following task names
//
// for each of:
// 1. `sign`
// 2. `signPublications`
// 3. `signPublishedArtifactsPublication`
//
// and we check both forms:
// 1. "${taskName}"
// 2. project.path + ":${taskName}"
//
// we need to check both again because of the "start parameters" discussion
def signingTaskNames = ["sign", "signPublications", "signPublishedArtifactsPublication"]
for ( String taskName : signingTaskNames ) {
if ( gradle.startParameter.taskNames.contains( taskName )
|| gradle.startParameter.taskNames.contains( "${project.path}:${taskName}" ) ) {
return true
}
}
return false
}
if ( wasSigningExplicitlyRequested() ) {
// signing was explicitly requested // signing was explicitly requested
signingExtension.required = true signingExtension.required = true
} }
else { else {
gradle.taskGraph.whenReady { graph -> gradle.taskGraph.whenReady { graph ->
var publishingTask = project.tasks.getByName( publishTaskName ) as PublishToMavenRepository
if ( graph.hasTask( signingTask ) ) { if ( graph.hasTask( signingTask ) ) {
// signing is scheduled to happen. // signing is scheduled to happen.
// //
// we know, from above if-check, that it was not explicitly requested - // we know, from above if-check, that it was not explicitly requested -
// so it is triggered via task dependency. make sure we want it to happen // so it is triggered via task dependency. make sure we want it to happen
var publishingTask = project.tasks.getByName( "publishPublishedArtifactsPublicationToSonatypeRepository" ) as PublishToMavenRepository
if ( graph.hasTask( publishingTask ) ) { if ( graph.hasTask( publishingTask ) ) {
// we are publishing to Sonatype OSSRH - we need the signing to happen // we are publishing to Sonatype OSSRH - we need the signing to happen
signingExtension.required = true signingExtension.required = true
} }
else { else {
// signing was not explicitly requested and we are not publishign to OSSRH // signing was not explicitly requested and we are not publishing to OSSRH,
// so do not sign // so do not sign.
signingTask.enabled = false signingTask.enabled = false
} }
} }
@ -188,7 +228,7 @@ else {
// Release / publishing tasks // Release / publishing tasks
task ciBuild { task ciBuild {
dependsOn test, publishToSonatype dependsOn test, tasks.publishToSonatype
} }
tasks.release.dependsOn tasks.test, tasks.publishToSonatype tasks.release.dependsOn tasks.test, tasks.publishToSonatype

View File

@ -37,6 +37,7 @@ dependencies {
implementation gradleApi() implementation gradleApi()
// for Gradle // for Gradle
implementation jakartaLibs.inject implementation jakartaLibs.inject
implementation jakartaLibs.inject
implementation localGroovy() implementation localGroovy()
} }
@ -95,7 +96,7 @@ gradle.taskGraph.whenReady {
logger.info( "Task `{}` had null repository", t.path ) logger.info( "Task `{}` had null repository", t.path )
} }
else if ( t.repository.name == "sonatype" ) { else if ( t.repository.name == "sonatype" ) {
logger.lifecycle( "Disabling task `{}` because it publishes to Sonatype", t.path ) logger.debug( "Disabling task `{}` because it publishes to Sonatype", t.path )
t.enabled = false t.enabled = false
} }
} }