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