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.1.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      /** {@inheritDoc} */
95      public void execute() throws MojoExecutionException {
96          super.execute();
97  
98          // skip checkout if checkout directory is already created. See SCM-201
99          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 }