001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.plugin;
020
021import java.io.IOException;
022
023import org.apache.maven.plugin.MojoExecutionException;
024import org.apache.maven.plugins.annotations.Mojo;
025import org.apache.maven.plugins.annotations.Parameter;
026import org.apache.maven.scm.ScmBranchParameters;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.command.branch.BranchScmResult;
029import org.apache.maven.scm.provider.ScmProvider;
030import org.apache.maven.scm.repository.ScmRepository;
031
032/**
033 * Branch the project.
034 *
035 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
036 */
037@Mojo(name = "branch", aggregator = true)
038public class BranchMojo extends AbstractScmMojo {
039    /**
040     * The branch name.
041     */
042    @Parameter(property = "branch", required = true)
043    private String branch;
044
045    /**
046     * The message applied to the tag creation.
047     */
048    @Parameter(property = "message")
049    private String message;
050
051    /**
052     * currently only implemented with svn scm. Enable a workaround to prevent issue
053     * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
054     *
055     * @since 1.3
056     */
057    @Parameter(property = "remoteBranching", defaultValue = "true")
058    private boolean remoteBranching;
059
060    /**
061     * Currently only implemented with Subversion. Enable the "--pin-externals"
062     * option in svn copy commands which is new in Subversion 1.9.
063     *
064     * @since 1.11.0
065     *
066     * @see https://subversion.apache.org/docs/release-notes/1.9.html
067     */
068    @Parameter(property = "pinExternals", defaultValue = "false")
069    private boolean pinExternals;
070
071    /** {@inheritDoc} */
072    public void execute() throws MojoExecutionException {
073        super.execute();
074
075        try {
076            ScmRepository repository = getScmRepository();
077            ScmProvider provider = getScmManager().getProviderByRepository(repository);
078
079            String finalBranch = provider.sanitizeTagName(branch);
080            getLog().info("Final Branch Name: '" + finalBranch + "'");
081
082            ScmBranchParameters scmBranchParameters = new ScmBranchParameters(message);
083            scmBranchParameters.setRemoteBranching(remoteBranching);
084            scmBranchParameters.setPinExternals(pinExternals);
085
086            BranchScmResult result = provider.branch(repository, getFileSet(), finalBranch, scmBranchParameters);
087
088            checkResult(result);
089        } catch (IOException | ScmException e) {
090            throw new MojoExecutionException("Cannot run branch command", e);
091        }
092    }
093}