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 Invoker invoker =
334 new DefaultInvoker().setMavenHome( releaseEnvironment.getMavenHome() ).setLogger( bridge ).setOutputHandler(
335 handler ).setErrorHandler( handler );
336
337 InvocationRequest req =
338 new DefaultInvocationRequest().setDebug( getLogger().isDebugEnabled() ).setBaseDirectory(
339 workingDirectory ).setInteractive( interactive );
340
341 if ( pomFileName != null )
342 {
343 req.setPomFileName( pomFileName );
344 }
345
346 File settingsFile = null;
347 if ( releaseEnvironment.getSettings() != null )
348 {
349
350 try
351 {
352 settingsFile = File.createTempFile( "release-settings", ".xml" );
353 SettingsXpp3Writer writer = getSettingsWriter();
354 FileWriter fileWriter = null;
355 try
356 {
357 fileWriter = new FileWriter( settingsFile );
358 writer.write( fileWriter, encryptSettings( releaseEnvironment.getSettings() ) );
359 }
360 finally
361 {
362 IOUtil.close( fileWriter );
363 }
364 req.setUserSettingsFile( settingsFile );
365 }
366 catch ( IOException e )
367 {
368 throw new MavenExecutorException( "Could not create temporary file for release settings.xml", e );
369 }
370 }
371 try
372 {
373 File localRepoDir = releaseEnvironment.getLocalRepositoryDirectory();
374 if ( localRepoDir != null )
375 {
376 req.setLocalRepositoryDirectory( localRepoDir );
377 }
378
379 setupRequest( req, bridge, additionalArguments );
380
381 req.setGoals( goals );
382
383 try
384 {
385 InvocationResult invocationResult = invoker.execute( req );
386
387 if ( invocationResult.getExecutionException() != null )
388 {
389 throw new MavenExecutorException( "Error executing Maven.",
390 invocationResult.getExecutionException() );
391 }
392 if ( invocationResult.getExitCode() != 0 )
393 {
394 throw new MavenExecutorException(
395 "Maven execution failed, exit code: \'" + invocationResult.getExitCode() + "\'",
396 invocationResult.getExitCode(), "", "" );
397 }
398 }
399 catch ( MavenInvocationException e )
400 {
401 throw new MavenExecutorException( "Failed to invoke Maven build.", e );
402 }
403 }
404 finally
405 {
406 if ( settingsFile != null && settingsFile.exists() && !settingsFile.delete() )
407 {
408 settingsFile.deleteOnExit();
409 }
410 }
411 }
412
413 protected InvokerLogger getInvokerLogger()
414 {
415 return new LoggerBridge( getLogger() );
416 }
417
418 protected InvocationOutputHandler getOutputHandler()
419 {
420 return new Handler( getLogger() );
421 }
422
423 private static final class Handler
424 implements InvocationOutputHandler
425 {
426 private Logger logger;
427
428 Handler( Logger logger )
429 {
430 this.logger = logger;
431 }
432
433 public void consumeLine( String line )
434 {
435 logger.info( line );
436 }
437 }
438
439 private static final class LoggerBridge
440 implements InvokerLogger
441 {
442
443 private Logger logger;
444
445 LoggerBridge( Logger logger )
446 {
447 this.logger = logger;
448 }
449
450 public void debug( String message, Throwable error )
451 {
452 logger.debug( message, error );
453 }
454
455 public void debug( String message )
456 {
457 logger.debug( message );
458 }
459
460 public void error( String message, Throwable error )
461 {
462 logger.error( message, error );
463 }
464
465 public void error( String message )
466 {
467 logger.error( message );
468 }
469
470 public void fatalError( String message, Throwable error )
471 {
472 logger.fatalError( message, error );
473 }
474
475 public void fatalError( String message )
476 {
477 logger.fatalError( message );
478 }
479
480 public Logger getChildLogger( String message )
481 {
482 return logger.getChildLogger( message );
483 }
484
485 public String getName()
486 {
487 return logger.getName();
488 }
489
490 public int getThreshold()
491 {
492 return logger.getThreshold();
493 }
494
495 public void info( String message, Throwable error )
496 {
497 logger.info( message, error );
498 }
499
500 public void info( String message )
501 {
502 logger.info( message );
503 }
504
505 public boolean isDebugEnabled()
506 {
507 return logger.isDebugEnabled();
508 }
509
510 public boolean isErrorEnabled()
511 {
512 return logger.isErrorEnabled();
513 }
514
515 public boolean isFatalErrorEnabled()
516 {
517 return logger.isFatalErrorEnabled();
518 }
519
520 public boolean isInfoEnabled()
521 {
522 return logger.isInfoEnabled();
523 }
524
525 public boolean isWarnEnabled()
526 {
527 return logger.isWarnEnabled();
528 }
529
530 public void setThreshold( int level )
531 {
532
533
534
535 }
536
537 public void warn( String message, Throwable error )
538 {
539 logger.warn( message, error );
540 }
541
542 public void warn( String message )
543 {
544 logger.warn( message );
545 }
546
547 }
548
549 }