1 package org.apache.maven.shared.release.exec;
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.FileWriter;
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.OptionBuilder;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.PosixParser;
33 import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
34 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
35 import org.apache.maven.shared.invoker.DefaultInvoker;
36 import org.apache.maven.shared.invoker.InvocationOutputHandler;
37 import org.apache.maven.shared.invoker.InvocationRequest;
38 import org.apache.maven.shared.invoker.InvocationResult;
39 import org.apache.maven.shared.invoker.Invoker;
40 import org.apache.maven.shared.invoker.InvokerLogger;
41 import org.apache.maven.shared.invoker.MavenInvocationException;
42 import org.apache.maven.shared.release.ReleaseResult;
43 import org.apache.maven.shared.release.env.ReleaseEnvironment;
44 import org.codehaus.plexus.logging.Logger;
45 import org.codehaus.plexus.util.IOUtil;
46 import org.codehaus.plexus.util.cli.CommandLineUtils;
47
48
49
50
51
52
53 @SuppressWarnings( "static-access" )
54 public class InvokerMavenExecutor
55 extends AbstractMavenExecutor
56 {
57
58 private static final Options OPTIONS = new Options();
59
60 private static final char SET_SYSTEM_PROPERTY = 'D';
61
62 private static final char OFFLINE = 'o';
63
64 private static final char REACTOR = 'r';
65
66 private static final char QUIET = 'q';
67
68 private static final char DEBUG = 'X';
69
70 private static final char ERRORS = 'e';
71
72 private static final char NON_RECURSIVE = 'N';
73
74 private static final char UPDATE_SNAPSHOTS = 'U';
75
76 private static final char ACTIVATE_PROFILES = 'P';
77
78 private static final String FORCE_PLUGIN_UPDATES = "cpu";
79
80 private static final String FORCE_PLUGIN_UPDATES2 = "up";
81
82 private static final String SUPPRESS_PLUGIN_UPDATES = "npu";
83
84 private static final String SUPPRESS_PLUGIN_REGISTRY = "npr";
85
86 private static final char CHECKSUM_FAILURE_POLICY = 'C';
87
88 private static final char CHECKSUM_WARNING_POLICY = 'c';
89
90 private static final char ALTERNATE_USER_SETTINGS = 's';
91
92 private static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
93
94 private static final String FAIL_FAST = "ff";
95
96 private static final String FAIL_AT_END = "fae";
97
98 private static final String FAIL_NEVER = "fn";
99
100 private static final String ALTERNATE_POM_FILE = "f";
101
102 private static final String THREADS = "T";
103
104
105 static
106 {
107 OPTIONS.addOption(
108 OptionBuilder.withLongOpt( "define" ).hasArg().withDescription( "Define a system property" ).create(
109 SET_SYSTEM_PROPERTY ) );
110
111 OPTIONS.addOption( OptionBuilder.withLongOpt( "offline" ).withDescription( "Work offline" ).create( OFFLINE ) );
112
113 OPTIONS.addOption(
114 OptionBuilder.withLongOpt( "quiet" ).withDescription( "Quiet output - only show errors" ).create( QUIET ) );
115
116 OPTIONS.addOption(
117 OptionBuilder.withLongOpt( "debug" ).withDescription( "Produce execution debug output" ).create( DEBUG ) );
118
119 OPTIONS.addOption(
120 OptionBuilder.withLongOpt( "errors" ).withDescription( "Produce execution error messages" ).create(
121 ERRORS ) );
122
123 OPTIONS.addOption( OptionBuilder.withLongOpt( "reactor" ).withDescription(
124 "Execute goals for project found in the reactor" ).create( REACTOR ) );
125
126 OPTIONS.addOption(
127 OptionBuilder.withLongOpt( "non-recursive" ).withDescription( "Do not recurse into sub-projects" ).create(
128 NON_RECURSIVE ) );
129
130 OPTIONS.addOption( OptionBuilder.withLongOpt( "update-snapshots" ).withDescription(
131 "Forces a check for updated releases and snapshots on remote repositories" ).create( UPDATE_SNAPSHOTS ) );
132
133 OPTIONS.addOption( OptionBuilder.withLongOpt( "activate-profiles" ).withDescription(
134 "Comma-delimited list of profiles to activate" ).hasArg().create( ACTIVATE_PROFILES ) );
135
136 OPTIONS.addOption( OptionBuilder.withLongOpt( "check-plugin-updates" ).withDescription(
137 "Force upToDate check for any relevant registered plugins" ).create( FORCE_PLUGIN_UPDATES ) );
138
139 OPTIONS.addOption( OptionBuilder.withLongOpt( "update-plugins" ).withDescription(
140 "Synonym for " + FORCE_PLUGIN_UPDATES ).create( FORCE_PLUGIN_UPDATES2 ) );
141
142 OPTIONS.addOption( OptionBuilder.withLongOpt( "no-plugin-updates" ).withDescription(
143 "Suppress upToDate check for any relevant registered plugins" ).create( SUPPRESS_PLUGIN_UPDATES ) );
144
145 OPTIONS.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ).withDescription(
146 "Don't use ~/.m2/plugin-registry.xml for plugin versions" ).create( SUPPRESS_PLUGIN_REGISTRY ) );
147
148 OPTIONS.addOption( OptionBuilder.withLongOpt( "strict-checksums" ).withDescription(
149 "Fail the build if checksums don't match" ).create( CHECKSUM_FAILURE_POLICY ) );
150
151 OPTIONS.addOption(
152 OptionBuilder.withLongOpt( "lax-checksums" ).withDescription( "Warn if checksums don't match" ).create(
153 CHECKSUM_WARNING_POLICY ) );
154
155 OPTIONS.addOption( OptionBuilder.withLongOpt( "settings" ).withDescription(
156 "Alternate path for the user settings file" ).hasArg().create( ALTERNATE_USER_SETTINGS ) );
157
158 OPTIONS.addOption( OptionBuilder.withLongOpt( "global-settings" ).withDescription(
159 " Alternate path for the global settings file" ).hasArg().create( ALTERNATE_GLOBAL_SETTINGS ) );
160
161 OPTIONS.addOption( OptionBuilder.withLongOpt( "fail-fast" ).withDescription(
162 "Stop at first failure in reactorized builds" ).create( FAIL_FAST ) );
163
164 OPTIONS.addOption( OptionBuilder.withLongOpt( "fail-at-end" ).withDescription(
165 "Only fail the build afterwards; allow all non-impacted builds to continue" ).create( FAIL_AT_END ) );
166
167 OPTIONS.addOption( OptionBuilder.withLongOpt( "fail-never" ).withDescription(
168 "NEVER fail the build, regardless of project result" ).create( FAIL_NEVER ) );
169
170 OPTIONS.addOption( OptionBuilder.withLongOpt( "file" ).withDescription(
171 "Force the use of an alternate POM file." ).hasArg().create( ALTERNATE_POM_FILE ) );
172
173 OPTIONS.addOption( OptionBuilder.withLongOpt( "threads" ).withDescription(
174 "Thread count, for instance 2.0C where C is core multiplied" ).hasArg().create( THREADS ) );
175 }
176
177
178 protected void setupRequest( InvocationRequest req,
179 InvokerLogger bridge,
180 String additionalArguments )
181 throws MavenExecutorException
182 {
183 try
184 {
185 String[] args = CommandLineUtils.translateCommandline( additionalArguments );
186 CommandLine cli = new PosixParser().parse( OPTIONS, args );
187
188 if ( cli.hasOption( SET_SYSTEM_PROPERTY ) )
189 {
190 String[] properties = cli.getOptionValues( SET_SYSTEM_PROPERTY );
191 Properties props = new Properties();
192 for ( int i = 0; i < properties.length; i++ )
193 {
194 String property = properties[i];
195 String name, value;
196 int sep = property.indexOf( "=" );
197 if ( sep <= 0 )
198 {
199 name = property.trim();
200 value = "true";
201 }
202 else
203 {
204 name = property.substring( 0, sep ).trim();
205 value = property.substring( sep + 1 ).trim();
206 }
207 props.setProperty( name, value );
208 }
209
210 req.setProperties( props );
211 }
212
213 if ( cli.hasOption( OFFLINE ) )
214 {
215 req.setOffline( true );
216 }
217
218 if ( cli.hasOption( QUIET ) )
219 {
220
221 req.setDebug( false );
222 }
223 else if ( cli.hasOption( DEBUG ) )
224 {
225 req.setDebug( true );
226 }
227 else if ( cli.hasOption( ERRORS ) )
228 {
229 req.setShowErrors( true );
230 }
231
232 if ( cli.hasOption( REACTOR ) )
233 {
234 req.setRecursive( true );
235 }
236 else if ( cli.hasOption( NON_RECURSIVE ) )
237 {
238 req.setRecursive( false );
239 }
240
241 if ( cli.hasOption( UPDATE_SNAPSHOTS ) )
242 {
243 req.setUpdateSnapshots( true );
244 }
245
246 if ( cli.hasOption( ACTIVATE_PROFILES ) )
247 {
248 String[] profiles = cli.getOptionValues( ACTIVATE_PROFILES );
249
250 if ( profiles != null )
251 {
252 req.setProfiles( Arrays.asList( profiles ) );
253 }
254 }
255
256 if ( cli.hasOption( FORCE_PLUGIN_UPDATES ) || cli.hasOption( FORCE_PLUGIN_UPDATES2 ) )
257 {
258 getLogger().warn( "Forcing plugin updates is not supported currently." );
259 }
260 else if ( cli.hasOption( SUPPRESS_PLUGIN_UPDATES ) )
261 {
262 req.setNonPluginUpdates( true );
263 }
264
265 if ( cli.hasOption( SUPPRESS_PLUGIN_REGISTRY ) )
266 {
267 getLogger().warn( "Explicit suppression of the plugin registry is not supported currently." );
268 }
269
270 if ( cli.hasOption( CHECKSUM_FAILURE_POLICY ) )
271 {
272 req.setGlobalChecksumPolicy( InvocationRequest.CHECKSUM_POLICY_FAIL );
273 }
274 else if ( cli.hasOption( CHECKSUM_WARNING_POLICY ) )
275 {
276 req.setGlobalChecksumPolicy( InvocationRequest.CHECKSUM_POLICY_WARN );
277 }
278
279 if ( cli.hasOption( ALTERNATE_USER_SETTINGS ) )
280 {
281 req.setUserSettingsFile( new File( cli.getOptionValue( ALTERNATE_USER_SETTINGS ) ) );
282 }
283
284 if ( cli.hasOption( ALTERNATE_GLOBAL_SETTINGS ) )
285 {
286 req.setGlobalSettingsFile( new File( cli.getOptionValue( ALTERNATE_GLOBAL_SETTINGS ) ) );
287 }
288
289 if ( cli.hasOption( FAIL_AT_END ) )
290 {
291 req.setFailureBehavior( InvocationRequest.REACTOR_FAIL_AT_END );
292 }
293 else if ( cli.hasOption( FAIL_FAST ) )
294 {
295 req.setFailureBehavior( InvocationRequest.REACTOR_FAIL_FAST );
296 }
297 if ( cli.hasOption( FAIL_NEVER ) )
298 {
299 req.setFailureBehavior( InvocationRequest.REACTOR_FAIL_NEVER );
300 }
301 if ( cli.hasOption( ALTERNATE_POM_FILE ) )
302 {
303 if ( req.getPomFileName() != null )
304 {
305 getLogger().info( "pomFileName is already set, ignoring the -f argument" );
306 }
307 else
308 {
309 req.setPomFileName( cli.getOptionValue( ALTERNATE_POM_FILE ) );
310 }
311 }
312
313 if ( cli.hasOption( THREADS ) )
314 {
315 req.setThreads( cli.getOptionValue( THREADS ) );
316 }
317 }
318 catch ( Exception e )
319 {
320 throw new MavenExecutorException( "Failed to re-parse additional arguments for Maven invocation.", e );
321 }
322 }
323
324 @Override
325 public void executeGoals( File workingDirectory, List<String> goals, ReleaseEnvironment releaseEnvironment,
326 boolean interactive, String additionalArguments, String pomFileName,
327 ReleaseResult result )
328 throws MavenExecutorException
329 {
330 InvocationOutputHandler handler = getOutputHandler();
331 InvokerLogger bridge = getInvokerLogger();
332
333 File mavenPath = null;
334
335 if ( releaseEnvironment.getMavenHome() != null )
336 {
337 mavenPath = releaseEnvironment.getMavenHome();
338 }
339 else
340 {
341 String mavenHome = System.getProperty( "maven.home" );
342 if ( mavenHome == null )
343 {
344 mavenHome = System.getenv("MAVEN_HOME");
345 }
346 if ( mavenHome == null )
347 {
348 mavenHome = System.getenv("M2_HOME");
349 }
350 mavenPath = mavenHome == null ? null : new File( mavenHome );
351 }
352 Invoker invoker =
353 new DefaultInvoker().setMavenHome( mavenPath ).setLogger( bridge ).setOutputHandler(
354 handler ).setErrorHandler( handler );
355
356 InvocationRequest req =
357 new DefaultInvocationRequest().setDebug( getLogger().isDebugEnabled() ).setBaseDirectory(
358 workingDirectory ).setInteractive( interactive );
359
360 if ( pomFileName != null )
361 {
362 req.setPomFileName( pomFileName );
363 }
364
365 File settingsFile = null;
366 if ( releaseEnvironment.getSettings() != null )
367 {
368
369 try
370 {
371 settingsFile = File.createTempFile( "release-settings", ".xml" );
372 SettingsXpp3Writer writer = getSettingsWriter();
373 FileWriter fileWriter = null;
374 try
375 {
376 fileWriter = new FileWriter( settingsFile );
377 writer.write( fileWriter, encryptSettings( releaseEnvironment.getSettings() ) );
378 }
379 finally
380 {
381 IOUtil.close( fileWriter );
382 }
383 req.setUserSettingsFile( settingsFile );
384 }
385 catch ( IOException e )
386 {
387 throw new MavenExecutorException( "Could not create temporary file for release settings.xml", e );
388 }
389 }
390 try
391 {
392 File localRepoDir = releaseEnvironment.getLocalRepositoryDirectory();
393 if ( localRepoDir != null )
394 {
395 req.setLocalRepositoryDirectory( localRepoDir );
396 }
397
398 setupRequest( req, bridge, additionalArguments );
399
400 req.setGoals( goals );
401
402 try
403 {
404 InvocationResult invocationResult = invoker.execute( req );
405
406 if ( invocationResult.getExecutionException() != null )
407 {
408 throw new MavenExecutorException( "Error executing Maven.",
409 invocationResult.getExecutionException() );
410 }
411 if ( invocationResult.getExitCode() != 0 )
412 {
413 throw new MavenExecutorException(
414 "Maven execution failed, exit code: \'" + invocationResult.getExitCode() + "\'",
415 invocationResult.getExitCode(), "", "" );
416 }
417 }
418 catch ( MavenInvocationException e )
419 {
420 throw new MavenExecutorException( "Failed to invoke Maven build.", e );
421 }
422 }
423 finally
424 {
425 if ( settingsFile != null && settingsFile.exists() && !settingsFile.delete() )
426 {
427 settingsFile.deleteOnExit();
428 }
429 }
430 }
431
432 protected InvokerLogger getInvokerLogger()
433 {
434 return new LoggerBridge( getLogger() );
435 }
436
437 protected InvocationOutputHandler getOutputHandler()
438 {
439 return new Handler( getLogger() );
440 }
441
442 private static final class Handler
443 implements InvocationOutputHandler
444 {
445 private Logger logger;
446
447 Handler( Logger logger )
448 {
449 this.logger = logger;
450 }
451
452 public void consumeLine( String line )
453 {
454 logger.info( line );
455 }
456 }
457
458 private static final class LoggerBridge
459 implements InvokerLogger
460 {
461
462 private Logger logger;
463
464 LoggerBridge( Logger logger )
465 {
466 this.logger = logger;
467 }
468
469 public void debug( String message, Throwable error )
470 {
471 logger.debug( message, error );
472 }
473
474 public void debug( String message )
475 {
476 logger.debug( message );
477 }
478
479 public void error( String message, Throwable error )
480 {
481 logger.error( message, error );
482 }
483
484 public void error( String message )
485 {
486 logger.error( message );
487 }
488
489 public void fatalError( String message, Throwable error )
490 {
491 logger.fatalError( message, error );
492 }
493
494 public void fatalError( String message )
495 {
496 logger.fatalError( message );
497 }
498
499 public Logger getChildLogger( String message )
500 {
501 return logger.getChildLogger( message );
502 }
503
504 public String getName()
505 {
506 return logger.getName();
507 }
508
509 public int getThreshold()
510 {
511 return logger.getThreshold();
512 }
513
514 public void info( String message, Throwable error )
515 {
516 logger.info( message, error );
517 }
518
519 public void info( String message )
520 {
521 logger.info( message );
522 }
523
524 public boolean isDebugEnabled()
525 {
526 return logger.isDebugEnabled();
527 }
528
529 public boolean isErrorEnabled()
530 {
531 return logger.isErrorEnabled();
532 }
533
534 public boolean isFatalErrorEnabled()
535 {
536 return logger.isFatalErrorEnabled();
537 }
538
539 public boolean isInfoEnabled()
540 {
541 return logger.isInfoEnabled();
542 }
543
544 public boolean isWarnEnabled()
545 {
546 return logger.isWarnEnabled();
547 }
548
549 public void setThreshold( int level )
550 {
551
552
553
554 }
555
556 public void warn( String message, Throwable error )
557 {
558 logger.warn( message, error );
559 }
560
561 public void warn( String message )
562 {
563 logger.warn( message );
564 }
565
566 }
567
568 }