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 javax.inject.Inject;
022
023import java.io.IOException;
024
025import org.apache.maven.plugin.MojoExecutionException;
026import org.apache.maven.plugins.annotations.Mojo;
027import org.apache.maven.plugins.annotations.Parameter;
028import org.apache.maven.scm.ScmBranchParameters;
029import org.apache.maven.scm.ScmException;
030import org.apache.maven.scm.command.branch.BranchScmResult;
031import org.apache.maven.scm.manager.ScmManager;
032import org.apache.maven.scm.provider.ScmProvider;
033import org.apache.maven.scm.repository.ScmRepository;
034import org.apache.maven.settings.crypto.SettingsDecrypter;
035
036/**
037 * Branch the project.
038 *
039 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
040 */
041@Mojo(name = "branch", aggregator = true)
042public class BranchMojo extends AbstractScmMojo {
043    /**
044     * The branch name.
045     */
046    @Parameter(property = "branch", required = true)
047    private String branch;
048
049    /**
050     * The message applied to the tag creation.
051     */
052    @Parameter(property = "message")
053    private String message;
054
055    /**
056     * currently only implemented with svn scm. Enable a workaround to prevent issue
057     * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
058     *
059     * @since 1.3
060     */
061    @Parameter(property = "remoteBranching", defaultValue = "true")
062    private boolean remoteBranching;
063
064    /**
065     * Currently only implemented with Subversion. Enable the "--pin-externals"
066     * option in svn copy commands which is new in Subversion 1.9.
067     *
068     * @since 1.11.0
069     *
070     * @see https://subversion.apache.org/docs/release-notes/1.9.html
071     */
072    @Parameter(property = "pinExternals", defaultValue = "false")
073    private boolean pinExternals;
074
075    @Inject
076    public BranchMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
077        super(manager, settingsDecrypter);
078    }
079
080    /** {@inheritDoc} */
081    public void execute() throws MojoExecutionException {
082        super.execute();
083
084        try {
085            ScmRepository repository = getScmRepository();
086            ScmProvider provider = getScmManager().getProviderByRepository(repository);
087
088            String finalBranch = provider.sanitizeTagName(branch);
089            getLog().info("Final Branch Name: '" + finalBranch + "'");
090
091            ScmBranchParameters scmBranchParameters = new ScmBranchParameters(message);
092            scmBranchParameters.setRemoteBranching(remoteBranching);
093            scmBranchParameters.setPinExternals(pinExternals);
094
095            BranchScmResult result = provider.branch(repository, getFileSet(), finalBranch, scmBranchParameters);
096
097            checkResult(result);
098        } catch (IOException | ScmException e) {
099            throw new MojoExecutionException("Cannot run branch command", e);
100        }
101    }
102}