1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.release;
20
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Arrays;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.plugins.annotations.ResolutionScope;
31 import org.apache.maven.shared.release.DefaultReleaseManagerListener;
32 import org.apache.maven.shared.release.ReleaseExecutionException;
33 import org.apache.maven.shared.release.ReleaseFailureException;
34 import org.apache.maven.shared.release.ReleasePrepareRequest;
35 import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
36 import org.codehaus.plexus.configuration.PlexusConfiguration;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @Mojo(name = "prepare", aggregator = true, requiresDependencyCollection = ResolutionScope.TEST)
52 public class PrepareReleaseMojo extends AbstractScmReleaseMojo {
53
54
55
56
57 @Parameter(defaultValue = "true", property = "resume")
58 private boolean resume;
59
60
61
62
63 @Deprecated
64 @Parameter(defaultValue = "false", property = "generateReleasePoms")
65 private boolean generateReleasePoms;
66
67
68
69
70 @Parameter(defaultValue = "false", property = "useEditMode")
71 private boolean useEditMode;
72
73
74
75
76
77
78 @Parameter(defaultValue = "true", property = "updateDependencies")
79 private boolean updateDependencies;
80
81
82
83
84
85
86
87 @Parameter(defaultValue = "false", property = "autoVersionSubmodules")
88 private boolean autoVersionSubmodules;
89
90
91
92
93
94
95
96 @Parameter(defaultValue = "false", property = "dryRun")
97 private boolean dryRun;
98
99
100
101
102 @Parameter(defaultValue = "true", property = "addSchema")
103 private boolean addSchema;
104
105
106
107
108
109 @Parameter(property = "preparationProfiles")
110 private String preparationProfiles;
111
112
113
114
115 @Parameter(defaultValue = "clean verify", property = "preparationGoals")
116 private String preparationGoals;
117
118
119
120
121
122
123
124 @Parameter(defaultValue = "", property = "completionGoals")
125 private String completionGoals;
126
127
128
129
130
131
132 @Parameter(defaultValue = "false", property = "commitByProject")
133 private boolean commitByProject;
134
135
136
137
138
139
140 @Parameter(defaultValue = "false", property = "ignoreSnapshots")
141 private boolean allowTimestampedSnapshots;
142
143
144
145
146
147
148
149 @Parameter(defaultValue = "false", property = "allowReleasePluginSnapshot", readonly = true)
150 private boolean allowReleasePluginSnapshot;
151
152
153
154
155
156
157
158 @Parameter
159 private String[] checkModificationExcludes;
160
161
162
163
164
165
166 @Parameter(property = "checkModificationExcludeList")
167 private String checkModificationExcludeList;
168
169
170
171
172
173
174 @Parameter(property = "releaseVersion")
175 private String releaseVersion;
176
177
178
179
180
181
182 @Parameter(property = "developmentVersion")
183 private String developmentVersion;
184
185
186
187
188
189
190
191
192
193
194
195
196 @Parameter(defaultValue = "true", property = "remoteTagging")
197 private boolean remoteTagging;
198
199
200
201
202
203
204 @Parameter(property = "signTag")
205 private boolean signTag = false;
206
207
208
209
210
211
212 @Parameter(defaultValue = "true", property = "updateWorkingCopyVersions")
213 private boolean updateWorkingCopyVersions;
214
215
216
217
218
219
220
221
222
223
224
225 @Parameter(defaultValue = "false", property = "suppressCommitBeforeTag")
226 private boolean suppressCommitBeforeTag;
227
228
229
230
231
232
233
234
235 @Parameter(defaultValue = "0", property = "waitBeforeTagging")
236 private int waitBeforeTagging;
237
238
239
240
241
242
243
244
245 @Parameter(defaultValue = "default", property = "projectVersionPolicyId")
246 private String projectVersionPolicyId;
247
248
249
250
251
252
253 @Parameter(property = "projectVersionPolicyConfig")
254 private PlexusConfiguration projectVersionPolicyConfig;
255
256
257
258
259
260
261
262
263 @Parameter(property = "projectNamingPolicyId")
264 private String projectTagNamingPolicyId;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 @Parameter(defaultValue = "@{prefix} prepare release @{releaseLabel}", property = "scmReleaseCommitComment")
283 private String scmReleaseCommitComment = "@{prefix} prepare release @{releaseLabel}";
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 @Parameter(
302 defaultValue = "@{prefix} prepare for next development iteration",
303 property = "scmDevelopmentCommitComment")
304 private String scmDevelopmentCommitComment = "@{prefix} prepare for next development iteration";
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 @Parameter(property = "autoResolveSnapshots")
322 private String autoResolveSnapshots;
323
324
325
326
327
328
329
330 @Parameter(defaultValue = "false", property = "pinExternals")
331 private boolean pinExternals;
332
333
334
335
336 @Override
337 protected String getAdditionalProfiles() {
338 return preparationProfiles;
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 @Parameter(defaultValue = "source", property = "lineSeparator")
355 private String lineSeparator;
356
357
358
359
360 @Override
361 public void execute() throws MojoExecutionException, MojoFailureException {
362 if (generateReleasePoms) {
363 throw new MojoFailureException("Generating release POMs is no longer supported in release:prepare. "
364 + "Please run release:prepare-with-pom instead.");
365 }
366
367 prepareRelease(generateReleasePoms);
368 }
369
370
371
372
373
374
375
376
377 protected void prepareRelease(boolean generateReleasePoms) throws MojoExecutionException, MojoFailureException {
378
379
380 super.execute();
381
382 final ReleaseDescriptorBuilder config = createReleaseDescriptor();
383 config.setAddSchema(addSchema);
384 config.setGenerateReleasePoms(generateReleasePoms);
385 config.setScmUseEditMode(useEditMode);
386 config.setPreparationGoals(preparationGoals);
387 config.setCompletionGoals(completionGoals);
388 config.setCommitByProject(commitByProject);
389 config.setUpdateDependencies(updateDependencies);
390 config.setAutoVersionSubmodules(autoVersionSubmodules);
391 config.setAllowTimestampedSnapshots(allowTimestampedSnapshots);
392 config.setSnapshotReleasePluginAllowed(allowReleasePluginSnapshot);
393 config.setDefaultReleaseVersion(releaseVersion);
394 config.setDefaultDevelopmentVersion(developmentVersion);
395 config.setRemoteTagging(remoteTagging);
396 config.setScmSignTags(signTag);
397 config.setUpdateWorkingCopyVersions(updateWorkingCopyVersions);
398 config.setSuppressCommitBeforeTagOrBranch(suppressCommitBeforeTag);
399 config.setWaitBeforeTagging(waitBeforeTagging);
400 config.setProjectVersionPolicyId(projectVersionPolicyId);
401 if (projectVersionPolicyConfig != null) {
402 config.setProjectVersionPolicyConfig(projectVersionPolicyConfig.toString());
403 }
404 config.setProjectNamingPolicyId(projectTagNamingPolicyId);
405 config.setScmDevelopmentCommitComment(scmDevelopmentCommitComment);
406 config.setScmReleaseCommitComment(scmReleaseCommitComment);
407 config.setAutoResolveSnapshots(autoResolveSnapshots);
408 config.setPinExternals(pinExternals);
409 config.setLineSeparator(resolveLineSeparator());
410
411 if (checkModificationExcludeList != null) {
412 checkModificationExcludes =
413 checkModificationExcludeList.replaceAll("\\s", "").split(",");
414 }
415
416 if (checkModificationExcludes != null) {
417 config.setCheckModificationExcludes(Arrays.asList(checkModificationExcludes));
418 }
419
420 ReleasePrepareRequest prepareRequest = new ReleasePrepareRequest();
421 prepareRequest.setReleaseDescriptorBuilder(config);
422 prepareRequest.setReleaseEnvironment(getReleaseEnvironment());
423 prepareRequest.setReactorProjects(getReactorProjects());
424 prepareRequest.setReleaseManagerListener(new DefaultReleaseManagerListener(getLog(), dryRun));
425 prepareRequest.setResume(resume);
426 prepareRequest.setDryRun(dryRun);
427 prepareRequest.setUserProperties(session.getUserProperties());
428
429 try {
430 releaseManager.prepare(prepareRequest);
431 } catch (ReleaseExecutionException e) {
432 throw new MojoExecutionException(e.getMessage(), e);
433 } catch (ReleaseFailureException e) {
434 throw new MojoFailureException(e.getMessage(), e);
435 }
436 }
437
438 private String resolveLineSeparator() throws MojoExecutionException {
439 if (lineSeparator == null) {
440 return getLineSeparatorFromPom();
441 }
442
443 switch (lineSeparator) {
444 case "lf":
445 return "\n";
446 case "cr":
447 return "\r";
448 case "crlf":
449 return "\r\n";
450 case "system":
451 return System.lineSeparator();
452 case "source":
453 return getLineSeparatorFromPom();
454 default:
455 throw new IllegalArgumentException(String.format(
456 "Unknown property lineSeparator: '%s'. Use one of"
457 + " the following: 'source', 'system', 'lf', 'cr', 'crlf'.",
458 lineSeparator));
459 }
460 }
461
462 private String getLineSeparatorFromPom() throws MojoExecutionException {
463 char current;
464 String lineSeparator = "";
465 try (InputStream is = new FileInputStream(this.project.getFile())) {
466 while (is.available() > 0) {
467 current = (char) is.read();
468 if ((current == '\n') || (current == '\r')) {
469 lineSeparator += current;
470 if (is.available() > 0) {
471 char next = (char) is.read();
472 if ((next != current) && ((next == '\r') || (next == '\n'))) {
473 lineSeparator += next;
474 }
475 }
476 return lineSeparator;
477 }
478 }
479 } catch (IOException e) {
480 throw new MojoExecutionException("Failed to detect line separator of " + this.project.getFile(), e);
481 }
482
483 return lineSeparator;
484 }
485 }