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