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 java.io.File;
023    import java.io.IOException;
024    
025    import org.apache.maven.plugin.MojoExecutionException;
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFileSet;
028    import org.apache.maven.scm.ScmResult;
029    import org.apache.maven.scm.repository.ScmRepository;
030    import org.codehaus.plexus.util.FileUtils;
031    
032    /**
033     * Get a fresh copy of the latest source from the configured scm url.
034     *
035     * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
036     * @version $Id: CheckoutMojo.java 928132 2010-03-27 03:39:51Z dantran $
037     * @goal checkout
038     * @requiresProject false
039     */
040    public class CheckoutMojo
041        extends AbstractScmMojo
042    {
043        /**
044         * Use Export instead of checkout
045         *
046         * @parameter expression="${useExport}" defaultValue="false";
047         */
048        private boolean useExport;
049        
050        /**
051         * The directory to checkout the sources to for the bootstrap and checkout goals.
052         *
053         * @parameter expression="${checkoutDirectory}" default-value="${project.build.directory}/checkout"
054         */
055        private File checkoutDirectory;
056    
057        /**
058         * Skip checkout if checkoutDirectory exists.
059         *
060         * @parameter expression="${skipCheckoutIfExists}" default-value="false"
061         */
062        private boolean skipCheckoutIfExists = false;
063    
064        /**
065         * The version type (branch/tag/revision) of scmVersion.
066         *
067         * @parameter expression="${scmVersionType}"
068         */
069        private String scmVersionType;
070    
071        /**
072         * The version (revision number/branch name/tag name).
073         *
074         * @parameter expression="${scmVersion}"
075         */
076        private String scmVersion;
077    
078        /**
079         * allow extended mojo (ie BootStrap ) to see checkout result
080         */
081        private ScmResult checkoutResult;
082    
083        /** {@inheritDoc} */
084        public void execute()
085            throws MojoExecutionException
086        {
087            super.execute();
088    
089            //skip checkout if checkout directory is already created. See SCM-201
090            checkoutResult = null;
091            if ( !getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists )
092            {
093                checkoutResult = checkout();
094            }
095        }
096    
097        protected File getCheckoutDirectory()
098        {
099            return this.checkoutDirectory;
100        }
101    
102        public void setCheckoutDirectory( File checkoutDirectory )
103        {
104            this.checkoutDirectory = checkoutDirectory;
105        }
106    
107        protected ScmResult checkout()
108            throws MojoExecutionException
109        {
110            try
111            {
112                ScmRepository repository = getScmRepository();
113    
114                this.prepareOutputDirectory( getCheckoutDirectory() );
115                
116                ScmResult result = null;
117                
118                ScmFileSet fileSet = new ScmFileSet( getCheckoutDirectory().getAbsoluteFile() );
119                if ( useExport )
120                {
121                    result = getScmManager().export( repository,fileSet, getScmVersion( scmVersionType, scmVersion ) );
122                }
123                else
124                {
125                    result = getScmManager().checkOut( repository,fileSet , getScmVersion( scmVersionType, scmVersion ) );
126                }
127    
128                checkResult( result );
129                
130               
131                handleExcludesIncludesAfterCheckoutAndExport( this.checkoutDirectory );
132    
133                return result;
134            }
135            catch ( ScmException e )
136            {
137                throw new MojoExecutionException( "Cannot run checkout command : ", e );
138            }
139        }
140    
141        private void prepareOutputDirectory( File ouputDirectory )
142            throws MojoExecutionException
143        {
144            try
145            {
146                this.getLog().info( "Removing " + ouputDirectory );
147    
148                FileUtils.deleteDirectory( getCheckoutDirectory() );
149            }
150            catch ( IOException e )
151            {
152                throw new MojoExecutionException( "Cannot remove " + ouputDirectory );
153            }
154            
155            if ( !getCheckoutDirectory().mkdirs() )
156            {
157                throw new MojoExecutionException( "Cannot create " + ouputDirectory );
158            }
159        }
160        
161        protected ScmResult getCheckoutResult()
162        {
163            return checkoutResult;
164        }
165        
166    
167    }