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