View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.scm.CommandParameter;
30  import org.apache.maven.scm.CommandParameters;
31  import org.apache.maven.scm.ScmException;
32  import org.apache.maven.scm.ScmFileSet;
33  import org.apache.maven.scm.ScmResult;
34  import org.apache.maven.scm.manager.ScmManager;
35  import org.apache.maven.scm.repository.ScmRepository;
36  import org.apache.maven.settings.crypto.SettingsDecrypter;
37  import org.codehaus.plexus.util.FileUtils;
38  
39  /**
40   * Get a fresh copy of the latest source from the configured scm url.
41   *
42   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
43   */
44  @Mojo(name = "checkout", requiresProject = false)
45  public class CheckoutMojo extends AbstractScmMojo {
46      /**
47       * Use Export instead of checkout.
48       */
49      @Parameter(property = "useExport", defaultValue = "false")
50      private boolean useExport;
51  
52      /**
53       * The directory to checkout the sources to for the bootstrap and checkout goals.
54       */
55      @Parameter(property = "checkoutDirectory", defaultValue = "${project.build.directory}/checkout")
56      private File checkoutDirectory;
57  
58      /**
59       * Skip checkout if checkoutDirectory exists.
60       */
61      @Parameter(property = "skipCheckoutIfExists", defaultValue = "false")
62      private boolean skipCheckoutIfExists = false;
63  
64      /**
65       * The version type (branch/tag/revision) of scmVersion.
66       */
67      @Parameter(property = "scmVersionType")
68      private String scmVersionType;
69  
70      /**
71       * The version (revision number/branch name/tag name).
72       */
73      @Parameter(property = "scmVersion")
74      private String scmVersion;
75  
76      /**
77       * Currently only implemented with Git Executable. Perform a shallow checkout.
78       *
79       * @since 2.2.1
80       */
81      @Parameter(property = "shallow", defaultValue = "false")
82      private boolean shallow = false;
83  
84      /**
85       * Allow extended mojo (ie BootStrap ) to see checkout result.
86       */
87      private ScmResult checkoutResult;
88  
89      @Inject
90      public CheckoutMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
91          super(manager, settingsDecrypter);
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public void execute() throws MojoExecutionException {
98          super.execute();
99  
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 }