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