View Javadoc
1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * 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, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.command.export.ExportScmResult;
27  import org.apache.maven.scm.repository.ScmRepository;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  /**
31   * Get a fresh exported copy of the latest source from the configured scm url.
32   *
33   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34   */
35  @Mojo( name = "export", requiresProject = false )
36  public class ExportMojo
37      extends AbstractScmMojo
38  {
39      /**
40       * The version type (branch/tag/revision) of scmVersion.
41       */
42      @Parameter( property = "scmVersionType" )
43      private String scmVersionType;
44  
45      /**
46       * The version (revision number/branch name/tag name).
47       */
48      @Parameter( property = "scmVersion" )
49      private String scmVersion;
50  
51      /**
52       * The directory to export the sources to.
53       */
54      @Parameter( property = "exportDirectory", defaultValue = "${project.build.directory}/export", required = true )
55      private File exportDirectory;
56  
57      /**
58       * Skip export if exportDirectory exists.
59       */
60      @Parameter( property = "skipExportIfExists", defaultValue = "false" )
61      private boolean skipExportIfExists = false;
62  
63  
64      /** {@inheritDoc} */
65      public void execute()
66          throws MojoExecutionException
67      {
68          super.execute();
69  
70          if ( this.skipExportIfExists && this.exportDirectory.isDirectory()  )
71          {
72              return;
73          }
74  
75          export();
76      }
77  
78      protected File getExportDirectory()
79      {
80          return this.exportDirectory;
81      }
82  
83      public void setExportDirectory( File exportDirectory )
84      {
85          this.exportDirectory = exportDirectory;
86      }
87  
88      protected void export()
89          throws MojoExecutionException
90      {
91          if ( this.exportDirectory.getPath().contains( "${project.basedir}" ))
92          {
93              //project.basedir is not set under maven 3.x when run without a project
94              this.exportDirectory = new File( this.getBasedir(), "target/export");
95          }
96          try
97          {
98              ScmRepository repository = getScmRepository();
99  
100             try
101             {
102                 if ( this.exportDirectory.exists() )
103                 {
104                     this.getLog().info( "Removing " + this.exportDirectory );
105 
106                     FileUtils.deleteDirectory( this.exportDirectory );
107                 }
108             }
109             catch ( IOException e )
110             {
111                 throw new MojoExecutionException( "Cannot remove " + getExportDirectory() );
112             }
113 
114             if ( !this.exportDirectory.mkdirs() )
115             {
116                 throw new MojoExecutionException( "Cannot create " + this.exportDirectory );
117             }
118 
119             ExportScmResult result = getScmManager().export( repository,
120                                                              new ScmFileSet( this.exportDirectory.getAbsoluteFile() ),
121                                                              getScmVersion( scmVersionType, scmVersion ) );
122 
123             checkResult( result );
124 
125             handleExcludesIncludesAfterCheckoutAndExport( this.exportDirectory );
126         }
127         catch ( ScmException e )
128         {
129             throw new MojoExecutionException( "Cannot run export command : ", e );
130         }
131     }
132 }