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