001    package org.apache.maven.scm.plugin;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
005     * agreements. See the NOTICE file distributed with this work for additional information regarding
006     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance with the License. You may obtain a
008     * 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, software distributed under the License
013     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014     * or implied. See the License for the specific language governing permissions and limitations under
015     * the License.
016     */
017    
018    import java.io.File;
019    import java.io.IOException;
020    
021    import org.apache.maven.plugin.MojoExecutionException;
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.command.export.ExportScmResult;
025    import org.apache.maven.scm.repository.ScmRepository;
026    import org.codehaus.plexus.util.FileUtils;
027    
028    /**
029     * Get a fresh exported copy of the latest source from the configured scm url.
030     * 
031     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
032     * @version $Id: ExportMojo.java 928132 2010-03-27 03:39:51Z dantran $
033     * @goal export
034     * @requiresProject false
035     */
036    public class ExportMojo
037        extends AbstractScmMojo
038    {
039        /**
040         * The version type (branch/tag/revision) of scmVersion.
041         * 
042         * @parameter expression="${scmVersionType}"
043         */
044        private String scmVersionType;
045    
046        /**
047         * The version (revision number/branch name/tag name).
048         * 
049         * @parameter expression="${scmVersion}"
050         */
051        private String scmVersion;
052    
053        /**
054         * The directory to export the sources to.
055         * 
056         * @parameter expression="${exportDirectory}" default-value="${project.build.directory}/export
057         * @required
058         */
059        private File exportDirectory;
060        
061        /**
062         * Skip export if exportDirectory exists.
063         *
064         * @parameter expression="${skipExportIfExists}" default-value="false"
065         */
066        private boolean skipExportIfExists = false;
067        
068    
069        /** {@inheritDoc} */
070        public void execute()
071            throws MojoExecutionException
072        {
073            super.execute();
074    
075            if ( this.skipExportIfExists && this.exportDirectory.isDirectory()  )
076            {
077                return;
078            }
079            
080            export();
081        }
082    
083        protected File getExportDirectory()
084        {
085            return this.exportDirectory;
086        }
087    
088        public void setExportDirectory( File exportDirectory )
089        {
090            this.exportDirectory = exportDirectory;
091        }
092    
093        protected void export()
094            throws MojoExecutionException
095        {
096            try
097            {
098                ScmRepository repository = getScmRepository();
099    
100                try
101                {
102                    if ( this.exportDirectory.exists() )
103                    {
104                        this.getLog().info( "Removing " + this.exportDirectory );
105    
106                        FileUtils.deleteDirectory( this.exportDirectory );
107                    }
108                }
109                catch ( IOException e )
110                {
111                    throw new MojoExecutionException( "Cannot remove " + getExportDirectory() );
112                }
113    
114                if ( !this.exportDirectory.mkdirs() )
115                {
116                    throw new MojoExecutionException( "Cannot create " + this.exportDirectory );
117                }                
118                
119                ExportScmResult result = getScmManager().export( repository,
120                                                                 new ScmFileSet( this.exportDirectory.getAbsoluteFile() ),
121                                                                 getScmVersion( scmVersionType, scmVersion ) );
122    
123                checkResult( result );
124                
125                handleExcludesIncludesAfterCheckoutAndExport( this.exportDirectory );            
126            }
127            catch ( ScmException e )
128            {
129                throw new MojoExecutionException( "Cannot run export command : ", e );
130            }
131        }
132    }