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 java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmResult;
31  import org.apache.maven.scm.repository.ScmRepository;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  /**
35   * Get a fresh copy of the latest source from the configured scm url.
36   *
37   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
38   */
39  @Mojo( name = "checkout", requiresProject = false )
40  public class CheckoutMojo
41      extends AbstractScmMojo
42  {
43      /**
44       * Use Export instead of checkout
45       */
46      @Parameter( property = "useExport", defaultValue = "false" )
47      private boolean useExport;
48      
49      /**
50       * The directory to checkout the sources to for the bootstrap and checkout goals.
51       */
52      @Parameter( property = "checkoutDirectory", defaultValue = "${project.build.directory}/checkout" )
53      private File checkoutDirectory;
54  
55      /**
56       * Skip checkout if checkoutDirectory exists.
57       */
58      @Parameter( property = "skipCheckoutIfExists", defaultValue = "false" )
59      private boolean skipCheckoutIfExists = false;
60  
61      /**
62       * The version type (branch/tag/revision) of scmVersion.
63       */
64      @Parameter( property = "scmVersionType" )
65      private String scmVersionType;
66  
67      /**
68       * The version (revision number/branch name/tag name).
69       */
70      @Parameter( property = "scmVersion" )
71      private String scmVersion;
72  
73      /**
74       * allow extended mojo (ie BootStrap ) to see checkout result
75       */
76      private ScmResult checkoutResult;
77  
78      /** {@inheritDoc} */
79      public void execute()
80          throws MojoExecutionException
81      {
82          super.execute();
83  
84          //skip checkout if checkout directory is already created. See SCM-201
85          checkoutResult = null;
86          if ( !getCheckoutDirectory().isDirectory() || !this.skipCheckoutIfExists )
87          {
88              checkoutResult = checkout();
89          }
90      }
91  
92      protected File getCheckoutDirectory()
93      {
94          return this.checkoutDirectory;
95      }
96  
97      public void setCheckoutDirectory( File checkoutDirectory )
98      {
99          this.checkoutDirectory = checkoutDirectory;
100     }
101 
102     protected ScmResult checkout()
103         throws MojoExecutionException
104     {
105         try
106         {
107             ScmRepository repository = getScmRepository();
108 
109             this.prepareOutputDirectory( getCheckoutDirectory() );
110             
111             ScmResult result = null;
112             
113             ScmFileSet fileSet = new ScmFileSet( getCheckoutDirectory().getAbsoluteFile() );
114             if ( useExport )
115             {
116                 result = getScmManager().export( repository,fileSet, getScmVersion( scmVersionType, scmVersion ) );
117             }
118             else
119             {
120                 result = getScmManager().checkOut( repository,fileSet , getScmVersion( scmVersionType, scmVersion ) );
121             }
122 
123             checkResult( result );
124             
125            
126             handleExcludesIncludesAfterCheckoutAndExport( this.checkoutDirectory );
127 
128             return result;
129         }
130         catch ( ScmException e )
131         {
132             throw new MojoExecutionException( "Cannot run checkout command : ", e );
133         }
134     }
135 
136     private void prepareOutputDirectory( File ouputDirectory )
137         throws MojoExecutionException
138     {
139         try
140         {
141             this.getLog().info( "Removing " + ouputDirectory );
142 
143             FileUtils.deleteDirectory( getCheckoutDirectory() );
144         }
145         catch ( IOException e )
146         {
147             throw new MojoExecutionException( "Cannot remove " + ouputDirectory );
148         }
149         
150         if ( !getCheckoutDirectory().mkdirs() )
151         {
152             throw new MojoExecutionException( "Cannot create " + ouputDirectory );
153         }
154     }
155     
156     protected ScmResult getCheckoutResult()
157     {
158         return checkoutResult;
159     }
160     
161 
162 }