View Javadoc

1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.command.export.ExportScmResult;
26  import org.apache.maven.scm.repository.ScmRepository;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import java.io.File;
31  import java.io.IOException;
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   * @version $Id: ExportMojo.java 594453 2007-11-13 08:20:20Z evenisse $
38   * @goal export
39   * @description Export a project
40   * @requiresProject false
41   */
42  public class ExportMojo
43      extends AbstractScmMojo
44  {
45      /**
46       * The version type (branch/tag/revision) of scmVersion.
47       *
48       * @parameter expression="${scmVersionType}"
49       */
50      private String scmVersionType;
51  
52      /**
53       * The version (revision number/branch name/tag name).
54       *
55       * @parameter expression="${scmVersion}"
56       */
57      private String scmVersion;
58  
59      /**
60       * The directory to export the sources to.
61       *
62       * @parameter expression="${exportDirectory}"
63       * @required
64       */
65      private String exportDirectory;
66  
67      public void execute()
68          throws MojoExecutionException
69      {
70          export();
71      }
72  
73      protected String getExportDirectory()
74      {
75          return this.exportDirectory;
76      }
77  
78      public void setExportDirectory( String exportDirectory )
79      {
80          this.exportDirectory = exportDirectory;
81      }
82  
83      protected void export()
84          throws MojoExecutionException
85      {
86          super.execute();
87  
88          try
89          {
90              ScmRepository repository = getScmRepository();
91  
92              try
93              {
94                  if ( StringUtils.isNotEmpty( getExportDirectory() ) )
95                  {
96                      File f = new File( getExportDirectory() );
97                      if ( f.exists() )
98                      {
99                          this.getLog().info( "Removing " + getExportDirectory() );
100 
101                         FileUtils.deleteDirectory( getExportDirectory() );
102                     }
103 
104                     f.mkdirs();
105                 }
106             }
107             catch ( IOException e )
108             {
109                 throw new MojoExecutionException( "Cannot remove " + getExportDirectory() );
110             }
111 
112             ExportScmResult result = getScmManager().export( repository, new ScmFileSet(
113                 new File( getExportDirectory() ).getAbsoluteFile() ), getScmVersion( scmVersionType, scmVersion ),
114                                                                       getExportDirectory() );
115 
116             checkResult( result );
117         }
118         catch ( ScmException e )
119         {
120             throw new MojoExecutionException( "Cannot run export command : ", e );
121         }
122     }
123 }