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.command.export.ExportScmResult;
30  import org.apache.maven.scm.repository.ScmRepository;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  /**
34   * Get a fresh exported copy of the latest source from the configured scm url.
35   *
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   */
38  @Mojo(name = "export", requiresProject = false)
39  public class ExportMojo extends AbstractScmMojo {
40      /**
41       * The version type (branch/tag/revision) of scmVersion.
42       */
43      @Parameter(property = "scmVersionType")
44      private String scmVersionType;
45  
46      /**
47       * The version (revision number/branch name/tag name).
48       */
49      @Parameter(property = "scmVersion")
50      private String scmVersion;
51  
52      /**
53       * The directory to export the sources to.
54       */
55      @Parameter(property = "exportDirectory", defaultValue = "${project.build.directory}/export", required = true)
56      private File exportDirectory;
57  
58      /**
59       * Skip export if exportDirectory exists.
60       */
61      @Parameter(property = "skipExportIfExists", defaultValue = "false")
62      private boolean skipExportIfExists = false;
63  
64      /** {@inheritDoc} */
65      public void execute() throws MojoExecutionException {
66          super.execute();
67  
68          if (this.skipExportIfExists && this.exportDirectory.isDirectory()) {
69              return;
70          }
71  
72          export();
73      }
74  
75      protected File getExportDirectory() {
76          return this.exportDirectory;
77      }
78  
79      public void setExportDirectory(File exportDirectory) {
80          this.exportDirectory = exportDirectory;
81      }
82  
83      protected void export() throws MojoExecutionException {
84          if (this.exportDirectory.getPath().contains("${project.basedir}")) {
85              // project.basedir is not set under maven 3.x when run without a project
86              this.exportDirectory = new File(this.getBasedir(), "target/export");
87          }
88          try {
89              ScmRepository repository = getScmRepository();
90  
91              try {
92                  if (this.exportDirectory.exists()) {
93                      this.getLog().info("Removing " + this.exportDirectory);
94  
95                      FileUtils.deleteDirectory(this.exportDirectory);
96                  }
97              } catch (IOException e) {
98                  throw new MojoExecutionException("Cannot remove " + getExportDirectory());
99              }
100 
101             if (!this.exportDirectory.mkdirs()) {
102                 throw new MojoExecutionException("Cannot create " + this.exportDirectory);
103             }
104 
105             ExportScmResult result = getScmManager()
106                     .export(
107                             repository,
108                             new ScmFileSet(this.exportDirectory.getAbsoluteFile()),
109                             getScmVersion(scmVersionType, scmVersion));
110 
111             checkResult(result);
112 
113             handleExcludesIncludesAfterCheckoutAndExport(this.exportDirectory);
114         } catch (ScmException e) {
115             throw new MojoExecutionException("Cannot run export command : ", e);
116         }
117     }
118 }