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     * @see https://subversion.apache.org/docs/release-notes/1.9.html
069     * @since 1.11.0
070     */
071    @Parameter(property = "pinExternals", defaultValue = "false")
072    private boolean pinExternals;
073
074    @Inject
075    public BranchMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
076        super(manager, settingsDecrypter);
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    public void execute() throws MojoExecutionException {
083        super.execute();
084
085        try {
086            ScmRepository repository = getScmRepository();
087            ScmProvider provider = getScmManager().getProviderByRepository(repository);
088
089            String finalBranch = provider.sanitizeTagName(branch);
090            getLog().debug("Final Branch Name: '" + finalBranch + "'");
091
092            ScmBranchParameters scmBranchParameters = new ScmBranchParameters(message);
093            scmBranchParameters.setRemoteBranching(remoteBranching);
094            scmBranchParameters.setPinExternals(pinExternals);
095
096            BranchScmResult result = provider.branch(repository, getFileSet(), finalBranch, scmBranchParameters);
097
098            checkResult(result);
099        } catch (IOException | ScmException e) {
100            throw new MojoExecutionException("Cannot run branch command", e);
101        }
102    }
103}