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