View Javadoc

1   package org.apache.maven.scm.client.cli;
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.util.List;
24  
25  import org.apache.maven.scm.ScmBranch;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.ScmRevision;
31  import org.apache.maven.scm.ScmTag;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.checkin.CheckInScmResult;
34  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
35  import org.apache.maven.scm.command.update.UpdateScmResult;
36  import org.apache.maven.scm.manager.NoSuchScmProviderException;
37  import org.apache.maven.scm.manager.ScmManager;
38  import org.apache.maven.scm.repository.ScmRepository;
39  import org.apache.maven.scm.repository.ScmRepositoryException;
40  import org.codehaus.plexus.embed.Embedder;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  /**
44   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
45   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46   *
47   */
48  public class MavenScmCli
49  {
50      private Embedder plexus;
51  
52      private ScmManager scmManager;
53  
54      // ----------------------------------------------------------------------
55      // Lifecycle
56      // ----------------------------------------------------------------------
57  
58      public MavenScmCli()
59          throws Exception
60      {
61          plexus = new Embedder();
62  
63          plexus.start();
64  
65          scmManager = (ScmManager) plexus.lookup( ScmManager.ROLE );
66      }
67  
68      public void stop()
69      {
70          try
71          {
72              plexus.stop();
73          }
74          catch ( Exception ex )
75          {
76              // ignore
77          }
78      }
79  
80      // ----------------------------------------------------------------------
81      //
82      // ----------------------------------------------------------------------
83  
84      public static void main( String[] args )
85      {
86          MavenScmCli cli;
87  
88          try
89          {
90              cli = new MavenScmCli();
91          }
92          catch ( Exception ex )
93          {
94              System.err.println( "Error while starting Maven Scm." );
95  
96              ex.printStackTrace( System.err );
97  
98              return;
99          }
100 
101         String scmUrl;
102 
103         String command;
104 
105         if ( args.length != 3 )
106         {
107             System.err.println(
108                 "Usage: maven-scm-client <command> <working directory> <scm url> [<scmVersion> [<scmVersionType>]]" );
109             System.err.println( "scmVersion is a branch name/tag name/revision number." );
110             System.err.println( "scmVersionType can be 'branch', 'tag', 'revision'. "
111                 + "The default value is 'revision'." );
112 
113             return;
114         }
115 
116         command = args[0];
117 
118         // SCM-641
119         File workingDirectory = new File( args[1] ).getAbsoluteFile();
120 
121         scmUrl = args[2];
122 
123         ScmVersion scmVersion = null;
124         if ( args.length > 3 )
125         {
126             String version = args[3];
127 
128             if ( args.length > 4 )
129             {
130                 String type = args[4];
131 
132                 if ( "tag".equals( type ) )
133                 {
134                     scmVersion = new ScmTag( version );
135                 }
136                 else if ( "branch".equals( type ) )
137                 {
138                     scmVersion = new ScmBranch( version );
139                 }
140                 else if ( "revision".equals( type ) )
141                 {
142                     scmVersion = new ScmRevision( version );
143                 }
144                 else
145                 {
146                     throw new IllegalArgumentException( "'" + type + "' version type isn't known." );
147                 }
148             }
149             else
150             {
151                 scmVersion = new ScmRevision( args[3] );
152             }
153         }
154 
155         cli.execute( scmUrl, command, workingDirectory, scmVersion );
156 
157         cli.stop();
158     }
159 
160     // ----------------------------------------------------------------------
161     //
162     // ----------------------------------------------------------------------
163 
164     public void execute( String scmUrl, String command, File workingDirectory, ScmVersion version )
165     {
166         ScmRepository repository;
167 
168         try
169         {
170             repository = scmManager.makeScmRepository( scmUrl );
171         }
172         catch ( NoSuchScmProviderException ex )
173         {
174             System.err.println( "Could not find a provider." );
175 
176             return;
177         }
178         catch ( ScmRepositoryException ex )
179         {
180             System.err.println( "Error while connecting to the repository" );
181 
182             ex.printStackTrace( System.err );
183 
184             return;
185         }
186 
187         try
188         {
189             if ( command.equals( "checkout" ) )
190             {
191                 checkOut( repository, workingDirectory, version );
192             }
193             else if ( command.equals( "checkin" ) )
194             {
195                 checkIn( repository, workingDirectory, version );
196             }
197             else if ( command.equals( "update" ) )
198             {
199                 update( repository, workingDirectory, version );
200             }
201             else
202             {
203                 System.err.println( "Unknown SCM command '" + command + "'." );
204             }
205         }
206         catch ( ScmException ex )
207         {
208             System.err.println( "Error while executing the SCM command." );
209 
210             ex.printStackTrace( System.err );
211 
212             return;
213         }
214     }
215 
216     // ----------------------------------------------------------------------
217     //
218     // ----------------------------------------------------------------------
219 
220     private void checkOut( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
221         throws ScmException
222     {
223         if ( workingDirectory.exists() )
224         {
225             System.err.println( "The working directory already exist: '" + workingDirectory.getAbsolutePath()
226                 + "'." );
227 
228             return;
229         }
230 
231         if ( !workingDirectory.mkdirs() )
232         {
233             System.err.println(
234                 "Error while making the working directory: '" + workingDirectory.getAbsolutePath() + "'." );
235 
236             return;
237         }
238 
239         CheckOutScmResult result = scmManager.checkOut( scmRepository, new ScmFileSet( workingDirectory ), version );
240 
241         if ( !result.isSuccess() )
242         {
243             showError( result );
244 
245             return;
246         }
247 
248         List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
249 
250         System.out.println( "Checked out these files: " );
251 
252         for ( ScmFile file : checkedOutFiles )
253         {
254             System.out.println( " " + file.getPath() );
255         }
256     }
257 
258     private void checkIn( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
259         throws ScmException
260     {
261         if ( !workingDirectory.exists() )
262         {
263             System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath()
264                 + "'." );
265 
266             return;
267         }
268 
269         String message = "";
270 
271         CheckInScmResult result =
272             scmManager.checkIn( scmRepository, new ScmFileSet( workingDirectory ), version, message );
273 
274         if ( !result.isSuccess() )
275         {
276             showError( result );
277 
278             return;
279         }
280 
281         List<ScmFile> checkedInFiles = result.getCheckedInFiles();
282 
283         System.out.println( "Checked in these files: " );
284 
285         for ( ScmFile file : checkedInFiles )
286         {
287             System.out.println( " " + file.getPath() );
288         }
289     }
290 
291     private void update( ScmRepository scmRepository, File workingDirectory, ScmVersion version )
292         throws ScmException
293     {
294         if ( !workingDirectory.exists() )
295         {
296             System.err.println( "The working directory doesn't exist: '" + workingDirectory.getAbsolutePath()
297                 + "'." );
298 
299             return;
300         }
301 
302         UpdateScmResult result = scmManager.update( scmRepository, new ScmFileSet( workingDirectory ), version );
303 
304         if ( !result.isSuccess() )
305         {
306             showError( result );
307 
308             return;
309         }
310 
311         List<ScmFile> updatedFiles = result.getUpdatedFiles();
312 
313         System.out.println( "Updated these files: " );
314 
315         for ( ScmFile file : updatedFiles )
316         {
317             System.out.println( " " + file.getPath() );
318         }
319     }
320 
321     // ----------------------------------------------------------------------
322     //
323     // ----------------------------------------------------------------------
324 
325     private void showError( ScmResult result )
326     {
327         System.err.println( "There was a error while executing the SCM command." );
328 
329         String providerMessage = result.getProviderMessage();
330 
331         if ( !StringUtils.isEmpty( providerMessage ) )
332         {
333             System.err.println( "Error message from the provider: " + providerMessage );
334         }
335         else
336         {
337             System.err.println( "The provider didn't give a error message." );
338         }
339 
340         String output = result.getCommandOutput();
341 
342         if ( !StringUtils.isEmpty( output ) )
343         {
344             System.err.println( "Command output:" );
345 
346             System.err.println( output );
347         }
348     }
349 }