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.1.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    /** {@inheritDoc} */
095    public void execute() throws MojoExecutionException {
096        super.execute();
097
098        // skip checkout if checkout directory is already created. See SCM-201
099        checkoutResult = null;
100        if (!getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists) {
101            checkoutResult = checkout();
102        }
103    }
104
105    protected File getCheckoutDirectory() {
106        if (this.checkoutDirectory.getPath().contains("${project.basedir}")) {
107            // project.basedir is not set under maven 3.x when run without a project
108            this.checkoutDirectory = new File(this.getBasedir(), "target/checkout");
109        }
110        return this.checkoutDirectory;
111    }
112
113    public void setCheckoutDirectory(File checkoutDirectory) {
114        this.checkoutDirectory = checkoutDirectory;
115    }
116
117    protected ScmResult checkout() throws MojoExecutionException {
118        try {
119            ScmRepository repository = getScmRepository();
120
121            this.prepareOutputDirectory(getCheckoutDirectory());
122
123            ScmResult result = null;
124
125            ScmFileSet fileSet = new ScmFileSet(getCheckoutDirectory().getAbsoluteFile());
126            if (useExport) {
127                result = getScmManager().export(repository, fileSet, getScmVersion(scmVersionType, scmVersion));
128            } else {
129                CommandParameters parameters = new CommandParameters();
130                parameters.setString(CommandParameter.RECURSIVE, Boolean.toString(true));
131                parameters.setString(CommandParameter.SHALLOW, Boolean.toString(shallow));
132                result = getScmManager()
133                        .getProviderByRepository(repository)
134                        .checkOut(repository, fileSet, getScmVersion(scmVersionType, scmVersion), parameters);
135            }
136
137            checkResult(result);
138
139            handleExcludesIncludesAfterCheckoutAndExport(this.checkoutDirectory);
140
141            return result;
142        } catch (ScmException e) {
143            throw new MojoExecutionException("Cannot run checkout command : ", e);
144        }
145    }
146
147    private void prepareOutputDirectory(File ouputDirectory) throws MojoExecutionException {
148        try {
149            this.getLog().info("Removing " + ouputDirectory);
150
151            FileUtils.deleteDirectory(getCheckoutDirectory());
152        } catch (IOException e) {
153            throw new MojoExecutionException("Cannot remove " + ouputDirectory);
154        }
155
156        if (!getCheckoutDirectory().mkdirs()) {
157            throw new MojoExecutionException("Cannot create " + ouputDirectory);
158        }
159    }
160
161    protected ScmResult getCheckoutResult() {
162        return checkoutResult;
163    }
164}