Skip to content

Commit b27d9d8

Browse files
sebersoleDavideD
authored andcommitted
[#1095] Sign the artifacts for Sonatype
1 parent 8e70e3a commit b27d9d8

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

publish.gradle

+31-50
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,19 @@ publishing {
8282

8383
var signingExtension = project.getExtensions().getByType(SigningExtension) as SigningExtension
8484

85-
def signPublicationsTask = tasks.register('signPublications') {
86-
description "Grouping task which executes all Sign tasks"
87-
dependsOn tasks.withType( Sign )
88-
}
85+
var publishingExtension = project.getExtensions().getByType(PublishingExtension) as PublishingExtension
86+
signingExtension.sign publishingExtension.publications.publishedArtifacts
8987

90-
tasks.named( "publishPublishedArtifactsPublicationToSonatypeRepository" ) {
91-
// publishing depends on signing
92-
dependsOn signPublicationsTask
93-
}
88+
var signingKey = resolveSigningKey()
89+
var signingPassphrase = resolveSigningPassphrase()
90+
signingExtension.useInMemoryPgpKeys(signingKey, signingPassphrase)
9491

9592
gradle.taskGraph.whenReady { TaskExecutionGraph graph ->
96-
boolean wasSigningRequested = false
9793
boolean wasPublishingRequested = false
9894

9995
graph.allTasks.each {task ->
100-
if ( task instanceof Sign ) {
101-
wasSigningRequested = true
102-
}
103-
else if ( task instanceof PublishToMavenRepository ) {
96+
if ( task instanceof PublishToMavenRepository ) {
97+
logger.lifecycle( "Found PublishToMavenRepository task : {}", task.path )
10498
wasPublishingRequested = true
10599
}
106100
}
@@ -111,24 +105,34 @@ gradle.taskGraph.whenReady { TaskExecutionGraph graph ->
111105
if ( publishUser == null || publishPass == null ) {
112106
throw new RuntimeException( "Cannot perform publishing to OSSRH without credentials." )
113107
}
114-
logger.lifecycle "Publishing groupId: '" + project.group + "', version: '" + project.version + "'"
115-
}
116108

117-
if ( wasSigningRequested || wasPublishingRequested ) {
118-
// signing was explicitly requested and/or we are publishing to Sonatype OSSRH
119-
// - we need the signing to happen
109+
logger.lifecycle "Publishing {} : {} : {}", project.group, project.name, project.version
110+
111+
// require signing if publishing to OSSRH
120112
signingExtension.required = true
113+
}
114+
else if ( signingKey == null || signingPassphrase == null ) {
115+
tasks.withType( Sign ).each { t-> t.enabled = false }
116+
}
117+
}
118+
121119

122-
var signingKey = resolveSigningKey()
123-
var signingPassword = resolveSigningPassphrase()
124-
signingExtension.useInMemoryPgpKeys( signingKey, signingPassword )
125-
signingExtension.sign publishing.publications.publishedArtifacts
120+
static String resolveSigningKey() {
121+
var key = System.getenv().get( "SIGNING_GPG_PRIVATE_KEY" )
122+
if ( key != null ) {
123+
return key
126124
}
127-
else {
128-
// signing was not explicitly requested and we are not publishing to OSSRH,
129-
// - disable all Sign tasks
130-
tasks.withType( Sign ).each { enabled = false }
125+
126+
var keyFile = System.getenv().get( "SIGNING_GPG_PRIVATE_KEY_PATH" )
127+
if ( keyFile != null ) {
128+
return new File( keyFile ).text
131129
}
130+
131+
return null
132+
}
133+
134+
static String resolveSigningPassphrase() {
135+
return System.getenv().get( "SIGNING_GPG_PASSPHRASE" )
132136
}
133137

134138
String resolvePublishUser() {
@@ -164,27 +168,4 @@ String projectPropOrNull(String name) {
164168
return project.findProperty( name )
165169
}
166170
return null;
167-
}
168-
169-
170-
static String resolveSigningKey() {
171-
var key = System.getenv().get( "SIGNING_GPG_PRIVATE_KEY" )
172-
if ( key != null ) {
173-
return key
174-
}
175-
176-
var keyFile = System.getenv().get( "SIGNING_GPG_PRIVATE_KEY_PATH" )
177-
if ( keyFile != null ) {
178-
return new File( keyFile ).text
179-
}
180-
181-
throw new RuntimeException( "Cannot perform signing without GPG details." )
182-
}
183-
184-
static String resolveSigningPassphrase() {
185-
var passphrase = System.getenv().get( "SIGNING_GPG_PASSPHRASE" )
186-
if ( passphrase == null ) {
187-
throw new RuntimeException( "Cannot perform signing without GPG details." )
188-
}
189-
return passphrase
190-
}
171+
}

0 commit comments

Comments
 (0)