-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathAbstractScmMojo.java
294 lines (244 loc) · 10.2 KB
/
AbstractScmMojo.java
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package org.codehaus.mojo.build;
/**
* The MIT License
*
* Copyright (c) 2015 Codehaus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.CommandParameter;
import org.apache.maven.scm.CommandParameters;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.ScmTag;
import org.apache.maven.scm.command.info.InfoItem;
import org.apache.maven.scm.command.info.InfoScmResult;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.StringUtils;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
public abstract class AbstractScmMojo extends AbstractMojo {
/**
* @since 1.0-beta-5
*/
@Parameter(defaultValue = "${project.scm.connection}", alias = "readUrlScm", readonly = true)
protected String scmConnectionUrl;
/**
* @since 1.0-beta-5
*/
@Parameter(defaultValue = "${project.scm.developerConnection}", alias = "urlScm", readonly = true)
protected String scmDeveloperConnectionUrl;
/**
* @since 1.4
*/
@Parameter(defaultValue = "${project.scm.tag}", readonly = true)
protected String scmTag;
/**
* The username that is used when connecting to the SCM system.
*
* @since 1.0-beta-1
*/
@Parameter(property = "username")
protected String username;
/**
* The password that is used when connecting to the SCM system.
*
* @since 1.0-beta-1
*/
@Parameter(property = "password")
protected String password;
/**
* Issue SCM actions at this local directory
*
* @since 1.0-beta-1
*/
@Parameter(property = "maven.buildNumber.scmDirectory", defaultValue = "${basedir}")
protected File scmDirectory;
/**
* Max length of a revision id (GIT only)
*
* @since 1.1
*/
@Parameter(property = "maven.buildNumber.shortRevisionLength", defaultValue = "0")
protected int shortRevisionLength = 0;
/**
* Setting this value allows the build to continue even in the event of an SCM failure. The value set will be used
* as the revision string in the event of a failure to retrieve the revision it from the SCM.
*
* @since 1.0-beta-2
*/
@Parameter(property = "maven.buildNumber.revisionOnScmFailure")
protected String revisionOnScmFailure;
/**
* whether to retrieve the revision for the last commit, or the last revision of the repository.
*
* @since 1.0-beta-2
*/
@Parameter(property = "maven.buildNumber.useLastCommittedRevision", defaultValue = "false")
private boolean useLastCommittedRevision;
/**
* Whether to skip this execution.
*
* @since 1.3
*/
@Parameter(property = "maven.buildNumber.skip", defaultValue = "false")
protected boolean skip;
/**
* Maven Settings
*
* @since 1.4
*/
@Parameter(defaultValue = "${settings}", readonly = true)
protected Settings settings;
@Parameter(defaultValue = "${project}", required = true, readonly = true)
protected MavenProject project;
@Component
protected ScmManager scmManager;
/**
* Ignore error when scm url from pom is empty and replace by scm:scmProvider {@link #scmProvider}
* @since 3.2.1
*/
@Parameter(property = "maven.buildNumber.ignoreEmptyScmUrl", defaultValue = "false")
private boolean ignoreEmptyScmUrl;
/**
* When scm url is empty, scm provider is needed {@link #ignoreEmptyScmUrl}
* @since 3.2.1
*/
@Parameter(property = "maven.buildNumber.scmProvider", defaultValue = "git")
private String scmProvider;
/**
* Maven Security Dispatcher
*
* @since 1.4
*/
@Component(hint = "mng-4384")
private SecDispatcher securityDispatcher;
/**
* Load username password from settings.
*/
private void loadInfosFromSettings(ScmProviderRepositoryWithHost repo) {
if (username == null || password == null) {
String host = repo.getHost();
int port = repo.getPort();
if (port > 0) {
host += ":" + port;
}
Server server = this.settings.getServer(host);
if (server != null) {
setPasswordIfNotEmpty(repo, decrypt(server.getPassword(), host));
setUserIfNotEmpty(repo, server.getUsername());
}
}
}
private String decrypt(String str, String server) {
try {
return securityDispatcher.decrypt(str);
} catch (SecDispatcherException e) {
getLog().warn("Failed to decrypt password/passphrase for server " + server + ", using auth token as is");
return str;
}
}
protected ScmRepository getScmRepository() throws ScmException {
String repoUrl = !StringUtils.isBlank(this.scmConnectionUrl) ? scmConnectionUrl : scmDeveloperConnectionUrl;
ScmRepository repository = scmManager.makeScmRepository(
StringUtils.isBlank(repoUrl) ? (ignoreEmptyScmUrl ? "scm:" + scmProvider + ":" : "") : repoUrl);
ScmProviderRepository scmRepo = repository.getProviderRepository();
if (scmRepo instanceof ScmProviderRepositoryWithHost) {
loadInfosFromSettings((ScmProviderRepositoryWithHost) scmRepo);
}
setPasswordIfNotEmpty(scmRepo, password);
setUserIfNotEmpty(scmRepo, username);
return repository;
}
private void setPasswordIfNotEmpty(ScmProviderRepository repository, String password) {
if (!StringUtils.isEmpty(password)) {
repository.setPassword(password);
}
}
private void setUserIfNotEmpty(ScmProviderRepository repository, String user) {
if (!StringUtils.isEmpty(user)) {
repository.setUser(user);
}
}
protected void checkResult(ScmResult result) throws ScmException {
if (!result.isSuccess()) {
// TODO: improve error handling
getLog().error("Provider message:");
getLog().error(result.getProviderMessage());
getLog().error("Command output:");
getLog().error(result.getCommandOutput());
throw new ScmException("Error!");
}
}
// TODO this should be rolled into org.apache.maven.scm.provider.ScmProvider and
// org.apache.maven.scm.provider.svn.SvnScmProvider
/**
* Get info from scm.
*
* @param repository
* @param fileSet
* @return
* @throws ScmException
*/
protected InfoScmResult info(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
CommandParameters commandParameters = new CommandParameters();
// only for Git, we will make a test for shortRevisionLength parameter
if (GitScmProviderRepository.PROTOCOL_GIT.equals(
scmManager.getProviderByRepository(repository).getScmType())
&& this.shortRevisionLength > 0) {
getLog().info("ShortRevision tag detected. The value is '" + this.shortRevisionLength + "'.");
if (shortRevisionLength >= 0 && shortRevisionLength < 4) {
getLog().warn(
"shortRevision parameter less then 4. ShortRevisionLength is relaying on 'git rev-parese --short=LENGTH' command, accordingly to Git rev-parse specification the LENGTH value is miminum 4. ");
}
commandParameters.setInt(CommandParameter.SCM_SHORT_REVISION_LENGTH, this.shortRevisionLength);
}
if (!StringUtils.isBlank(scmTag) && !"HEAD".equals(scmTag)) {
commandParameters.setScmVersion(CommandParameter.SCM_VERSION, new ScmTag(scmTag));
}
return scmManager
.getProviderByRepository(repository)
.info(repository.getProviderRepository(), fileSet, commandParameters);
}
protected String getScmRevision() throws ScmException {
ScmRepository repository = getScmRepository();
InfoScmResult scmResult = info(repository, new ScmFileSet(scmDirectory));
if (scmResult == null || scmResult.getInfoItems().isEmpty()) {
return (!StringUtils.isEmpty(revisionOnScmFailure)) ? revisionOnScmFailure : null;
}
checkResult(scmResult);
InfoItem info = scmResult.getInfoItems().get(0);
if (useLastCommittedRevision) {
// It seemed the case that for git getLastChangedRevision()
// returns null instead of the last revision in contradiction
// to the above code: scmResult.getInfoItems().get(0);
return info.getLastChangedRevision();
}
return info.getRevision();
}
}