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 java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.repository.ScmRepository;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  /**
34   * Get a fresh copy of the latest source from the configured scm url.
35   *
36   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
37   */
38  @Mojo(name = "checkout", requiresProject = false)
39  public class CheckoutMojo extends AbstractScmMojo {
40      /**
41       * Use Export instead of checkout
42       */
43      @Parameter(property = "useExport", defaultValue = "false")
44      private boolean useExport;
45  
46      /**
47       * The directory to checkout the sources to for the bootstrap and checkout goals.
48       */
49      @Parameter(property = "checkoutDirectory", defaultValue = "${project.build.directory}/checkout")
50      private File checkoutDirectory;
51  
52      /**
53       * Skip checkout if checkoutDirectory exists.
54       */
55      @Parameter(property = "skipCheckoutIfExists", defaultValue = "false")
56      private boolean skipCheckoutIfExists = false;
57  
58      /**
59       * The version type (branch/tag/revision) of scmVersion.
60       */
61      @Parameter(property = "scmVersionType")
62      private String scmVersionType;
63  
64      /**
65       * The version (revision number/branch name/tag name).
66       */
67      @Parameter(property = "scmVersion")
68      private String scmVersion;
69  
70      /**
71       * allow extended mojo (ie BootStrap ) to see checkout result
72       */
73      private ScmResult checkoutResult;
74  
75      /** {@inheritDoc} */
76      public void execute() throws MojoExecutionException {
77          super.execute();
78  
79          // skip checkout if checkout directory is already created. See SCM-201
80          checkoutResult = null;
81          if (!getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists) {
82              checkoutResult = checkout();
83          }
84      }
85  
86      protected File getCheckoutDirectory() {
87          if (this.checkoutDirectory.getPath().contains("${project.basedir}")) {
88              // project.basedir is not set under maven 3.x when run without a project
89              this.checkoutDirectory = new File(this.getBasedir(), "target/checkout");
90          }
91          return this.checkoutDirectory;
92      }
93  
94      public void setCheckoutDirectory(File checkoutDirectory) {
95          this.checkoutDirectory = checkoutDirectory;
96      }
97  
98      protected ScmResult checkout() throws MojoExecutionException {
99          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 }