@@ -24,155 +24,107 @@ dependencies {
24
24
javadocSources sourceSets. main. allJava
25
25
}
26
26
27
+ java {
28
+ // Configure the Java "software component" to include javadoc and sources jars in addition to the classes jar.
29
+ // Ultimately, this component is what makes up the publication for this project.
30
+ withJavadocJar()
31
+ withSourcesJar()
32
+ }
33
+
27
34
28
35
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29
36
// Publishing
30
37
31
- publishing {
32
- publications {
33
- // main publication
34
- publishedArtifacts {
35
- // Add the Java component to the main publication
36
- from components. java
37
- }
38
+ var publishingExtension = project. getExtensions(). getByType(PublishingExtension ) as PublishingExtension
39
+ publishingExtension. publications {
40
+ // main publication
41
+ publishedArtifacts {
42
+ // Add the Java component to the main publication
43
+ from components. java
38
44
}
39
45
}
40
46
41
- java {
42
- // include javadoc and sources jar in the Java component
43
- // - classes jar included by default
44
- withJavadocJar()
45
- withSourcesJar()
46
- }
47
47
48
- var signingKey = resolveSigningKey()
49
- var signingPassword = findSigningProperty( " RELEASE_GPG_PASSPHRASE " )
48
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
+ // Signing
50
50
51
- signing {
52
- useInMemoryPgpKeys( signingKey, signingPassword )
51
+ def signPublicationsTask = tasks . register( ' signPublications ' ) {
52
+ description " Grouping task which executes all Sign tasks "
53
53
54
- sign publishing . publications . publishedArtifacts
54
+ dependsOn tasks . withType( Sign )
55
55
}
56
56
57
- String resolveSigningKey () {
58
- var key = findSigningProperty( " RELEASE_GPG_PRIVATE_KEY" )
59
- if ( key != null ) {
60
- return key
61
- }
62
-
63
- var keyFile = findSigningProperty( " RELEASE_GPG_PRIVATE_KEY_PATH" )
64
- if ( keyFile != null ) {
65
- return new File ( keyFile ). text
66
- }
57
+ tasks. named( " publishPublishedArtifactsPublicationToSonatypeRepository" ) {
58
+ // publishing depends on signing
59
+ dependsOn signPublicationsTask
60
+ }
67
61
68
- return null
62
+ tasks. register(' sign' ) {
63
+ description " Pseudonym for :signPublications"
64
+ dependsOn signPublicationsTask
69
65
}
70
66
71
- String findSigningProperty (String propName ) {
72
- def sysProp = System . getProperty(propName)
73
- if ( sysProp != null ) {
74
- logger. debug " Found `{}` as a system property" , propName
75
- return sysProp
76
- }
67
+ var signingExtension = project. getExtensions(). getByType(SigningExtension ) as SigningExtension
77
68
78
- def envVar = System . getenv(). get(propName)
79
- if ( envVar != null ) {
80
- logger. debug " Found `{}` as an env-var property" , propName
81
- return envVar
82
- }
69
+ gradle. taskGraph. whenReady { TaskExecutionGraph graph ->
70
+ boolean wasSigningRequested = false
71
+ boolean wasPublishingRequested = false
83
72
84
- def projectProp = project. hasProperty(propName)
85
- if (projectProp) {
86
- logger. debug " Found `{}` as a project property" , propName
87
- return projectProp
73
+ graph. allTasks. each {task ->
74
+ if ( task instanceof Sign ) {
75
+ wasSigningRequested = true
76
+ }
77
+ else if ( task instanceof PublishToMavenRepository ) {
78
+ wasPublishingRequested = true
79
+ }
88
80
}
89
81
90
- logger. debug " Did not find `{}`" , propName
91
- return null
92
- }
93
-
94
-
95
- var signingTask = project. tasks. getByName( " signPublishedArtifactsPublication" ) as Sign
96
- var signingExtension = project. getExtensions(). getByType(SigningExtension ) as SigningExtension
82
+ if ( wasPublishingRequested ) {
83
+ def ossrhUser = System . getenv(). get( " ORG_GRADLE_PROJECT_sonatypeUsername" )
84
+ def ossrhPass = System . getenv(). get( " ORG_GRADLE_PROJECT_sonatypePassword" )
85
+ if ( ossrhUser == null || ossrhPass == null ) {
86
+ throw new RuntimeException ( " Cannot perform publishing to OSSRH without credentials." )
87
+ }
88
+ logger. lifecycle " Publishing groupId: '" + project. group + " ', version: '" + project. version + " '"
89
+ }
97
90
98
- task sign {
99
- dependsOn " signPublications"
100
- }
91
+ if ( wasSigningRequested || wasPublishingRequested ) {
92
+ // signing was explicitly requested and/or we are publishing to Sonatype OSSRH
93
+ // - we need the signing to happen
94
+ signingExtension. required = true
101
95
102
- task signPublications { t ->
103
- tasks. withType( Sign ). all { s ->
104
- t. dependsOn s
96
+ var signingKey = resolveSigningKey()
97
+ var signingPassword = resolveSigningPassphrase()
98
+ signingExtension. useInMemoryPgpKeys( signingKey, signingPassword )
99
+ signingExtension. sign publishing. publications. publishedArtifacts
105
100
}
106
- }
107
-
108
- signingTask. doFirst {
109
- if ( signingKey == null || signingPassword == null ) {
110
- throw new GradleException (
111
- " Cannot perform signing without GPG details. Please set the `signingKey` and `signingKeyFile` properties"
112
- )
101
+ else {
102
+ // signing was not explicitly requested and we are not publishing to OSSRH,
103
+ // - disable all Sign tasks
104
+ tasks. withType( Sign ). each { enabled = false }
113
105
}
114
106
}
115
107
116
-
117
- boolean wasSigningExplicitlyRequested () {
118
- // check whether signing task was explicitly requested when running the build
119
- //
120
- // NOTE: due to https://discuss.gradle.org/t/how-to-tell-if-a-task-was-explicitly-asked-for-on-the-command-line/42853/3
121
- // we cannot definitively know whether the task was requested. Gradle really just does not expose this information.
122
- // so we make a convention - we check the "start parameters" object to see which task-names were requested;
123
- // the problem is that these are the raw names directly from the command line. e.g. it is perfectly legal to
124
- // say `gradlew signPubArtPub` in place of `gradlew signPublishedArtifactsPublication` - Gradle will simply
125
- // "expand" the name it finds. However, it does not make that available.
126
- //
127
- // so the convention is that we will check for the following task names
128
- //
129
- // for each of:
130
- // 1. `sign`
131
- // 2. `signPublications`
132
- // 3. `signPublishedArtifactsPublication`
133
- //
134
- // and we check both forms:
135
- // 1. "${taskName}"
136
- // 2. project.path + ":${taskName}"
137
- //
138
- // we need to check both again because of the "start parameters" discussion
139
-
140
- def signingTaskNames = [" sign" , " signPublications" , " signPublishedArtifactsPublication" ]
141
-
142
- for ( String taskName : signingTaskNames ) {
143
- if ( gradle. startParameter. taskNames. contains( taskName )
144
- || gradle. startParameter. taskNames. contains( " ${ project.path} :${ taskName} " ) ) {
145
- return true
146
- }
108
+ static String resolveSigningKey () {
109
+ var key = System . getenv(). get( " SIGNING_GPG_PRIVATE_KEY" )
110
+ if ( key != null ) {
111
+ return key
147
112
}
148
113
149
- return false
150
- }
114
+ var keyFile = System . getenv(). get( " SIGNING_GPG_PRIVATE_KEY_PATH" )
115
+ if ( keyFile != null ) {
116
+ return new File ( keyFile ). text
117
+ }
151
118
152
- if ( wasSigningExplicitlyRequested() ) {
153
- // signing was explicitly requested
154
- signingExtension. required = true
119
+ throw new RuntimeException ( " Cannot perform signing without GPG details." )
155
120
}
156
- else {
157
- gradle. taskGraph. whenReady { graph ->
158
- if ( graph. hasTask( signingTask ) ) {
159
- // signing is scheduled to happen.
160
- //
161
- // we know, from above if-check, that it was not explicitly requested -
162
- // so it is triggered via task dependency. make sure we want it to happen
163
- var publishingTask = project. tasks. getByName( " publishPublishedArtifactsPublicationToSonatypeRepository" ) as PublishToMavenRepository
164
- if ( graph. hasTask( publishingTask ) ) {
165
- // we are publishing to Sonatype OSSRH - we need the signing to happen
166
- signingExtension. required = true
167
- }
168
- else {
169
- // signing was not explicitly requested and we are not publishing to OSSRH,
170
- // so do not sign.
171
- signingTask. enabled = false
172
- }
173
- }
174
121
122
+ static String resolveSigningPassphrase () {
123
+ var passphrase = System . getenv(). get( " SIGNING_GPG_PASSPHRASE" )
124
+ if ( passphrase == null ) {
125
+ throw new RuntimeException ( " Cannot perform signing without GPG details." )
175
126
}
127
+ return passphrase
176
128
}
177
129
178
130
0 commit comments