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