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          try
92          {
93              ScmRepository repository = getScmRepository();
94  
95              try
96              {
97                  if ( this.exportDirectory.exists() )
98                  {
99                      this.getLog().info( "Removing " + this.exportDirectory );
100 
101                     FileUtils.deleteDirectory( this.exportDirectory );
102                 }
103             }
104             catch ( IOException e )
105             {
106                 throw new MojoExecutionException( "Cannot remove " + getExportDirectory() );
107             }
108 
109             if ( !this.exportDirectory.mkdirs() )
110             {
111                 throw new MojoExecutionException( "Cannot create " + this.exportDirectory );
112             }                
113             
114             ExportScmResult result = getScmManager().export( repository,
115                                                              new ScmFileSet( this.exportDirectory.getAbsoluteFile() ),
116                                                              getScmVersion( scmVersionType, scmVersion ) );
117 
118             checkResult( result );
119             
120             handleExcludesIncludesAfterCheckoutAndExport( this.exportDirectory );            
121         }
122         catch ( ScmException e )
123         {
124             throw new MojoExecutionException( "Cannot run export command : ", e );
125         }
126     }
127 }