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 java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import java.util.Map.Entry; |
29 | |
import java.util.Properties; |
30 | |
|
31 | |
import org.apache.maven.plugin.AbstractMojo; |
32 | |
import org.apache.maven.plugin.MojoExecutionException; |
33 | |
import org.apache.maven.scm.ScmBranch; |
34 | |
import org.apache.maven.scm.ScmException; |
35 | |
import org.apache.maven.scm.ScmFileSet; |
36 | |
import org.apache.maven.scm.ScmResult; |
37 | |
import org.apache.maven.scm.ScmRevision; |
38 | |
import org.apache.maven.scm.ScmTag; |
39 | |
import org.apache.maven.scm.ScmVersion; |
40 | |
import org.apache.maven.scm.manager.ScmManager; |
41 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
42 | |
import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost; |
43 | |
import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; |
44 | |
import org.apache.maven.scm.repository.ScmRepository; |
45 | |
import org.apache.maven.scm.repository.ScmRepositoryException; |
46 | |
import org.apache.maven.settings.Server; |
47 | |
import org.apache.maven.settings.Settings; |
48 | |
import org.apache.maven.shared.model.fileset.FileSet; |
49 | |
import org.apache.maven.shared.model.fileset.util.FileSetManager; |
50 | |
import org.codehaus.plexus.util.StringUtils; |
51 | |
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; |
52 | |
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | public abstract class AbstractScmMojo |
60 | |
extends AbstractMojo |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private String connectionUrl; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private String developerConnectionUrl; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
private String connectionType; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private File workingDirectory; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private String username; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private String password; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
private String privateKey; |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
private String passphrase; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private String tagBase; |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private String includes; |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
private String excludes; |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
private ScmManager manager; |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
private SecDispatcher secDispatcher; |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private File basedir; |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
private Settings settings; |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
private Properties systemProperties; |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
private Map<String,String> providerImplementations; |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private boolean pushChanges; |
193 | |
|
194 | |
|
195 | |
public void execute() |
196 | |
throws MojoExecutionException |
197 | |
{ |
198 | 0 | if ( systemProperties != null ) |
199 | |
{ |
200 | |
|
201 | 0 | Iterator<Object> iter = systemProperties.keySet().iterator(); |
202 | |
|
203 | 0 | while ( iter.hasNext() ) |
204 | |
{ |
205 | 0 | String key = (String) iter.next(); |
206 | |
|
207 | 0 | String value = systemProperties.getProperty( key ); |
208 | |
|
209 | 0 | System.setProperty( key, value ); |
210 | 0 | } |
211 | |
} |
212 | |
|
213 | 0 | if ( providerImplementations != null && !providerImplementations.isEmpty() ) |
214 | |
{ |
215 | 0 | for ( Entry<String,String> entry : providerImplementations.entrySet() ) |
216 | |
{ |
217 | 0 | String providerType = entry.getKey(); |
218 | 0 | String providerImplementation = entry.getValue(); |
219 | 0 | getLog().info( |
220 | |
"Change the default '" + providerType + "' provider implementation to '" |
221 | |
+ providerImplementation + "'." ); |
222 | 0 | getScmManager().setScmProviderImplementation( providerType, providerImplementation ); |
223 | 0 | } |
224 | |
} |
225 | 0 | } |
226 | |
|
227 | |
protected void setConnectionType( String connectionType ) |
228 | |
{ |
229 | 0 | this.connectionType = connectionType; |
230 | 0 | } |
231 | |
|
232 | |
public String getConnectionUrl() |
233 | |
{ |
234 | 0 | boolean requireDeveloperConnection = !"connection".equals( connectionType.toLowerCase() ); |
235 | 0 | if ( StringUtils.isNotEmpty( connectionUrl ) && !requireDeveloperConnection ) |
236 | |
{ |
237 | 0 | return connectionUrl; |
238 | |
} |
239 | 0 | else if ( StringUtils.isNotEmpty( developerConnectionUrl ) ) |
240 | |
{ |
241 | 0 | return developerConnectionUrl; |
242 | |
} |
243 | 0 | if ( requireDeveloperConnection ) |
244 | |
{ |
245 | 0 | throw new NullPointerException( "You need to define a developerConnectionUrl parameter" ); |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | 0 | throw new NullPointerException( "You need to define a connectionUrl parameter" ); |
250 | |
} |
251 | |
} |
252 | |
|
253 | |
public void setConnectionUrl( String connectionUrl ) |
254 | |
{ |
255 | 0 | this.connectionUrl = connectionUrl; |
256 | 0 | } |
257 | |
|
258 | |
public File getWorkingDirectory() |
259 | |
{ |
260 | 0 | if ( workingDirectory == null ) |
261 | |
{ |
262 | 0 | return basedir; |
263 | |
} |
264 | |
|
265 | 0 | return workingDirectory; |
266 | |
} |
267 | |
|
268 | |
public void setWorkingDirectory( File workingDirectory ) |
269 | |
{ |
270 | 0 | this.workingDirectory = workingDirectory; |
271 | 0 | } |
272 | |
|
273 | |
public ScmManager getScmManager() |
274 | |
{ |
275 | 0 | return manager; |
276 | |
} |
277 | |
|
278 | |
public ScmFileSet getFileSet() |
279 | |
throws IOException |
280 | |
{ |
281 | 0 | if ( includes != null || excludes != null ) |
282 | |
{ |
283 | 0 | return new ScmFileSet( getWorkingDirectory(), includes, excludes ); |
284 | |
} |
285 | |
else |
286 | |
{ |
287 | 0 | return new ScmFileSet( getWorkingDirectory() ); |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
public ScmRepository getScmRepository() |
292 | |
throws ScmException |
293 | |
{ |
294 | |
ScmRepository repository; |
295 | |
|
296 | |
try |
297 | |
{ |
298 | 0 | repository = getScmManager().makeScmRepository( getConnectionUrl() ); |
299 | |
|
300 | 0 | ScmProviderRepository providerRepo = repository.getProviderRepository(); |
301 | |
|
302 | 0 | providerRepo.setPushChanges( pushChanges ); |
303 | |
|
304 | 0 | if ( !StringUtils.isEmpty( username ) ) |
305 | |
{ |
306 | 0 | providerRepo.setUser( username ); |
307 | |
} |
308 | |
|
309 | 0 | if ( !StringUtils.isEmpty( password ) ) |
310 | |
{ |
311 | 0 | providerRepo.setPassword( password ); |
312 | |
} |
313 | |
|
314 | 0 | if ( repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost ) |
315 | |
{ |
316 | 0 | ScmProviderRepositoryWithHost repo = (ScmProviderRepositoryWithHost) repository.getProviderRepository(); |
317 | |
|
318 | 0 | loadInfosFromSettings( repo ); |
319 | |
|
320 | 0 | if ( !StringUtils.isEmpty( username ) ) |
321 | |
{ |
322 | 0 | repo.setUser( username ); |
323 | |
} |
324 | |
|
325 | 0 | if ( !StringUtils.isEmpty( password ) ) |
326 | |
{ |
327 | 0 | repo.setPassword( password ); |
328 | |
} |
329 | |
|
330 | 0 | if ( !StringUtils.isEmpty( privateKey ) ) |
331 | |
{ |
332 | 0 | repo.setPrivateKey( privateKey ); |
333 | |
} |
334 | |
|
335 | 0 | if ( !StringUtils.isEmpty( passphrase ) ) |
336 | |
{ |
337 | 0 | repo.setPassphrase( passphrase ); |
338 | |
} |
339 | |
} |
340 | |
|
341 | 0 | if ( !StringUtils.isEmpty( tagBase ) && repository.getProvider().equals( "svn" ) ) |
342 | |
{ |
343 | 0 | SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository(); |
344 | |
|
345 | 0 | svnRepo.setTagBase( tagBase ); |
346 | |
} |
347 | |
} |
348 | 0 | catch ( ScmRepositoryException e ) |
349 | |
{ |
350 | 0 | if ( !e.getValidationMessages().isEmpty() ) |
351 | |
{ |
352 | 0 | for ( String message : e.getValidationMessages() ) |
353 | |
{ |
354 | 0 | getLog().error( message ); |
355 | |
} |
356 | |
} |
357 | |
|
358 | 0 | throw new ScmException( "Can't load the scm provider.", e ); |
359 | |
} |
360 | 0 | catch ( Exception e ) |
361 | |
{ |
362 | 0 | throw new ScmException( "Can't load the scm provider.", e ); |
363 | 0 | } |
364 | |
|
365 | 0 | return repository; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
private void loadInfosFromSettings( ScmProviderRepositoryWithHost repo ) |
374 | |
{ |
375 | 0 | if ( username == null || password == null ) |
376 | |
{ |
377 | 0 | String host = repo.getHost(); |
378 | |
|
379 | 0 | int port = repo.getPort(); |
380 | |
|
381 | 0 | if ( port > 0 ) |
382 | |
{ |
383 | 0 | host += ":" + port; |
384 | |
} |
385 | |
|
386 | 0 | Server server = this.settings.getServer( host ); |
387 | |
|
388 | 0 | if ( server != null ) |
389 | |
{ |
390 | 0 | if ( username == null ) |
391 | |
{ |
392 | 0 | username = server.getUsername(); |
393 | |
} |
394 | |
|
395 | 0 | if ( password == null ) |
396 | |
{ |
397 | 0 | password = decrypt( server.getPassword(), host ); |
398 | |
} |
399 | |
|
400 | 0 | if ( privateKey == null ) |
401 | |
{ |
402 | 0 | privateKey = server.getPrivateKey(); |
403 | |
} |
404 | |
|
405 | 0 | if ( passphrase == null ) |
406 | |
{ |
407 | 0 | passphrase = decrypt( server.getPassphrase(), host ); |
408 | |
} |
409 | |
} |
410 | |
} |
411 | 0 | } |
412 | |
|
413 | |
private String decrypt( String str, String server ) |
414 | |
{ |
415 | |
try |
416 | |
{ |
417 | 0 | return secDispatcher.decrypt( str ); |
418 | |
} |
419 | 0 | catch ( SecDispatcherException e ) |
420 | |
{ |
421 | 0 | getLog().warn( "Failed to decrypt password/passphrase for server " + server + ", using auth token as is" ); |
422 | 0 | return str; |
423 | |
} |
424 | |
} |
425 | |
|
426 | |
public void checkResult( ScmResult result ) |
427 | |
throws MojoExecutionException |
428 | |
{ |
429 | 0 | if ( !result.isSuccess() ) |
430 | |
{ |
431 | 0 | getLog().error( "Provider message:" ); |
432 | |
|
433 | 0 | getLog().error( result.getProviderMessage() == null ? "" : result.getProviderMessage() ); |
434 | |
|
435 | 0 | getLog().error( "Command output:" ); |
436 | |
|
437 | 0 | getLog().error( result.getCommandOutput() == null ? "" : result.getCommandOutput() ); |
438 | |
|
439 | 0 | throw new MojoExecutionException( |
440 | |
"Command failed." + StringUtils.defaultString( result.getProviderMessage() ) ); |
441 | |
} |
442 | 0 | } |
443 | |
|
444 | |
public String getIncludes() |
445 | |
{ |
446 | 0 | return includes; |
447 | |
} |
448 | |
|
449 | |
public void setIncludes( String includes ) |
450 | |
{ |
451 | 0 | this.includes = includes; |
452 | 0 | } |
453 | |
|
454 | |
public String getExcludes() |
455 | |
{ |
456 | 0 | return excludes; |
457 | |
} |
458 | |
|
459 | |
public void setExcludes( String excludes ) |
460 | |
{ |
461 | 0 | this.excludes = excludes; |
462 | 0 | } |
463 | |
|
464 | |
public ScmVersion getScmVersion( String versionType, String version ) |
465 | |
throws MojoExecutionException |
466 | |
{ |
467 | 0 | if ( StringUtils.isEmpty( versionType ) && StringUtils.isNotEmpty( version ) ) |
468 | |
{ |
469 | 0 | throw new MojoExecutionException( "You must specify the version type." ); |
470 | |
} |
471 | |
|
472 | 0 | if ( StringUtils.isEmpty( version ) ) |
473 | |
{ |
474 | 0 | return null; |
475 | |
} |
476 | |
|
477 | 0 | if ( "branch".equals( versionType ) ) |
478 | |
{ |
479 | 0 | return new ScmBranch( version ); |
480 | |
} |
481 | |
|
482 | 0 | if ( "tag".equals( versionType ) ) |
483 | |
{ |
484 | 0 | return new ScmTag( version ); |
485 | |
} |
486 | |
|
487 | 0 | if ( "revision".equals( versionType ) ) |
488 | |
{ |
489 | 0 | return new ScmRevision( version ); |
490 | |
} |
491 | |
|
492 | 0 | throw new MojoExecutionException( "Unknown '" + versionType + "' version type." ); |
493 | |
} |
494 | |
|
495 | |
protected void handleExcludesIncludesAfterCheckoutAndExport( File checkoutDirectory ) |
496 | |
throws MojoExecutionException |
497 | |
{ |
498 | 0 | List<String> includes = new ArrayList<String>(); |
499 | |
|
500 | 0 | if ( ! StringUtils.isBlank( this.getIncludes() ) ) |
501 | |
{ |
502 | 0 | String[] tokens = StringUtils.split( this.getIncludes(), "," ); |
503 | 0 | for ( int i = 0; i < tokens.length; ++i ) |
504 | |
{ |
505 | 0 | includes.add( tokens[i] ); |
506 | |
} |
507 | |
} |
508 | |
|
509 | 0 | List<String> excludes = new ArrayList<String>(); |
510 | |
|
511 | 0 | if ( ! StringUtils.isBlank( this.getExcludes() ) ) |
512 | |
{ |
513 | 0 | String[] tokens = StringUtils.split( this.getExcludes(), "," ); |
514 | 0 | for ( int i = 0; i < tokens.length; ++i ) |
515 | |
{ |
516 | 0 | excludes.add( tokens[i] ); |
517 | |
} |
518 | |
} |
519 | |
|
520 | 0 | if ( includes.isEmpty() && excludes.isEmpty() ) |
521 | |
{ |
522 | 0 | return; |
523 | |
} |
524 | |
|
525 | 0 | FileSetManager fileSetManager = new FileSetManager(); |
526 | |
|
527 | 0 | FileSet fileset = new FileSet(); |
528 | 0 | fileset.setDirectory( checkoutDirectory.getAbsolutePath() ); |
529 | 0 | fileset.setIncludes( excludes ); |
530 | 0 | fileset.setExcludes( includes ); |
531 | 0 | fileset.setUseDefaultExcludes( false ); |
532 | |
|
533 | |
try |
534 | |
{ |
535 | 0 | fileSetManager.delete( fileset ); |
536 | |
} |
537 | 0 | catch ( IOException e ) |
538 | |
{ |
539 | 0 | throw new MojoExecutionException( "Error found while cleaning up output directory base on excludes/includes configurations.", e ); |
540 | 0 | } |
541 | |
|
542 | 0 | } |
543 | |
} |