1 package org.apache.maven.scm.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.scm.ScmBranch;
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmFileSet;
27 import org.apache.maven.scm.ScmResult;
28 import org.apache.maven.scm.ScmRevision;
29 import org.apache.maven.scm.ScmTag;
30 import org.apache.maven.scm.ScmVersion;
31 import org.apache.maven.scm.manager.ScmManager;
32 import org.apache.maven.scm.provider.ScmProviderRepository;
33 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
34 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
35 import org.apache.maven.scm.repository.ScmRepository;
36 import org.apache.maven.scm.repository.ScmRepositoryException;
37 import org.apache.maven.settings.Server;
38 import org.apache.maven.settings.Settings;
39 import org.codehaus.plexus.util.StringUtils;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.Iterator;
44 import java.util.Map;
45 import java.util.Properties;
46
47
48
49
50
51 public abstract class AbstractScmMojo
52 extends AbstractMojo
53 {
54
55
56
57
58
59 private String connectionUrl;
60
61
62
63
64
65
66 private String developerConnectionUrl;
67
68
69
70
71
72
73 private String connectionType;
74
75
76
77
78
79
80 private File workingDirectory;
81
82
83
84
85
86
87 private String username;
88
89
90
91
92
93
94 private String password;
95
96
97
98
99
100
101 private String privateKey;
102
103
104
105
106
107
108 private String passphrase;
109
110
111
112
113
114
115
116
117 private String tagBase;
118
119
120
121
122
123
124 private String includes;
125
126
127
128
129
130
131 private String excludes;
132
133
134
135
136
137
138 private ScmManager manager;
139
140
141
142
143
144
145
146 private File basedir;
147
148
149
150
151
152
153 private Settings settings;
154
155
156
157
158
159
160 private Properties systemProperties;
161
162
163
164
165
166
167 private Map providerImplementations;
168
169 public void execute()
170 throws MojoExecutionException
171 {
172 if ( systemProperties != null )
173 {
174
175 Iterator iter = systemProperties.keySet().iterator();
176
177 while ( iter.hasNext() )
178 {
179 String key = (String) iter.next();
180
181 String value = systemProperties.getProperty( key );
182
183 System.setProperty( key, value );
184 }
185 }
186
187 if ( providerImplementations != null )
188 {
189 for ( Iterator i = providerImplementations.keySet().iterator(); i.hasNext(); )
190 {
191 String providerType = (String) i.next();
192 String providerImplementation = (String) providerImplementations.get( providerType );
193 getLog().info( "Change the default '" + providerType + "' provider implementation to '" +
194 providerImplementation + "'." );
195 getScmManager().setScmProviderImplementation( providerType, providerImplementation );
196 }
197 }
198 }
199
200 protected void setConnectionType( String connectionType )
201 {
202 this.connectionType = connectionType;
203 }
204
205 public String getConnectionUrl()
206 {
207 boolean requireDeveloperConnection = !"connection".equals( connectionType.toLowerCase() );
208 if ( StringUtils.isNotEmpty( connectionUrl ) && !requireDeveloperConnection )
209 {
210 return connectionUrl;
211 }
212 else if ( StringUtils.isNotEmpty( developerConnectionUrl ) )
213 {
214 return developerConnectionUrl;
215 }
216 if ( requireDeveloperConnection )
217 {
218 throw new NullPointerException( "You need to define a developerConnectionUrl parameter" );
219 }
220 else
221 {
222 throw new NullPointerException( "You need to define a connectionUrl parameter" );
223 }
224 }
225
226 public void setConnectionUrl( String connectionUrl )
227 {
228 this.connectionUrl = connectionUrl;
229 }
230
231 public File getWorkingDirectory()
232 {
233 if ( workingDirectory == null )
234 {
235 return basedir;
236 }
237
238 return workingDirectory;
239 }
240
241 public void setWorkingDirectory( File workingDirectory )
242 {
243 this.workingDirectory = workingDirectory;
244 }
245
246 public ScmManager getScmManager()
247 {
248 return manager;
249 }
250
251 public ScmFileSet getFileSet()
252 throws IOException
253 {
254 if ( includes != null || excludes != null )
255 {
256 return new ScmFileSet( getWorkingDirectory(), includes, excludes );
257 }
258 else
259 {
260 return new ScmFileSet( getWorkingDirectory() );
261 }
262 }
263
264 public ScmRepository getScmRepository()
265 throws ScmException
266 {
267 ScmRepository repository;
268
269 try
270 {
271 repository = getScmManager().makeScmRepository( getConnectionUrl() );
272
273 ScmProviderRepository providerRepo = repository.getProviderRepository();
274
275 if ( !StringUtils.isEmpty( username ) )
276 {
277 providerRepo.setUser( username );
278 }
279
280 if ( !StringUtils.isEmpty( password ) )
281 {
282 providerRepo.setPassword( password );
283 }
284
285 if ( repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost )
286 {
287 ScmProviderRepositoryWithHost repo = (ScmProviderRepositoryWithHost) repository.getProviderRepository();
288
289 loadInfosFromSettings( repo );
290
291 if ( !StringUtils.isEmpty( username ) )
292 {
293 repo.setUser( username );
294 }
295
296 if ( !StringUtils.isEmpty( password ) )
297 {
298 repo.setPassword( password );
299 }
300
301 if ( !StringUtils.isEmpty( privateKey ) )
302 {
303 repo.setPrivateKey( privateKey );
304 }
305
306 if ( !StringUtils.isEmpty( passphrase ) )
307 {
308 repo.setPassphrase( passphrase );
309 }
310 }
311
312 if ( !StringUtils.isEmpty( tagBase ) && repository.getProvider().equals( "svn" ) )
313 {
314 SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
315
316 svnRepo.setTagBase( tagBase );
317 }
318 }
319 catch ( ScmRepositoryException e )
320 {
321 if ( !e.getValidationMessages().isEmpty() )
322 {
323 for ( Iterator i = e.getValidationMessages().iterator(); i.hasNext(); )
324 {
325 String message = (String) i.next();
326 getLog().error( message );
327 }
328 }
329
330 throw new ScmException( "Can't load the scm provider.", e );
331 }
332 catch ( Exception e )
333 {
334 throw new ScmException( "Can't load the scm provider.", e );
335 }
336
337 return repository;
338 }
339
340
341
342
343
344
345 private void loadInfosFromSettings( ScmProviderRepositoryWithHost repo )
346 {
347 if ( username == null || password == null )
348 {
349 String host = repo.getHost();
350
351 int port = repo.getPort();
352
353 if ( port > 0 )
354 {
355 host += ":" + port;
356 }
357
358 Server server = this.settings.getServer( host );
359
360 if ( server != null )
361 {
362 if ( username == null )
363 {
364 username = this.settings.getServer( host ).getUsername();
365 }
366
367 if ( password == null )
368 {
369 password = this.settings.getServer( host ).getPassword();
370 }
371
372 if ( privateKey == null )
373 {
374 privateKey = this.settings.getServer( host ).getPrivateKey();
375 }
376
377 if ( passphrase == null )
378 {
379 passphrase = this.settings.getServer( host ).getPassphrase();
380 }
381 }
382 }
383 }
384
385 public void checkResult( ScmResult result )
386 throws MojoExecutionException
387 {
388 if ( !result.isSuccess() )
389 {
390 getLog().error( "Provider message:" );
391
392 getLog().error( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
393
394 getLog().error( "Command output:" );
395
396 getLog().error( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
397
398 throw new MojoExecutionException(
399 "Command failed." + StringUtils.defaultString( result.getProviderMessage() ) );
400 }
401 }
402
403 public String getIncludes()
404 {
405 return includes;
406 }
407
408 public void setIncludes( String includes )
409 {
410 this.includes = includes;
411 }
412
413 public String getExcludes()
414 {
415 return excludes;
416 }
417
418 public void setExcludes( String excludes )
419 {
420 this.excludes = excludes;
421 }
422
423 public ScmVersion getScmVersion( String versionType, String version )
424 throws MojoExecutionException
425 {
426 if ( StringUtils.isEmpty( versionType ) && StringUtils.isNotEmpty( version ) )
427 {
428 throw new MojoExecutionException( "You must specify the version type." );
429 }
430
431 if ( StringUtils.isEmpty( version ) )
432 {
433 return null;
434 }
435
436 if ( "branch".equals( versionType ) )
437 {
438 return new ScmBranch( version );
439 }
440
441 if ( "tag".equals( versionType ) )
442 {
443 return new ScmTag( version );
444 }
445
446 if ( "revision".equals( versionType ) )
447 {
448 return new ScmRevision( version );
449 }
450
451 throw new MojoExecutionException( "Unknown '" + versionType + "' version type." );
452 }
453 }