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