001package 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
018import java.io.File;
019import java.io.IOException;
020
021import org.apache.maven.plugin.MojoExecutionException;
022import org.apache.maven.plugins.annotations.Mojo;
023import org.apache.maven.plugins.annotations.Parameter;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.command.export.ExportScmResult;
027import org.apache.maven.scm.repository.ScmRepository;
028import org.codehaus.plexus.util.FileUtils;
029
030/**
031 * Get a fresh exported copy of the latest source from the configured scm url.
032 * 
033 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
034 */
035@Mojo( name = "export", requiresProject = false )
036public class ExportMojo
037    extends AbstractScmMojo
038{
039    /**
040     * The version type (branch/tag/revision) of scmVersion.
041     */
042    @Parameter( property = "scmVersionType" )
043    private String scmVersionType;
044
045    /**
046     * The version (revision number/branch name/tag name).
047     */
048    @Parameter( property = "scmVersion" )
049    private String scmVersion;
050
051    /**
052     * The directory to export the sources to.
053     */
054    @Parameter( property = "exportDirectory", defaultValue = "${project.build.directory}/export", required = true )
055    private File exportDirectory;
056    
057    /**
058     * Skip export if exportDirectory exists.
059     */
060    @Parameter( property = "skipExportIfExists", defaultValue = "false" )
061    private boolean skipExportIfExists = false;
062    
063
064    /** {@inheritDoc} */
065    public void execute()
066        throws MojoExecutionException
067    {
068        super.execute();
069
070        if ( this.skipExportIfExists && this.exportDirectory.isDirectory()  )
071        {
072            return;
073        }
074        
075        export();
076    }
077
078    protected File getExportDirectory()
079    {
080        return this.exportDirectory;
081    }
082
083    public void setExportDirectory( File exportDirectory )
084    {
085        this.exportDirectory = exportDirectory;
086    }
087
088    protected void export()
089        throws MojoExecutionException
090    {
091        try
092        {
093            ScmRepository repository = getScmRepository();
094
095            try
096            {
097                if ( this.exportDirectory.exists() )
098                {
099                    this.getLog().info( "Removing " + this.exportDirectory );
100
101                    FileUtils.deleteDirectory( this.exportDirectory );
102                }
103            }
104            catch ( IOException e )
105            {
106                throw new MojoExecutionException( "Cannot remove " + getExportDirectory() );
107            }
108
109            if ( !this.exportDirectory.mkdirs() )
110            {
111                throw new MojoExecutionException( "Cannot create " + this.exportDirectory );
112            }                
113            
114            ExportScmResult result = getScmManager().export( repository,
115                                                             new ScmFileSet( this.exportDirectory.getAbsoluteFile() ),
116                                                             getScmVersion( scmVersionType, scmVersion ) );
117
118            checkResult( result );
119            
120            handleExcludesIncludesAfterCheckoutAndExport( this.exportDirectory );            
121        }
122        catch ( ScmException e )
123        {
124            throw new MojoExecutionException( "Cannot run export command : ", e );
125        }
126    }
127}