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        if ( this.exportDirectory.getPath().contains( "${project.basedir}" ))
092        {
093            //project.basedir is not set under maven 3.x when run without a project
094            this.exportDirectory = new File( this.getBasedir(), "target/export");
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}