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