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.scm.plugin;
20  
21  import javax.inject.Inject;
22  
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.scm.ScmBranchParameters;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.command.branch.BranchScmResult;
31  import org.apache.maven.scm.manager.ScmManager;
32  import org.apache.maven.scm.provider.ScmProvider;
33  import org.apache.maven.scm.repository.ScmRepository;
34  import org.apache.maven.settings.crypto.SettingsDecrypter;
35  
36  /**
37   * Branch the project.
38   *
39   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
40   */
41  @Mojo(name = "branch", aggregator = true)
42  public class BranchMojo extends AbstractScmMojo {
43      /**
44       * The branch name.
45       */
46      @Parameter(property = "branch", required = true)
47      private String branch;
48  
49      /**
50       * The message applied to the tag creation.
51       */
52      @Parameter(property = "message")
53      private String message;
54  
55      /**
56       * Currently only implemented with svn scm. Enable a workaround to prevent issue
57       * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
58       *
59       * @since 1.3
60       */
61      @Parameter(property = "remoteBranching", defaultValue = "true")
62      private boolean remoteBranching;
63  
64      /**
65       * Currently only implemented with Subversion. Enable the "--pin-externals"
66       * option in svn copy commands which is new in Subversion 1.9.
67       *
68       * @see https://subversion.apache.org/docs/release-notes/1.9.html
69       * @since 1.11.0
70       */
71      @Parameter(property = "pinExternals", defaultValue = "false")
72      private boolean pinExternals;
73  
74      @Inject
75      public BranchMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
76          super(manager, settingsDecrypter);
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      public void execute() throws MojoExecutionException {
83          super.execute();
84  
85          try {
86              ScmRepository repository = getScmRepository();
87              ScmProvider provider = getScmManager().getProviderByRepository(repository);
88  
89              String finalBranch = provider.sanitizeTagName(branch);
90              getLog().debug("Final Branch Name: '" + finalBranch + "'");
91  
92              ScmBranchParameters scmBranchParameters = new ScmBranchParameters(message);
93              scmBranchParameters.setRemoteBranching(remoteBranching);
94              scmBranchParameters.setPinExternals(pinExternals);
95  
96              BranchScmResult result = provider.branch(repository, getFileSet(), finalBranch, scmBranchParameters);
97  
98              checkResult(result);
99          } catch (IOException | ScmException e) {
100             throw new MojoExecutionException("Cannot run branch command", e);
101         }
102     }
103 }