View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.xml.XmlPlexusConfiguration;
37  
38  /**
39   * Prepare for a release in SCM. Steps through several phases to ensure the POM is ready to be released and then
40   * prepares SCM to eventually contain a tagged version of the release and a record in the local copy of the parameters
41   * used. This can be followed by a call to <code>release:perform</code>. For more info see <a
42   * href="https://maven.apache.org/plugins/maven-release-plugin/usage/prepare-release.html"
43   * >https://maven.apache.org/plugins/maven-release-plugin/usage/prepare-release.html</a>.
44   *
45   * @author <a href="mailto:jdcasey@apache.org">John Casey</a>
46   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
47   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
48   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
49   */
50  // TODO [!] check how this works with version ranges
51  @Mojo(name = "prepare", aggregator = true, requiresDependencyCollection = ResolutionScope.TEST)
52  public class PrepareReleaseMojo extends AbstractScmReleaseMojo {
53  
54      /**
55       * Resume a previous release attempt from the point where it was stopped.
56       */
57      @Parameter(defaultValue = "true", property = "resume")
58      private boolean resume;
59  
60      /**
61       * @deprecated Please use release:prepare-with-pom instead.
62       */
63      @Deprecated
64      @Parameter(defaultValue = "false", property = "generateReleasePoms")
65      private boolean generateReleasePoms;
66  
67      /**
68       * Whether to use "edit" mode on the SCM, to lock the file for editing during SCM operations.
69       */
70      @Parameter(defaultValue = "false", property = "useEditMode")
71      private boolean useEditMode;
72  
73      /**
74       * Whether to update dependencies version to the next development version.
75       *
76       * @since 2.0-beta-5
77       */
78      @Parameter(defaultValue = "true", property = "updateDependencies")
79      private boolean updateDependencies;
80  
81      /**
82       * Whether to automatically assign submodules the parent version. If set to false, the user will be prompted for the
83       * version of each submodules.
84       *
85       * @since 2.0-beta-5
86       */
87      @Parameter(defaultValue = "false", property = "autoVersionSubmodules")
88      private boolean autoVersionSubmodules;
89  
90      /**
91       * Dry run: don't checkin or tag anything in the scm repository, or modify the checkout. Running
92       * <code>mvn -DdryRun=true release:prepare</code> is useful in order to check that modifications to poms and scm
93       * operations (only listed on the console) are working as expected. Modified POMs are written alongside the
94       * originals without modifying them.
95       */
96      @Parameter(defaultValue = "false", property = "dryRun")
97      private boolean dryRun;
98  
99      /**
100      * Whether to add a schema to the POM if it was previously missing on release.
101      */
102     @Parameter(defaultValue = "true", property = "addSchema")
103     private boolean addSchema;
104 
105     /**
106      * Comma separated profiles to enable on release prepare, in addition to active profiles for project execution.
107      */
108     @Parameter(property = "preparationProfiles")
109     private String preparationProfiles;
110 
111     /**
112      * Goals to run as part of the preparation step, after transformation but before committing. Space delimited.
113      */
114     @Parameter(defaultValue = "clean verify", property = "preparationGoals")
115     private String preparationGoals;
116 
117     /**
118      * Goals to run on completion of the preparation step, after transformation back to the next development version but
119      * before committing. Space delimited.
120      *
121      * @since 2.2
122      */
123     @Parameter(defaultValue = "", property = "completionGoals")
124     private String completionGoals;
125 
126     /**
127      * Commits to do are atomic or by project.
128      *
129      * @since 2.0-beta-5
130      */
131     @Parameter(defaultValue = "false", property = "commitByProject")
132     private boolean commitByProject;
133 
134     /**
135      * Whether to allow timestamped SNAPSHOT dependencies. Default is to fail when finding any SNAPSHOT.
136      *
137      * @since 2.0-beta-7
138      */
139     @Parameter(defaultValue = "false", property = "ignoreSnapshots")
140     private boolean allowTimestampedSnapshots;
141 
142     /**
143      * Whether to allow usage of a SNAPSHOT version of the Release Plugin. This in an internal property used to support
144      * testing of the plugin itself in batch mode.
145      *
146      * @since 2.0-beta-9
147      */
148     @Parameter(defaultValue = "false", property = "allowReleasePluginSnapshot", readonly = true)
149     private boolean allowReleasePluginSnapshot;
150 
151     /**
152      * A list of additional exclude filters that will be skipped when checking for modifications on the working copy. Is
153      * ignored, when checkModificationExcludes is set.
154      *
155      * @since 2.1
156      */
157     @Parameter
158     private String[] checkModificationExcludes;
159 
160     /**
161      * Command-line version of checkModificationExcludes.
162      *
163      * @since 2.1
164      */
165     @Parameter(property = "checkModificationExcludeList")
166     private String checkModificationExcludeList;
167 
168     /**
169      * Default version to use when preparing a release or a branch.
170      *
171      * @since 2.0-beta-8
172      */
173     @Parameter(property = "releaseVersion")
174     private String releaseVersion;
175 
176     /**
177      * Default version to use for new local working copy.
178      *
179      * @since 2.0-beta-8
180      */
181     @Parameter(property = "developmentVersion")
182     private String developmentVersion;
183 
184     /**
185      * Currently only implemented with svn scm.
186      * <ul>
187      * <li>Enables a workaround to prevent issue due to svn client > 1.5.0 (fixed in 1.6.5)
188      * (https://issues.apache.org/jira/browse/SCM-406)</li>
189      * <li>You may not want to use this in conjunction with <code>suppressCommitBeforeTag</code>, such that no poms with
190      * released versions are committed to the working copy ever.</li>
191      * </ul>
192      *
193      * @since 2.0-beta-9
194      */
195     @Parameter(defaultValue = "true", property = "remoteTagging")
196     private boolean remoteTagging;
197 
198     /**
199      * Signs SCM tag when possible, for example when using the git-exe the '--sign' argument is used.
200      *
201      * @since 3.0.0-M4
202      */
203     @Parameter(property = "signTag")
204     private boolean signTag = false;
205 
206     /**
207      * Whether to bump the working copy versions to <code>developmentVersion</code>.
208      *
209      * @since 2.1
210      */
211     @Parameter(defaultValue = "true", property = "updateWorkingCopyVersions")
212     private boolean updateWorkingCopyVersions;
213 
214     /**
215      * Whether to suppress a commit of changes to the working copy before the tag is created. <br/>
216      * <br/>
217      * This requires <code>remoteTagging</code> to be set to false. <br/>
218      * <br/>
219      * <code>suppressCommitBeforeTag</code> is useful when you want to avoid poms with released versions in all
220      * revisions of your trunk or development branch.
221      *
222      * @since 2.1
223      */
224     @Parameter(defaultValue = "false", property = "suppressCommitBeforeTag")
225     private boolean suppressCommitBeforeTag;
226 
227     /**
228      * Wait the specified number of seconds before creating the tag. <br/>
229      * <code>waitBeforeTagging</code> is useful when your source repository is synced between several instances and
230      * access to it is determined by geographical location, like the SVN repository at the Apache Software Foundation.
231      *
232      * @since 2.2
233      */
234     @Parameter(defaultValue = "0", property = "waitBeforeTagging")
235     private int waitBeforeTagging;
236 
237     /**
238      * The role-hint for the {@link org.apache.maven.shared.release.policy.version.VersionPolicy}
239      * implementation used to calculate the project versions.
240      *
241      * @since 2.5.1
242      * @see org.apache.maven.shared.release.policies.DefaultVersionPolicy
243      */
244     @Parameter(defaultValue = "default", property = "projectVersionPolicyId")
245     private String projectVersionPolicyId;
246 
247     /**
248      * Optional config for the VersionPolicy implementation used to calculate the project versions.
249      *
250      * @since 3.0.0
251      */
252     @Parameter(property = "projectVersionPolicyConfig")
253     private XmlPlexusConfiguration projectVersionPolicyConfig;
254 
255     /**
256      * The role-hint for the {@link org.apache.maven.shared.release.policy.naming.NamingPolicy}
257      * implementation used to calculate the project branch and tag names.
258      *
259      * @since 3.0.0-M1
260      * @see org.apache.maven.shared.release.policies.DefaultNamingPolicy
261      */
262     @Parameter(property = "projectNamingPolicyId")
263     private String projectTagNamingPolicyId;
264 
265     /**
266      * The SCM commit comment when setting pom.xml to release.
267      * Defaults to "@{prefix} prepare release @{releaseLabel}".
268      * <p>
269      * Property interpolation is performed on the value, but in order to ensure that the interpolation occurs
270      * during release, you must use <code>@{...}</code> to reference the properties rather than <code>${...}</code>.
271      * The following properties are available:
272      * <ul>
273      *     <li><code>prefix</code> - The comment prefix.
274      *     <li><code>groupId</code> - The groupId of the root project.
275      *     <li><code>artifactId</code> - The artifactId of the root project.
276      *     <li><code>releaseLabel</code> - The release version of the root project.
277      * </ul>
278      *
279      * @since 3.0.0-M1
280      */
281     @Parameter(defaultValue = "@{prefix} prepare release @{releaseLabel}", property = "scmReleaseCommitComment")
282     private String scmReleaseCommitComment = "@{prefix} prepare release @{releaseLabel}";
283 
284     /**
285      * The SCM commit comment when setting pom.xml back to development.
286      * Defaults to "@{prefix} prepare for next development iteration".
287      * <p>
288      * Property interpolation is performed on the value, but in order to ensure that the interpolation occurs
289      * during release, you must use <code>@{...}</code> to reference the properties rather than <code>${...}</code>.
290      * The following properties are available:
291      * <ul>
292      *     <li><code>prefix</code> - The comment prefix.
293      *     <li><code>groupId</code> - The groupId of the root project.
294      *     <li><code>artifactId</code> - The artifactId of the root project.
295      *     <li><code>releaseLabel</code> - The release version of the root project.
296      * </ul>
297      *
298      * @since 3.0.0-M1
299      */
300     @Parameter(
301             defaultValue = "@{prefix} prepare for next development iteration",
302             property = "scmDevelopmentCommitComment")
303     private String scmDevelopmentCommitComment = "@{prefix} prepare for next development iteration";
304 
305     /**
306      * Specifies whether unresolved SNAPSHOT dependencies should automatically be resolved.
307      * If this is set, then this specifies the default answer to be used when unresolved SNAPSHOT
308      * dependencies should automatically be resolved ( 0:All 1:Project Dependencies 2:Plugins
309      * 3:Reports 4:Extensions ). Possible values are:
310      * <ul>
311      * <li>"all" or "0": resolve all kinds of snapshots, ie. project, plugin, report and extension dependencies </li>
312      * <li>"dependencies" or "1": resolve project dependencies</li>
313      * <li>"plugins" or "2": resolve plugin dependencis</li>
314      * <li>"reports" or "3": resolve report dependencies</li>
315      * <li>"extensions" or "4": resolve extension dependencies</li>
316      * </ul>
317      *
318      * @since 3.0.0-M4
319      */
320     @Parameter(property = "autoResolveSnapshots")
321     private String autoResolveSnapshots;
322 
323     /**
324      * Currently only implemented with svn scm. Enable the {@code --pin-externals} option in
325      * {@code svn copy} command which is new in Subversion 1.9.
326      *
327      * @since 3.0.0-M4
328      */
329     @Parameter(defaultValue = "false", property = "pinExternals")
330     private boolean pinExternals;
331 
332     /**
333      * {@inheritDoc}
334      */
335     @Override
336     protected String getAdditionalProfiles() {
337         return preparationProfiles;
338     }
339 
340     /**
341      * Specifies the line separator to format pom.xml. The following properties are
342      * available:
343      * <ul>
344      * <li><code>system</code> - Use the system line separator.</li>
345      * <li><code>lf</code> - Use \n as line separator.</li>
346      * <li><code>cr</code> - Use \r as line separator.</li>
347      * <li><code>crlf</code> - Use \r\n as line separator.</li>
348      * <li><code>source</code> - Use the same line separator as it is specified in the current pom.xml.</li>
349      * </ul>
350      *
351      * @since 3.0.0-M6
352      */
353     @Parameter(defaultValue = "source", property = "lineSeparator")
354     private String lineSeparator;
355 
356     /**
357      * {@inheritDoc}
358      */
359     @Override
360     public void execute() throws MojoExecutionException, MojoFailureException {
361         if (generateReleasePoms) {
362             throw new MojoFailureException("Generating release POMs is no longer supported in release:prepare. "
363                     + "Please run release:prepare-with-pom instead.");
364         }
365 
366         prepareRelease(generateReleasePoms);
367     }
368 
369     /**
370      * <p>prepareRelease.</p>
371      *
372      * @param generateReleasePoms a boolean
373      * @throws org.apache.maven.plugin.MojoExecutionException if any.
374      * @throws org.apache.maven.plugin.MojoFailureException if any.
375      */
376     protected void prepareRelease(boolean generateReleasePoms) throws MojoExecutionException, MojoFailureException {
377         // this is here so the subclass can call it without getting the extra generateReleasePoms check in execute()
378         // above
379         super.execute();
380 
381         final ReleaseDescriptorBuilder config = createReleaseDescriptor();
382         config.setAddSchema(addSchema);
383         config.setGenerateReleasePoms(generateReleasePoms);
384         config.setScmUseEditMode(useEditMode);
385         config.setPreparationGoals(preparationGoals);
386         config.setCompletionGoals(completionGoals);
387         config.setCommitByProject(commitByProject);
388         config.setUpdateDependencies(updateDependencies);
389         config.setAutoVersionSubmodules(autoVersionSubmodules);
390         config.setAllowTimestampedSnapshots(allowTimestampedSnapshots);
391         config.setSnapshotReleasePluginAllowed(allowReleasePluginSnapshot);
392         config.setDefaultReleaseVersion(releaseVersion);
393         config.setDefaultDevelopmentVersion(developmentVersion);
394         config.setRemoteTagging(remoteTagging);
395         config.setScmSignTags(signTag);
396         config.setUpdateWorkingCopyVersions(updateWorkingCopyVersions);
397         config.setSuppressCommitBeforeTagOrBranch(suppressCommitBeforeTag);
398         config.setWaitBeforeTagging(waitBeforeTagging);
399         config.setProjectVersionPolicyId(projectVersionPolicyId);
400         if (projectVersionPolicyConfig != null) {
401             config.setProjectVersionPolicyConfig(projectVersionPolicyConfig.toString());
402         }
403         config.setProjectNamingPolicyId(projectTagNamingPolicyId);
404         config.setScmDevelopmentCommitComment(scmDevelopmentCommitComment);
405         config.setScmReleaseCommitComment(scmReleaseCommitComment);
406         config.setAutoResolveSnapshots(autoResolveSnapshots);
407         config.setPinExternals(pinExternals);
408         config.setLineSeparator(resolveLineSeparator());
409 
410         if (checkModificationExcludeList != null) {
411             checkModificationExcludes =
412                     checkModificationExcludeList.replaceAll("\\s", "").split(",");
413         }
414 
415         if (checkModificationExcludes != null) {
416             config.setCheckModificationExcludes(Arrays.asList(checkModificationExcludes));
417         }
418 
419         ReleasePrepareRequest prepareRequest = new ReleasePrepareRequest();
420         prepareRequest.setReleaseDescriptorBuilder(config);
421         prepareRequest.setReleaseEnvironment(getReleaseEnvironment());
422         prepareRequest.setReactorProjects(getReactorProjects());
423         prepareRequest.setReleaseManagerListener(new DefaultReleaseManagerListener(getLog(), dryRun));
424         prepareRequest.setResume(resume);
425         prepareRequest.setDryRun(dryRun);
426         prepareRequest.setUserProperties(session.getUserProperties());
427 
428         try {
429             releaseManager.prepare(prepareRequest);
430         } catch (ReleaseExecutionException e) {
431             throw new MojoExecutionException(e.getMessage(), e);
432         } catch (ReleaseFailureException e) {
433             throw new MojoFailureException(e.getMessage(), e);
434         }
435     }
436 
437     private String resolveLineSeparator() throws MojoExecutionException {
438         if (lineSeparator == null) {
439             return getLineSeparatorFromPom();
440         }
441 
442         switch (lineSeparator) {
443             case "lf":
444                 return "\n";
445             case "cr":
446                 return "\r";
447             case "crlf":
448                 return "\r\n";
449             case "system":
450                 return System.lineSeparator();
451             case "source":
452                 return getLineSeparatorFromPom();
453             default:
454                 throw new IllegalArgumentException(String.format(
455                         "Unknown property lineSeparator: '%s'. Use one of"
456                                 + " the following: 'source', 'system', 'lf', 'cr', 'crlf'.",
457                         lineSeparator));
458         }
459     }
460 
461     private String getLineSeparatorFromPom() throws MojoExecutionException {
462         char current;
463         String lineSeparator = "";
464         try (InputStream is = new FileInputStream(this.project.getFile())) {
465             while (is.available() > 0) {
466                 current = (char) is.read();
467                 if ((current == '\n') || (current == '\r')) {
468                     lineSeparator += current;
469                     if (is.available() > 0) {
470                         char next = (char) is.read();
471                         if ((next != current) && ((next == '\r') || (next == '\n'))) {
472                             lineSeparator += next;
473                         }
474                     }
475                     return lineSeparator;
476                 }
477             }
478         } catch (IOException e) {
479             throw new MojoExecutionException("Failed to detect line separator of " + this.project.getFile(), e);
480         }
481 
482         return lineSeparator;
483     }
484 }