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.util.Arrays;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.shared.release.DefaultReleaseManagerListener;
28  import org.apache.maven.shared.release.ReleaseBranchRequest;
29  import org.apache.maven.shared.release.ReleaseExecutionException;
30  import org.apache.maven.shared.release.ReleaseFailureException;
31  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
32  import org.codehaus.plexus.configuration.PlexusConfiguration;
33  
34  /**
35   * Branch a project in SCM, using the same steps as the <code>release:prepare</code> goal, creating a branch instead of
36   * a tag. For more info see <a href="https://maven.apache.org/plugins/maven-release-plugin/usage/branch.html"
37   * >https://maven.apache.org/plugins/maven-release-plugin/usage/branch.html</a>.
38   *
39   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
40   * @since 2.0-beta-6
41   */
42  @Mojo(name = "branch", aggregator = true)
43  public class BranchReleaseMojo extends AbstractScmReleaseMojo {
44      /**
45       * The branch name to use.
46       *
47       * @since 2.0-beta-6
48       */
49      @Parameter(property = "branchName")
50      private String branchName;
51  
52      /**
53       * The branch base directory in SVN, you must define it if you don't use the standard svn layout
54       * (trunk/tags/branches). For example, <code>http://svn.apache.org/repos/asf/maven/plugins/branches</code>. The URL
55       * is an SVN URL and does not include the SCM provider and protocol.
56       *
57       * @since 2.0
58       */
59      @Parameter(property = "branchBase")
60      private String branchBase;
61  
62      /**
63       * Whether to update versions in the branch.
64       *
65       * @since 2.0-beta-6
66       */
67      @Parameter(defaultValue = "false", property = "updateBranchVersions")
68      private boolean updateBranchVersions;
69  
70      /**
71       * Whether to update versions in the working copy.
72       *
73       * @since 2.0-beta-6
74       */
75      @Parameter(defaultValue = "true", property = "updateWorkingCopyVersions")
76      private boolean updateWorkingCopyVersions;
77  
78      /**
79       * Whether to suppress a commit of changes to the working copy
80       * before the tag is created.
81       * <br/>
82       * <br/>This requires <code>remoteTagging</code> to be set to false.
83       * <br/>
84       * <br/><code>suppressCommitBeforeBranch</code> is useful when you want
85       * to avoid poms with released versions in all revisions of your
86       * trunk or development branch.
87       *
88       * @since 2.1
89       */
90      @Parameter(defaultValue = "false", property = "suppressCommitBeforeBranch")
91      private boolean suppressCommitBeforeBranch;
92  
93      /**
94       * Whether to update versions to SNAPSHOT in the branch.
95       *
96       * @since 2.0-beta-6
97       */
98      @Parameter(defaultValue = "true", property = "updateVersionsToSnapshot")
99      private boolean updateVersionsToSnapshot;
100 
101     /**
102      * Whether to use "edit" mode on the SCM, to lock the file for editing during SCM operations.
103      *
104      * @since 2.0-beta-6
105      */
106     @Parameter(defaultValue = "false", property = "useEditMode")
107     private boolean useEditMode;
108 
109     /**
110      * Whether to update dependencies version to the next development version.
111      *
112      * @since 2.0-beta-6
113      */
114     @Parameter(defaultValue = "true", property = "updateDependencies")
115     private boolean updateDependencies;
116 
117     /**
118      * Whether to automatically assign submodules the parent version.  If set to false,
119      * the user will be prompted for the version of each submodules.
120      *
121      * @since 2.0-beta-6
122      */
123     @Parameter(defaultValue = "false", property = "autoVersionSubmodules")
124     private boolean autoVersionSubmodules;
125 
126     /**
127      * Dry run: don't checkin or tag anything in the scm repository, or modify the checkout.
128      * Running <code>mvn -DdryRun=true release:prepare</code> is useful in order to check that modifications to
129      * poms and scm operations (only listed on the console) are working as expected.
130      * Modified POMs are written alongside the originals without modifying them.
131      *
132      * @since 2.0-beta-6
133      */
134     @Parameter(defaultValue = "false", property = "dryRun")
135     private boolean dryRun;
136 
137     /**
138      * Whether to add a schema to the POM if it was previously missing on release.
139      *
140      * @since 2.0-beta-6
141      */
142     @Parameter(defaultValue = "true", property = "addSchema")
143     private boolean addSchema;
144 
145     /**
146      * currently only implemented with svn scm. Enable a workaround to prevent issue
147      * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
148      *
149      * @since 2.0
150      */
151     @Parameter(defaultValue = "true", property = "remoteTagging")
152     private boolean remoteTagging;
153 
154     /**
155      * A list of additional exclude filters that will be skipped when checking for
156      * modifications on the working copy.
157      *
158      * Is ignored, when checkModificationExcludes is set.
159      *
160      * @since 2.1
161      */
162     @Parameter
163     private String[] checkModificationExcludes;
164 
165     /**
166      * Command-line version of checkModificationExcludes.
167      *
168      * @since 2.1
169      */
170     @Parameter(property = "checkModificationExcludeList")
171     private String checkModificationExcludeList;
172 
173     /**
174      * Specify the new version for the branch.
175      * This parameter is only meaningful if {@link #updateBranchVersions} = {@code true}.
176      *
177      * @since 2.0
178      */
179     @Parameter(property = "releaseVersion")
180     private String releaseVersion;
181 
182     /**
183      * Specify the new version for the working copy.
184      * This parameter is only meaningful if {@link #updateWorkingCopyVersions} = {@code true}.
185      *
186      * @since 2.0
187      */
188     @Parameter(property = "developmentVersion")
189     private String developmentVersion;
190 
191     /**
192      * The role-hint for the {@link org.apache.maven.shared.release.policy.version.VersionPolicy}
193      * implementation used to calculate the project versions.
194      *
195      * @since 3.0.0-M5
196      * @see org.apache.maven.shared.release.policies.DefaultVersionPolicy
197      */
198     @Parameter(defaultValue = "default", property = "projectVersionPolicyId")
199     private String projectVersionPolicyId;
200 
201     /**
202      * Optional config for the VersionPolicy implementation used to calculate the project versions.
203      *
204      * @since 3.0.0
205      */
206     @Parameter(property = "projectVersionPolicyConfig")
207     private PlexusConfiguration projectVersionPolicyConfig;
208 
209     /**
210      * The role-hint for the {@link org.apache.maven.shared.release.policy.naming.NamingPolicy}
211      * implementation used to calculate the project names.
212      *
213      * @since 3.0.0-M5
214      * @see org.apache.maven.shared.release.policies.DefaultNamingPolicy
215      */
216     @Parameter(property = "projectNamingPolicyId")
217     private String projectBranchNamingPolicyId;
218 
219     /**
220      * The SCM commit comment when branching.
221      * Defaults to "@{prefix} prepare branch @{releaseLabel}".
222      * <p>
223      * Property interpolation is performed on the value, but in order to ensure that the interpolation occurs
224      * during release, you must use <code>@{...}</code> to reference the properties rather than <code>${...}</code>.
225      * The following properties are available:
226      * <ul>
227      *     <li><code>prefix</code> - The comment prefix.
228      *     <li><code>groupId</code> - The groupId of the root project.
229      *     <li><code>artifactId</code> - The artifactId of the root project.
230      *     <li><code>releaseLabel</code> - The release version of the root project.
231      * </ul>
232      *
233      * @since 3.0.0-M1
234      */
235     @Parameter(defaultValue = "@{prefix} prepare branch @{releaseLabel}", property = "scmBranchCommitComment")
236     private String scmBranchCommitComment = "@{prefix} prepare branch @{releaseLabel}";
237 
238     /**
239      * Currently only implemented with svn scm. Enable the {@code --pin-externals} option in
240      * {@code svn copy} command which is new in Subversion 1.9.
241      *
242      * @since 3.0.0-M5
243      */
244     @Parameter(defaultValue = "false", property = "pinExternals")
245     private boolean pinExternals;
246 
247     @Override
248     public void execute() throws MojoExecutionException, MojoFailureException {
249         super.execute();
250 
251         final ReleaseDescriptorBuilder config = createReleaseDescriptor();
252         config.setAddSchema(addSchema);
253         config.setScmUseEditMode(useEditMode);
254         config.setUpdateDependencies(updateDependencies);
255         config.setAutoVersionSubmodules(autoVersionSubmodules);
256         config.setScmReleaseLabel(branchName);
257         config.setScmBranchBase(branchBase);
258         config.setBranchCreation(true);
259         config.setUpdateBranchVersions(updateBranchVersions);
260         config.setUpdateWorkingCopyVersions(updateWorkingCopyVersions);
261         config.setUpdateVersionsToSnapshot(updateVersionsToSnapshot);
262         config.setRemoteTagging(remoteTagging);
263         config.setDefaultReleaseVersion(releaseVersion);
264         config.setDefaultDevelopmentVersion(developmentVersion);
265         config.setSuppressCommitBeforeTagOrBranch(suppressCommitBeforeBranch);
266         config.setProjectVersionPolicyId(projectVersionPolicyId);
267         if (projectVersionPolicyConfig != null) {
268             config.setProjectVersionPolicyConfig(projectVersionPolicyConfig.toString());
269         }
270         config.setProjectNamingPolicyId(projectBranchNamingPolicyId);
271         config.setScmBranchCommitComment(scmBranchCommitComment);
272         config.setPinExternals(pinExternals);
273 
274         if (checkModificationExcludeList != null) {
275             checkModificationExcludes =
276                     checkModificationExcludeList.replaceAll("\\s", "").split(",");
277         }
278 
279         if (checkModificationExcludes != null) {
280             config.setCheckModificationExcludes(Arrays.asList(checkModificationExcludes));
281         }
282 
283         try {
284             ReleaseBranchRequest branchRequest = new ReleaseBranchRequest();
285             branchRequest.setReleaseDescriptorBuilder(config);
286             branchRequest.setReleaseEnvironment(getReleaseEnvironment());
287             branchRequest.setReactorProjects(getReactorProjects());
288             branchRequest.setReleaseManagerListener(new DefaultReleaseManagerListener(getLog(), dryRun));
289             branchRequest.setDryRun(dryRun);
290             branchRequest.setUserProperties(session.getUserProperties());
291 
292             releaseManager.branch(branchRequest);
293         } catch (ReleaseExecutionException e) {
294             throw new MojoExecutionException(e.getMessage(), e);
295         } catch (ReleaseFailureException e) {
296             throw new MojoFailureException(e.getMessage(), e);
297         }
298     }
299 }