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