This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
NexusPlugin.groovy
224 lines (187 loc) · 8.99 KB
/
NexusPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bmuschko.gradle.nexus
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.maven.MavenDeployment
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.plugins.MavenPlugin
import org.gradle.api.tasks.Upload
import org.gradle.plugins.signing.SigningPlugin
/**
* <p>A {@link Plugin} that provides task for configuring and uploading artifacts to Sonatype Nexus.</p>
*
* @author Benjamin Muschko
*/
class NexusPlugin implements Plugin<Project> {
static final String NEXUS_USERNAME = 'nexusUsername'
static final String NEXUS_PASSWORD = 'nexusPassword'
static final String SIGNING_KEY_ID = 'signing.keyId'
static final String SIGNING_KEYRING = 'signing.secretKeyRingFile'
static final String SIGNING_PASSWORD = 'signing.password'
@Override
void apply(Project project) {
project.plugins.apply(ExtraArchivePlugin)
project.plugins.apply(MavenPlugin)
project.plugins.apply(SigningPlugin)
NexusPluginExtension extension = project.extensions.create('nexus', NexusPluginExtension)
configureTasks(project, extension)
configureSigning(project, extension)
configurePom(project, extension)
configureUpload(project, extension)
}
private void configureTasks(Project project, NexusPluginExtension extension) {
project.afterEvaluate {
changeInstallTaskConfiguration(project, extension)
addArchiveTaskToOutgoingArtifacts(project, extension, ExtraArchivePlugin.SOURCES_JAR_TASK_NAME)
addArchiveTaskToOutgoingArtifacts(project, extension, ExtraArchivePlugin.TESTS_JAR_TASK_NAME)
addArchiveTaskToOutgoingArtifacts(project, extension, ExtraArchivePlugin.JAVADOC_JAR_TASK_NAME)
}
}
private void changeInstallTaskConfiguration(Project project, NexusPluginExtension extension) {
if(!extension.usesStandardConfiguration()) {
project.tasks.getByName(MavenPlugin.INSTALL_TASK_NAME).configuration = project.configurations[extension.configuration]
}
}
private void addArchiveTaskToOutgoingArtifacts(Project project, NexusPluginExtension extension, String taskName) {
Task archiveTask = project.tasks.findByName(taskName)
if(archiveTask) {
project.artifacts.add(extension.configuration, archiveTask)
}
}
private void configureSigning(Project project, NexusPluginExtension extension) {
project.afterEvaluate {
if(extension.sign) {
project.signing {
required {
// Gradle allows project.version to be of type Object and always uses the toString() representation.
project.gradle.taskGraph.hasTask(extension.getUploadTaskPath(project)) && !project.version.toString().endsWith('SNAPSHOT')
}
sign project.configurations[extension.configuration]
project.gradle.taskGraph.whenReady {
if(project.signing.required) {
getPrivateKeyForSigning(project)
}
signPomForUpload(project, extension)
signInstallPom(project, extension)
}
}
}
}
}
private void getPrivateKeyForSigning(Project project) {
if (!project.hasProperty(SIGNING_KEY_ID)) {
throw new GradleException("A GnuPG key ID is required for signing. Please set $SIGNING_KEY_ID=xxxxxxxx in <USER_HOME>/.gradle/gradle.properties.")
}
String signingKeyId = project.property(SIGNING_KEY_ID)
File keyringFile = project.hasProperty(SIGNING_KEYRING) ?
project.file(project.property(SIGNING_KEYRING)) :
new File(new File(System.getProperty('user.home'), '.gnupg'), 'secring.gpg')
if(keyringFile.exists()) {
project.ext.set(SIGNING_KEYRING, keyringFile.getPath())
}
else {
throw new GradleException("GnuPG secret key file $keyringFile not found. Please set $SIGNING_KEYRING=/path/to/file.gpg in <USER_HOME>/.gradle/gradle.properties.")
}
Console console = System.console()
console.printf "\nThis release $project.version will be signed with your GnuPG key $signingKeyId in $keyringFile.\n"
if(!project.hasProperty(SIGNING_PASSWORD)) {
String password = new String(console.readPassword('Please enter your passphrase to unlock the secret key: '))
project.ext.set(SIGNING_PASSWORD, password)
}
}
private void signPomForUpload(Project project, NexusPluginExtension extension) {
def uploadTasks = project.tasks.withType(Upload).matching { it.path == extension.getUploadTaskPath(project) }
uploadTasks.each { task ->
task.repositories.mavenDeployer() {
beforeDeployment { MavenDeployment deployment ->
project.signing.signPom(deployment)
}
}
}
}
private void signInstallPom(Project project, NexusPluginExtension extension) {
def installTasks = project.tasks.withType(Upload).matching { it.path == extension.getInstallTaskPath(project) }
installTasks.each { task ->
task.repositories.mavenInstaller() {
beforeDeployment { MavenDeployment deployment ->
project.signing.signPom(deployment)
}
}
}
}
private void configurePom(Project project, NexusPluginExtension extension) {
project.ext.modifyPom = { Closure modification ->
project.afterEvaluate {
project.poms.each {
it.whenConfigured { project.configure(it, modification) }
}
}
}
createPomsProjectProperty(project, extension)
}
private void createPomsProjectProperty(Project project, NexusPluginExtension extension) {
project.afterEvaluate {
project.ext.poms = []
Task installTask = project.tasks.findByPath(MavenPlugin.INSTALL_TASK_NAME)
if (installTask) {
project.ext.poms << installTask.repositories.mavenInstaller().pom
}
project.ext.poms << project.tasks.getByName(extension.uploadTaskName).repositories.mavenDeployer().pom
}
}
private void configureUpload(Project project, NexusPluginExtension extension) {
project.afterEvaluate {
project.tasks.getByName(extension.uploadTaskName).repositories.mavenDeployer() {
project.gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
if(taskGraph.hasTask(extension.getUploadTaskPath(project))) {
ConsoleHandler consoleHandler = new ConsoleHandler()
String nexusUsername = project.hasProperty(NEXUS_USERNAME) ?
project.property(NEXUS_USERNAME) :
consoleHandler.askForUsername()
String nexusPassword = project.hasProperty(NEXUS_PASSWORD) ?
project.property(NEXUS_PASSWORD) :
consoleHandler.askForPassword()
if(extension.repositoryUrl) {
repository(url: extension.repositoryUrl) {
authentication(userName: nexusUsername, password: nexusPassword)
}
}
if(extension.snapshotRepositoryUrl) {
snapshotRepository(url: extension.snapshotRepositoryUrl) {
authentication(userName: nexusUsername, password: nexusPassword)
}
}
}
}
}
}
}
private class ConsoleHandler {
Console console
ConsoleHandler() {
console = System.console()
}
String askForUsername() {
console ? console.readLine('\nPlease specify username: ') : null
}
String askForPassword() {
console ? new String(console.readPassword('\nPlease specify password: ')) : null
}
}
}