1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cli;
20
21 import java.io.BufferedInputStream;
22 import java.io.Console;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.PrintStream;
30 import java.nio.charset.Charset;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.ListIterator;
40 import java.util.Map;
41 import java.util.Map.Entry;
42 import java.util.Properties;
43 import java.util.Set;
44 import java.util.StringTokenizer;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47 import java.util.stream.Stream;
48
49 import com.google.inject.AbstractModule;
50 import org.apache.commons.cli.CommandLine;
51 import org.apache.commons.cli.Option;
52 import org.apache.commons.cli.ParseException;
53 import org.apache.commons.cli.UnrecognizedOptionException;
54 import org.apache.commons.lang3.math.NumberUtils;
55 import org.apache.maven.BuildAbort;
56 import org.apache.maven.InternalErrorException;
57 import org.apache.maven.Maven;
58 import org.apache.maven.building.FileSource;
59 import org.apache.maven.building.Problem;
60 import org.apache.maven.building.Source;
61 import org.apache.maven.cli.configuration.ConfigurationProcessor;
62 import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor;
63 import org.apache.maven.cli.event.DefaultEventSpyContext;
64 import org.apache.maven.cli.event.ExecutionEventLogger;
65 import org.apache.maven.cli.internal.BootstrapCoreExtensionManager;
66 import org.apache.maven.cli.internal.extension.model.CoreExtension;
67 import org.apache.maven.cli.internal.extension.model.io.xpp3.CoreExtensionsXpp3Reader;
68 import org.apache.maven.cli.logging.Slf4jConfiguration;
69 import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
70 import org.apache.maven.cli.logging.Slf4jLoggerManager;
71 import org.apache.maven.cli.logging.Slf4jStdoutLogger;
72 import org.apache.maven.cli.transfer.ConsoleMavenTransferListener;
73 import org.apache.maven.cli.transfer.QuietMavenTransferListener;
74 import org.apache.maven.cli.transfer.Slf4jMavenTransferListener;
75 import org.apache.maven.eventspy.internal.EventSpyDispatcher;
76 import org.apache.maven.exception.DefaultExceptionHandler;
77 import org.apache.maven.exception.ExceptionHandler;
78 import org.apache.maven.exception.ExceptionSummary;
79 import org.apache.maven.execution.DefaultMavenExecutionRequest;
80 import org.apache.maven.execution.ExecutionListener;
81 import org.apache.maven.execution.MavenExecutionRequest;
82 import org.apache.maven.execution.MavenExecutionRequestPopulationException;
83 import org.apache.maven.execution.MavenExecutionRequestPopulator;
84 import org.apache.maven.execution.MavenExecutionResult;
85 import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
86 import org.apache.maven.extension.internal.CoreExports;
87 import org.apache.maven.extension.internal.CoreExtensionEntry;
88 import org.apache.maven.lifecycle.LifecycleExecutionException;
89 import org.apache.maven.model.building.ModelProcessor;
90 import org.apache.maven.project.MavenProject;
91 import org.apache.maven.properties.internal.EnvironmentUtils;
92 import org.apache.maven.properties.internal.SystemProperties;
93 import org.apache.maven.session.scope.internal.SessionScopeModule;
94 import org.apache.maven.shared.utils.logging.MessageBuilder;
95 import org.apache.maven.shared.utils.logging.MessageUtils;
96 import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
97 import org.apache.maven.toolchain.building.ToolchainsBuilder;
98 import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
99 import org.codehaus.plexus.ContainerConfiguration;
100 import org.codehaus.plexus.DefaultContainerConfiguration;
101 import org.codehaus.plexus.DefaultPlexusContainer;
102 import org.codehaus.plexus.PlexusConstants;
103 import org.codehaus.plexus.PlexusContainer;
104 import org.codehaus.plexus.classworlds.ClassWorld;
105 import org.codehaus.plexus.classworlds.realm.ClassRealm;
106 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
107 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
108 import org.codehaus.plexus.interpolation.AbstractValueSource;
109 import org.codehaus.plexus.interpolation.InterpolationException;
110 import org.codehaus.plexus.interpolation.StringSearchInterpolator;
111 import org.codehaus.plexus.logging.LoggerManager;
112 import org.codehaus.plexus.util.StringUtils;
113 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
114 import org.eclipse.aether.transfer.TransferListener;
115 import org.slf4j.ILoggerFactory;
116 import org.slf4j.Logger;
117 import org.slf4j.LoggerFactory;
118 import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
119 import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
120 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
121 import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
122 import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
123
124 import static org.apache.maven.cli.CLIManager.COLOR;
125 import static org.apache.maven.cli.ResolveFile.resolveFile;
126 import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
127
128
129
130
131
132
133 public class MavenCli {
134 public static final String LOCAL_REPO_PROPERTY = "maven.repo.local";
135
136 public static final String MULTIMODULE_PROJECT_DIRECTORY = "maven.multiModuleProjectDirectory";
137
138 public static final String USER_HOME = System.getProperty("user.home");
139
140 public static final File USER_MAVEN_CONFIGURATION_HOME = new File(USER_HOME, ".m2");
141
142 public static final File DEFAULT_USER_TOOLCHAINS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "toolchains.xml");
143
144 public static final File DEFAULT_GLOBAL_TOOLCHAINS_FILE =
145 new File(System.getProperty("maven.conf"), "toolchains.xml");
146
147 private static final String EXT_CLASS_PATH = "maven.ext.class.path";
148
149 private static final String DOT_MVN = ".mvn";
150
151 private static final String UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE = "Unable to find the root directory. Create a "
152 + DOT_MVN + " directory in the project root directory to identify it.";
153
154 private static final String EXTENSIONS_FILENAME = DOT_MVN + "/extensions.xml";
155
156 private static final String MVN_MAVEN_CONFIG = DOT_MVN + "/maven.config";
157
158 public static final String STYLE_COLOR_PROPERTY = "style.color";
159
160 private ClassWorld classWorld;
161
162 private LoggerManager plexusLoggerManager;
163
164 private ILoggerFactory slf4jLoggerFactory;
165
166 private Logger slf4jLogger;
167
168 private EventSpyDispatcher eventSpyDispatcher;
169
170 private ModelProcessor modelProcessor;
171
172 private Maven maven;
173
174 private MavenExecutionRequestPopulator executionRequestPopulator;
175
176 private ToolchainsBuilder toolchainsBuilder;
177
178 private DefaultSecDispatcher dispatcher;
179
180 private Map<String, ConfigurationProcessor> configurationProcessors;
181
182 private CLIManager cliManager;
183
184 private static final Pattern NEXT_LINE = Pattern.compile("\r?\n");
185
186 public MavenCli() {
187 this(null);
188 }
189
190
191 public MavenCli(ClassWorld classWorld) {
192 this.classWorld = classWorld;
193 }
194
195 public static void main(String[] args) {
196 int result = main(args, null);
197
198 System.exit(result);
199 }
200
201 public static int main(String[] args, ClassWorld classWorld) {
202 MavenCli cli = new MavenCli();
203
204 MessageUtils.systemInstall();
205 MessageUtils.registerShutdownHook();
206 int result = cli.doMain(new CliRequest(args, classWorld));
207 MessageUtils.systemUninstall();
208
209 return result;
210 }
211
212
213 public static int doMain(String[] args, ClassWorld classWorld) {
214 MavenCli cli = new MavenCli();
215 return cli.doMain(new CliRequest(args, classWorld));
216 }
217
218
219
220
221
222
223 public int doMain(String[] args, String workingDirectory, PrintStream stdout, PrintStream stderr) {
224 PrintStream oldout = System.out;
225 PrintStream olderr = System.err;
226
227 final Set<String> realms;
228 if (classWorld != null) {
229 realms = new HashSet<>();
230 for (ClassRealm realm : classWorld.getRealms()) {
231 realms.add(realm.getId());
232 }
233 } else {
234 realms = Collections.emptySet();
235 }
236
237 try {
238 if (stdout != null) {
239 System.setOut(stdout);
240 }
241 if (stderr != null) {
242 System.setErr(stderr);
243 }
244
245 CliRequest cliRequest = new CliRequest(args, classWorld);
246 cliRequest.workingDirectory = workingDirectory;
247
248 return doMain(cliRequest);
249 } finally {
250 if (classWorld != null) {
251 for (ClassRealm realm : new ArrayList<>(classWorld.getRealms())) {
252 String realmId = realm.getId();
253 if (!realms.contains(realmId)) {
254 try {
255 classWorld.disposeRealm(realmId);
256 } catch (NoSuchRealmException ignored) {
257
258 }
259 }
260 }
261 }
262 System.setOut(oldout);
263 System.setErr(olderr);
264 }
265 }
266
267
268 public int doMain(CliRequest cliRequest) {
269 PlexusContainer localContainer = null;
270 try {
271 initialize(cliRequest);
272 cli(cliRequest);
273 properties(cliRequest);
274 logging(cliRequest);
275 informativeCommands(cliRequest);
276 version(cliRequest);
277 localContainer = container(cliRequest);
278 commands(cliRequest);
279 configure(cliRequest);
280 toolchains(cliRequest);
281 populateRequest(cliRequest);
282 encryption(cliRequest);
283 return execute(cliRequest);
284 } catch (ExitException e) {
285 return e.exitCode;
286 } catch (UnrecognizedOptionException e) {
287
288 return 1;
289 } catch (BuildAbort e) {
290 CLIReportingUtils.showError(slf4jLogger, "ABORTED", e, cliRequest.showErrors);
291
292 return 2;
293 } catch (Exception e) {
294 CLIReportingUtils.showError(slf4jLogger, "Error executing Maven.", e, cliRequest.showErrors);
295
296 return 1;
297 } finally {
298 if (localContainer != null) {
299 localContainer.dispose();
300 }
301 }
302 }
303
304 void initialize(CliRequest cliRequest) throws ExitException {
305 if (cliRequest.workingDirectory == null) {
306 cliRequest.workingDirectory = System.getProperty("user.dir");
307 }
308
309 if (cliRequest.multiModuleProjectDirectory == null) {
310 String basedirProperty = System.getProperty(MULTIMODULE_PROJECT_DIRECTORY);
311 if (basedirProperty == null) {
312 System.err.format("-D%s system property is not set.", MULTIMODULE_PROJECT_DIRECTORY);
313 throw new ExitException(1);
314 }
315 File basedir = basedirProperty != null ? new File(basedirProperty) : new File("");
316 try {
317 cliRequest.multiModuleProjectDirectory = basedir.getCanonicalFile();
318 } catch (IOException e) {
319 cliRequest.multiModuleProjectDirectory = basedir.getAbsoluteFile();
320 }
321 }
322
323
324
325
326 Path topDirectory = Paths.get(cliRequest.workingDirectory);
327 boolean isAltFile = false;
328 for (String arg : cliRequest.args) {
329 if (isAltFile) {
330
331 Path path = topDirectory.resolve(arg);
332 if (Files.isDirectory(path)) {
333 topDirectory = path;
334 } else if (Files.isRegularFile(path)) {
335 topDirectory = path.getParent();
336 if (!Files.isDirectory(topDirectory)) {
337 System.err.println("Directory " + topDirectory
338 + " extracted from the -f/--file command-line argument " + arg + " does not exist");
339 throw new ExitException(1);
340 }
341 } else {
342 System.err.println(
343 "POM file " + arg + " specified with the -f/--file command line argument does not exist");
344 throw new ExitException(1);
345 }
346 break;
347 } else {
348
349 isAltFile = arg.equals(String.valueOf(CLIManager.ALTERNATE_POM_FILE)) || arg.equals("file");
350 }
351 }
352 try {
353 topDirectory = topDirectory.toAbsolutePath().toRealPath();
354 } catch (IOException e) {
355 System.err.println("Error computing real path from " + topDirectory + ": " + e.getMessage());
356 throw new ExitException(1);
357 }
358 cliRequest.topDirectory = topDirectory;
359
360
361
362 cliRequest.rootDirectory = searchAcceptableRootDirectory(topDirectory);
363
364
365
366
367
368 String mavenHome = System.getProperty("maven.home");
369
370 if (mavenHome != null) {
371 System.setProperty("maven.home", new File(mavenHome).getAbsolutePath());
372 }
373 }
374
375 void cli(CliRequest cliRequest) throws Exception {
376
377
378
379
380 slf4jLogger = new Slf4jStdoutLogger();
381
382 cliManager = new CLIManager();
383
384 CommandLine mavenConfig = null;
385 try {
386 File configFile = new File(cliRequest.multiModuleProjectDirectory, MVN_MAVEN_CONFIG);
387
388 if (configFile.isFile()) {
389 try (Stream<String> lines = Files.lines(configFile.toPath(), Charset.defaultCharset())) {
390 String[] args = lines.filter(arg -> !arg.isEmpty()).toArray(String[]::new);
391 mavenConfig = cliManager.parse(args);
392 List<?> unrecognized = mavenConfig.getArgList();
393 if (!unrecognized.isEmpty()) {
394
395 throw new ParseException("Unrecognized maven.config file entries: " + unrecognized);
396 }
397 }
398 }
399 } catch (ParseException e) {
400 System.err.println("Unable to parse maven.config file options: " + e.getMessage());
401 cliManager.displayHelp(System.out);
402 throw e;
403 }
404
405 try {
406 CommandLine mavenCli = cliManager.parse(cliRequest.args);
407 if (mavenConfig == null) {
408 cliRequest.commandLine = mavenCli;
409 } else {
410 cliRequest.commandLine = cliMerge(mavenConfig, mavenCli);
411 }
412 } catch (ParseException e) {
413 System.err.println("Unable to parse command line options: " + e.getMessage());
414 cliManager.displayHelp(System.out);
415 throw e;
416 }
417
418
419 try {
420 if (cliRequest.commandLine.hasOption("llr")) {
421 throw new UnrecognizedOptionException("Option '-llr' is not supported starting with Maven 3.9.1");
422 }
423 } catch (ParseException e) {
424 System.err.println("Unsupported options: " + e.getMessage());
425 cliManager.displayHelp(System.out);
426 throw e;
427 }
428 }
429
430 private void informativeCommands(CliRequest cliRequest) throws ExitException {
431 if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
432 cliManager.displayHelp(System.out);
433 throw new ExitException(0);
434 }
435
436 if (cliRequest.commandLine.hasOption(CLIManager.VERSION)) {
437 if (cliRequest.commandLine.hasOption(CLIManager.QUIET)) {
438 System.out.println(CLIReportingUtils.showVersionMinimal());
439 } else {
440 System.out.println(CLIReportingUtils.showVersion());
441 }
442 throw new ExitException(0);
443 }
444 }
445
446 private CommandLine cliMerge(CommandLine mavenConfig, CommandLine mavenCli) {
447 CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
448
449
450 for (String arg : mavenCli.getArgs()) {
451 commandLineBuilder.addArg(arg);
452 }
453
454
455
456
457
458
459
460
461
462
463
464 List<Option> setPropertyOptions = new ArrayList<>();
465 for (Option opt : mavenCli.getOptions()) {
466 if (String.valueOf(CLIManager.SET_USER_PROPERTY).equals(opt.getOpt())) {
467 setPropertyOptions.add(opt);
468 } else {
469 commandLineBuilder.addOption(opt);
470 }
471 }
472 for (Option opt : mavenConfig.getOptions()) {
473 commandLineBuilder.addOption(opt);
474 }
475
476 for (Option opt : setPropertyOptions) {
477 commandLineBuilder.addOption(opt);
478 }
479 return commandLineBuilder.build();
480 }
481
482
483
484
485 void logging(CliRequest cliRequest) {
486
487 cliRequest.debug = cliRequest.commandLine.hasOption(CLIManager.DEBUG);
488 cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption(CLIManager.QUIET);
489 cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.ERRORS);
490
491 slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
492 Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory);
493
494 if (cliRequest.debug) {
495 cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG);
496 slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG);
497 } else if (cliRequest.quiet) {
498 cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR);
499 slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR);
500 }
501
502
503
504
505 String styleColor = cliRequest.getUserProperties().getProperty(STYLE_COLOR_PROPERTY, "auto");
506 styleColor = cliRequest.commandLine.getOptionValue(COLOR, styleColor);
507 if ("always".equals(styleColor) || "yes".equals(styleColor) || "force".equals(styleColor)) {
508 MessageUtils.setColorEnabled(true);
509 } else if ("never".equals(styleColor) || "no".equals(styleColor) || "none".equals(styleColor)) {
510 MessageUtils.setColorEnabled(false);
511 } else if (!"auto".equals(styleColor) && !"tty".equals(styleColor) && !"if-tty".equals(styleColor)) {
512 throw new IllegalArgumentException(
513 "Invalid color configuration value '" + styleColor + "'. Supported are 'auto', 'always', 'never'.");
514 } else if (cliRequest.commandLine.hasOption(CLIManager.BATCH_MODE)
515 || cliRequest.commandLine.hasOption(CLIManager.LOG_FILE)) {
516 MessageUtils.setColorEnabled(false);
517 }
518
519
520 if (cliRequest.commandLine.hasOption(CLIManager.LOG_FILE)) {
521 File logFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.LOG_FILE));
522 logFile = resolveFile(logFile, cliRequest.workingDirectory);
523
524
525 try {
526 PrintStream ps = new PrintStream(new FileOutputStream(logFile));
527 System.setOut(ps);
528 System.setErr(ps);
529 } catch (FileNotFoundException e) {
530
531
532
533 }
534 }
535
536 slf4jConfiguration.activate();
537
538 plexusLoggerManager = new Slf4jLoggerManager();
539 slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName());
540 }
541
542 private void version(CliRequest cliRequest) {
543 if (cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.SHOW_VERSION)) {
544 System.out.println(CLIReportingUtils.showVersion());
545 }
546 }
547
548 private void commands(CliRequest cliRequest) {
549 if (cliRequest.showErrors) {
550 slf4jLogger.info("Error stacktraces are turned on.");
551 }
552
553 if (MavenExecutionRequest.CHECKSUM_POLICY_WARN.equals(cliRequest.request.getGlobalChecksumPolicy())) {
554 slf4jLogger.info("Disabling strict checksum verification on all artifact downloads.");
555 } else if (MavenExecutionRequest.CHECKSUM_POLICY_FAIL.equals(cliRequest.request.getGlobalChecksumPolicy())) {
556 slf4jLogger.info("Enabling strict checksum verification on all artifact downloads.");
557 }
558
559 if (slf4jLogger.isDebugEnabled()) {
560 slf4jLogger.debug("Message scheme: {}", (MessageUtils.isColorEnabled() ? "color" : "plain"));
561 if (MessageUtils.isColorEnabled()) {
562 MessageBuilder buff = MessageUtils.buffer();
563 buff.a("Message styles: ");
564 buff.a(MessageUtils.level().debug("debug")).a(' ');
565 buff.a(MessageUtils.level().info("info")).a(' ');
566 buff.a(MessageUtils.level().warning("warning")).a(' ');
567 buff.a(MessageUtils.level().error("error")).a(' ');
568
569 buff.success("success").a(' ');
570 buff.failure("failure").a(' ');
571 buff.strong("strong").a(' ');
572 buff.mojo("mojo").a(' ');
573 buff.project("project");
574 slf4jLogger.debug(buff.toString());
575 }
576 }
577 }
578
579
580
581 void properties(CliRequest cliRequest) throws ExitException {
582 try {
583 populateProperties(cliRequest, cliRequest.systemProperties, cliRequest.userProperties);
584
585 StringSearchInterpolator interpolator =
586 createInterpolator(cliRequest, cliRequest.systemProperties, cliRequest.userProperties);
587 CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
588 for (Option option : cliRequest.commandLine.getOptions()) {
589 if (!String.valueOf(CLIManager.SET_USER_PROPERTY).equals(option.getOpt())) {
590 List<String> values = option.getValuesList();
591 for (ListIterator<String> it = values.listIterator(); it.hasNext(); ) {
592 it.set(interpolator.interpolate(it.next()));
593 }
594 }
595 commandLineBuilder.addOption(option);
596 }
597 for (String arg : cliRequest.commandLine.getArgList()) {
598 commandLineBuilder.addArg(interpolator.interpolate(arg));
599 }
600 cliRequest.commandLine = commandLineBuilder.build();
601 } catch (InterpolationException e) {
602 String message = "ERROR: Could not interpolate properties and/or arguments: " + e.getMessage();
603 System.err.println(message);
604 throw new ExitException(1);
605 } catch (IllegalUseOfUndefinedProperty e) {
606 String message = "ERROR: Illegal use of undefined property: " + e.property;
607 System.err.println(message);
608 if (cliRequest.rootDirectory == null) {
609 System.err.println();
610 System.err.println(UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE);
611 }
612 throw new ExitException(1);
613 }
614 }
615
616 PlexusContainer container(CliRequest cliRequest) throws Exception {
617 if (cliRequest.classWorld == null) {
618 cliRequest.classWorld =
619 new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader());
620 }
621
622 ClassRealm coreRealm = cliRequest.classWorld.getClassRealm("plexus.core");
623 if (coreRealm == null) {
624 coreRealm = cliRequest.classWorld.getRealms().iterator().next();
625 }
626
627 List<File> extClassPath = parseExtClasspath(cliRequest);
628
629 CoreExtensionEntry coreEntry = CoreExtensionEntry.discoverFrom(coreRealm);
630 List<CoreExtensionEntry> extensions =
631 loadCoreExtensions(cliRequest, coreRealm, coreEntry.getExportedArtifacts());
632
633 ClassRealm containerRealm = setupContainerRealm(cliRequest.classWorld, coreRealm, extClassPath, extensions);
634
635 ContainerConfiguration cc = new DefaultContainerConfiguration()
636 .setClassWorld(cliRequest.classWorld)
637 .setRealm(containerRealm)
638 .setClassPathScanning(PlexusConstants.SCANNING_INDEX)
639 .setAutoWiring(true)
640 .setJSR250Lifecycle(true)
641 .setName("maven");
642
643 Set<String> exportedArtifacts = new HashSet<>(coreEntry.getExportedArtifacts());
644 Set<String> exportedPackages = new HashSet<>(coreEntry.getExportedPackages());
645 for (CoreExtensionEntry extension : extensions) {
646 exportedArtifacts.addAll(extension.getExportedArtifacts());
647 exportedPackages.addAll(extension.getExportedPackages());
648 }
649
650 final CoreExports exports = new CoreExports(containerRealm, exportedArtifacts, exportedPackages);
651
652 DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {
653 @Override
654 protected void configure() {
655 bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
656 bind(CoreExports.class).toInstance(exports);
657 }
658 });
659
660
661 container.setLookupRealm(null);
662 Thread.currentThread().setContextClassLoader(container.getContainerRealm());
663
664 container.setLoggerManager(plexusLoggerManager);
665
666 for (CoreExtensionEntry extension : extensions) {
667 container.discoverComponents(
668 extension.getClassRealm(),
669 new SessionScopeModule(container),
670 new MojoExecutionScopeModule(container));
671 }
672
673 customizeContainer(container);
674
675 container.getLoggerManager().setThresholds(cliRequest.request.getLoggingLevel());
676
677 eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
678
679 DefaultEventSpyContext eventSpyContext = new DefaultEventSpyContext();
680 Map<String, Object> data = eventSpyContext.getData();
681 data.put("plexus", container);
682 data.put("workingDirectory", cliRequest.workingDirectory);
683 data.put("systemProperties", cliRequest.systemProperties);
684 data.put("userProperties", cliRequest.userProperties);
685 data.put("versionProperties", CLIReportingUtils.getBuildProperties());
686 eventSpyDispatcher.init(eventSpyContext);
687
688
689 slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName());
690
691 maven = container.lookup(Maven.class);
692
693 executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
694
695 modelProcessor = createModelProcessor(container);
696
697 configurationProcessors = container.lookupMap(ConfigurationProcessor.class);
698
699 toolchainsBuilder = container.lookup(ToolchainsBuilder.class);
700
701 dispatcher = (DefaultSecDispatcher) container.lookup(SecDispatcher.class, "maven");
702
703 return container;
704 }
705
706 private List<CoreExtensionEntry> loadCoreExtensions(
707 CliRequest cliRequest, ClassRealm containerRealm, Set<String> providedArtifacts) throws Exception {
708 if (cliRequest.multiModuleProjectDirectory == null) {
709 return Collections.emptyList();
710 }
711
712 File extensionsFile = new File(cliRequest.multiModuleProjectDirectory, EXTENSIONS_FILENAME);
713 if (!extensionsFile.isFile()) {
714 return Collections.emptyList();
715 }
716
717 List<CoreExtension> extensions = readCoreExtensionsDescriptor(extensionsFile);
718 if (extensions.isEmpty()) {
719 return Collections.emptyList();
720 }
721
722 ContainerConfiguration cc = new DefaultContainerConfiguration()
723 .setClassWorld(cliRequest.classWorld)
724 .setRealm(containerRealm)
725 .setClassPathScanning(PlexusConstants.SCANNING_INDEX)
726 .setAutoWiring(true)
727 .setJSR250Lifecycle(true)
728 .setName("maven");
729
730 DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {
731 @Override
732 protected void configure() {
733 bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
734 }
735 });
736
737 try {
738 container.setLookupRealm(null);
739
740 container.setLoggerManager(plexusLoggerManager);
741
742 container.getLoggerManager().setThresholds(cliRequest.request.getLoggingLevel());
743
744 Thread.currentThread().setContextClassLoader(container.getContainerRealm());
745
746 executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
747
748 configurationProcessors = container.lookupMap(ConfigurationProcessor.class);
749
750 configure(cliRequest);
751
752 MavenExecutionRequest request = DefaultMavenExecutionRequest.copy(cliRequest.request);
753
754 request = populateRequest(cliRequest, request);
755
756 request = executionRequestPopulator.populateDefaults(request);
757
758 BootstrapCoreExtensionManager resolver = container.lookup(BootstrapCoreExtensionManager.class);
759
760 return Collections.unmodifiableList(resolver.loadCoreExtensions(request, providedArtifacts, extensions));
761
762 } finally {
763 executionRequestPopulator = null;
764 container.dispose();
765 }
766 }
767
768 private List<CoreExtension> readCoreExtensionsDescriptor(File extensionsFile)
769 throws IOException, XmlPullParserException {
770 CoreExtensionsXpp3Reader parser = new CoreExtensionsXpp3Reader();
771
772 try (InputStream is = new BufferedInputStream(new FileInputStream(extensionsFile))) {
773
774 return parser.read(is).getExtensions();
775 }
776 }
777
778 private ClassRealm setupContainerRealm(
779 ClassWorld classWorld, ClassRealm coreRealm, List<File> extClassPath, List<CoreExtensionEntry> extensions)
780 throws Exception {
781 if (!extClassPath.isEmpty() || !extensions.isEmpty()) {
782 ClassRealm extRealm = classWorld.newRealm("maven.ext", null);
783
784 extRealm.setParentRealm(coreRealm);
785
786 slf4jLogger.debug("Populating class realm {}", extRealm.getId());
787
788 for (File file : extClassPath) {
789 slf4jLogger.debug(" Included {}", file);
790
791 extRealm.addURL(file.toURI().toURL());
792 }
793
794 for (CoreExtensionEntry entry : reverse(extensions)) {
795 Set<String> exportedPackages = entry.getExportedPackages();
796 ClassRealm realm = entry.getClassRealm();
797 for (String exportedPackage : exportedPackages) {
798 extRealm.importFrom(realm, exportedPackage);
799 }
800 if (exportedPackages.isEmpty()) {
801
802 extRealm.importFrom(realm, realm.getId());
803 }
804 }
805
806 return extRealm;
807 }
808
809 return coreRealm;
810 }
811
812 private static <T> List<T> reverse(List<T> list) {
813 List<T> copy = new ArrayList<>(list);
814 Collections.reverse(copy);
815 return copy;
816 }
817
818 private List<File> parseExtClasspath(CliRequest cliRequest) {
819 String extClassPath = cliRequest.userProperties.getProperty(EXT_CLASS_PATH);
820 if (extClassPath == null) {
821 extClassPath = cliRequest.systemProperties.getProperty(EXT_CLASS_PATH);
822 }
823
824 List<File> jars = new ArrayList<>();
825
826 if (StringUtils.isNotEmpty(extClassPath)) {
827 for (String jar : StringUtils.split(extClassPath, File.pathSeparator)) {
828 File file = resolveFile(new File(jar), cliRequest.workingDirectory);
829
830 slf4jLogger.debug(" Included {}", file);
831
832 jars.add(file);
833 }
834 }
835
836 return jars;
837 }
838
839
840
841
842 private void encryption(CliRequest cliRequest) throws Exception {
843 if (cliRequest.commandLine.hasOption(CLIManager.ENCRYPT_MASTER_PASSWORD)) {
844 String passwd = cliRequest.commandLine.getOptionValue(CLIManager.ENCRYPT_MASTER_PASSWORD);
845
846 if (passwd == null) {
847 Console cons = System.console();
848 char[] password = (cons == null) ? null : cons.readPassword("Master password: ");
849 if (password != null) {
850
851 passwd = String.copyValueOf(password);
852
853
854 java.util.Arrays.fill(password, ' ');
855 }
856 }
857
858 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
859
860 System.out.println(cipher.encryptAndDecorate(passwd, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION));
861
862 throw new ExitException(0);
863 } else if (cliRequest.commandLine.hasOption(CLIManager.ENCRYPT_PASSWORD)) {
864 String passwd = cliRequest.commandLine.getOptionValue(CLIManager.ENCRYPT_PASSWORD);
865
866 if (passwd == null) {
867 Console cons = System.console();
868 char[] password = (cons == null) ? null : cons.readPassword("Password: ");
869 if (password != null) {
870
871 passwd = String.copyValueOf(password);
872
873
874 java.util.Arrays.fill(password, ' ');
875 }
876 }
877
878 String configurationFile = dispatcher.getConfigurationFile();
879
880 if (configurationFile.startsWith("~")) {
881 configurationFile = System.getProperty("user.home") + configurationFile.substring(1);
882 }
883
884 String file = System.getProperty(DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile);
885
886 String master = null;
887
888 SettingsSecurity sec = SecUtil.read(file, true);
889 if (sec != null) {
890 master = sec.getMaster();
891 }
892
893 if (master == null) {
894 throw new IllegalStateException("Master password is not set in the setting security file: " + file);
895 }
896
897 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
898 String masterPasswd = cipher.decryptDecorated(master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION);
899 System.out.println(cipher.encryptAndDecorate(passwd, masterPasswd));
900
901 throw new ExitException(0);
902 }
903 }
904
905 private int execute(CliRequest cliRequest) throws MavenExecutionRequestPopulationException {
906 MavenExecutionRequest request = executionRequestPopulator.populateDefaults(cliRequest.request);
907
908 eventSpyDispatcher.onEvent(request);
909
910 MavenExecutionResult result = maven.execute(request);
911
912 eventSpyDispatcher.onEvent(result);
913
914 eventSpyDispatcher.close();
915
916 if (result.hasExceptions()) {
917 ExceptionHandler handler = new DefaultExceptionHandler();
918
919 Map<String, String> references = new LinkedHashMap<>();
920
921 MavenProject project = null;
922
923 for (Throwable exception : result.getExceptions()) {
924 ExceptionSummary summary = handler.handleException(exception);
925
926 logSummary(summary, references, "", cliRequest.showErrors);
927
928 if (project == null && exception instanceof LifecycleExecutionException) {
929 project = ((LifecycleExecutionException) exception).getProject();
930 }
931 }
932
933 slf4jLogger.error("");
934
935 if (!cliRequest.showErrors) {
936 slf4jLogger.error(
937 "To see the full stack trace of the errors, re-run Maven with the {} switch.",
938 buffer().strong("-e"));
939 }
940 if (!slf4jLogger.isDebugEnabled()) {
941 slf4jLogger.error(
942 "Re-run Maven using the {} switch to enable full debug logging.", buffer().strong("-X"));
943 }
944
945 if (!references.isEmpty()) {
946 slf4jLogger.error("");
947 slf4jLogger.error("For more information about the errors and possible solutions"
948 + ", please read the following articles:");
949
950 for (Map.Entry<String, String> entry : references.entrySet()) {
951 slf4jLogger.error("{} {}", buffer().strong(entry.getValue()), entry.getKey());
952 }
953 }
954
955 if (project != null
956 && !project.equals(result.getTopologicallySortedProjects().get(0))) {
957 slf4jLogger.error("");
958 slf4jLogger.error("After correcting the problems, you can resume the build with the command");
959 slf4jLogger.error(buffer().a(" ")
960 .strong("mvn <args> -rf " + getResumeFrom(result.getTopologicallySortedProjects(), project))
961 .toString());
962 }
963
964 if (MavenExecutionRequest.REACTOR_FAIL_NEVER.equals(cliRequest.request.getReactorFailureBehavior())) {
965 slf4jLogger.info("Build failures were ignored.");
966
967 return 0;
968 } else {
969 return 1;
970 }
971 } else {
972 return 0;
973 }
974 }
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992 private String getResumeFrom(List<MavenProject> mavenProjects, MavenProject failedProject) {
993 for (MavenProject buildProject : mavenProjects) {
994 if (failedProject.getArtifactId().equals(buildProject.getArtifactId())
995 && !failedProject.equals(buildProject)) {
996 return failedProject.getGroupId() + ":" + failedProject.getArtifactId();
997 }
998 }
999 return ":" + failedProject.getArtifactId();
1000 }
1001
1002 private void logSummary(
1003 ExceptionSummary summary, Map<String, String> references, String indent, boolean showErrors) {
1004 String referenceKey = "";
1005
1006 if (StringUtils.isNotEmpty(summary.getReference())) {
1007 referenceKey = references.get(summary.getReference());
1008 if (referenceKey == null) {
1009 referenceKey = "[Help " + (references.size() + 1) + "]";
1010 references.put(summary.getReference(), referenceKey);
1011 }
1012 }
1013
1014 String msg = summary.getMessage();
1015
1016 if (StringUtils.isNotEmpty(referenceKey)) {
1017 if (msg.indexOf('\n') < 0) {
1018 msg += " -> " + buffer().strong(referenceKey);
1019 } else {
1020 msg += "\n-> " + buffer().strong(referenceKey);
1021 }
1022 }
1023
1024 String[] lines = NEXT_LINE.split(msg);
1025 String currentColor = "";
1026
1027 for (int i = 0; i < lines.length; i++) {
1028
1029 String line = currentColor + lines[i];
1030
1031
1032 Matcher matcher = LAST_ANSI_SEQUENCE.matcher(line);
1033 String nextColor = "";
1034 if (matcher.find()) {
1035 nextColor = matcher.group(1);
1036 if (ANSI_RESET.equals(nextColor)) {
1037
1038 nextColor = "";
1039 }
1040 }
1041
1042
1043 line = indent + line + ("".equals(nextColor) ? "" : ANSI_RESET);
1044
1045 if ((i == lines.length - 1) && (showErrors || (summary.getException() instanceof InternalErrorException))) {
1046 slf4jLogger.error(line, summary.getException());
1047 } else {
1048 slf4jLogger.error(line);
1049 }
1050
1051 currentColor = nextColor;
1052 }
1053
1054 indent += " ";
1055
1056 for (ExceptionSummary child : summary.getChildren()) {
1057 logSummary(child, references, indent, showErrors);
1058 }
1059 }
1060
1061 private static final Pattern LAST_ANSI_SEQUENCE = Pattern.compile("(\u001B\\[[;\\d]*[ -/]*[@-~])[^\u001B]*$");
1062
1063 private static final String ANSI_RESET = "\u001B\u005Bm";
1064
1065 private void configure(CliRequest cliRequest) throws Exception {
1066
1067
1068
1069
1070
1071
1072 cliRequest.request.setEventSpyDispatcher(eventSpyDispatcher);
1073
1074
1075
1076
1077
1078
1079
1080
1081 int userSuppliedConfigurationProcessorCount = configurationProcessors.size() - 1;
1082
1083 if (userSuppliedConfigurationProcessorCount == 0) {
1084
1085
1086
1087
1088 configurationProcessors.get(SettingsXmlConfigurationProcessor.HINT).process(cliRequest);
1089 } else if (userSuppliedConfigurationProcessorCount == 1) {
1090
1091
1092
1093 for (Entry<String, ConfigurationProcessor> entry : configurationProcessors.entrySet()) {
1094 String hint = entry.getKey();
1095 if (!hint.equals(SettingsXmlConfigurationProcessor.HINT)) {
1096 ConfigurationProcessor configurationProcessor = entry.getValue();
1097 configurationProcessor.process(cliRequest);
1098 }
1099 }
1100 } else if (userSuppliedConfigurationProcessorCount > 1) {
1101
1102
1103
1104 StringBuilder sb = new StringBuilder(String.format(
1105 "\nThere can only be one user supplied ConfigurationProcessor, there are %s:\n\n",
1106 userSuppliedConfigurationProcessorCount));
1107 for (Entry<String, ConfigurationProcessor> entry : configurationProcessors.entrySet()) {
1108 String hint = entry.getKey();
1109 if (!hint.equals(SettingsXmlConfigurationProcessor.HINT)) {
1110 ConfigurationProcessor configurationProcessor = entry.getValue();
1111 sb.append(String.format(
1112 "%s\n", configurationProcessor.getClass().getName()));
1113 }
1114 }
1115 sb.append("\n");
1116 throw new Exception(sb.toString());
1117 }
1118 }
1119
1120 void toolchains(CliRequest cliRequest) throws Exception {
1121 File userToolchainsFile;
1122
1123 if (cliRequest.commandLine.hasOption(CLIManager.ALTERNATE_USER_TOOLCHAINS)) {
1124 userToolchainsFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.ALTERNATE_USER_TOOLCHAINS));
1125 userToolchainsFile = resolveFile(userToolchainsFile, cliRequest.workingDirectory);
1126
1127 if (!userToolchainsFile.isFile()) {
1128 throw new FileNotFoundException(
1129 "The specified user toolchains file does not exist: " + userToolchainsFile);
1130 }
1131 } else {
1132 userToolchainsFile = DEFAULT_USER_TOOLCHAINS_FILE;
1133 }
1134
1135 File globalToolchainsFile;
1136
1137 if (cliRequest.commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS)) {
1138 globalToolchainsFile =
1139 new File(cliRequest.commandLine.getOptionValue(CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS));
1140 globalToolchainsFile = resolveFile(globalToolchainsFile, cliRequest.workingDirectory);
1141
1142 if (!globalToolchainsFile.isFile()) {
1143 throw new FileNotFoundException(
1144 "The specified global toolchains file does not exist: " + globalToolchainsFile);
1145 }
1146 } else {
1147 globalToolchainsFile = DEFAULT_GLOBAL_TOOLCHAINS_FILE;
1148 }
1149
1150 cliRequest.request.setGlobalToolchainsFile(globalToolchainsFile);
1151 cliRequest.request.setUserToolchainsFile(userToolchainsFile);
1152
1153 DefaultToolchainsBuildingRequest toolchainsRequest = new DefaultToolchainsBuildingRequest();
1154 if (globalToolchainsFile.isFile()) {
1155 toolchainsRequest.setGlobalToolchainsSource(new FileSource(globalToolchainsFile));
1156 }
1157 if (userToolchainsFile.isFile()) {
1158 toolchainsRequest.setUserToolchainsSource(new FileSource(userToolchainsFile));
1159 }
1160
1161 eventSpyDispatcher.onEvent(toolchainsRequest);
1162
1163 slf4jLogger.debug(
1164 "Reading global toolchains from {}",
1165 getLocation(toolchainsRequest.getGlobalToolchainsSource(), globalToolchainsFile));
1166 slf4jLogger.debug(
1167 "Reading user toolchains from {}",
1168 getLocation(toolchainsRequest.getUserToolchainsSource(), userToolchainsFile));
1169
1170 ToolchainsBuildingResult toolchainsResult = toolchainsBuilder.build(toolchainsRequest);
1171
1172 eventSpyDispatcher.onEvent(toolchainsResult);
1173
1174 executionRequestPopulator.populateFromToolchains(cliRequest.request, toolchainsResult.getEffectiveToolchains());
1175
1176 if (!toolchainsResult.getProblems().isEmpty() && slf4jLogger.isWarnEnabled()) {
1177 slf4jLogger.warn("");
1178 slf4jLogger.warn("Some problems were encountered while building the effective toolchains");
1179
1180 for (Problem problem : toolchainsResult.getProblems()) {
1181 slf4jLogger.warn("{} @ {}", problem.getMessage(), problem.getLocation());
1182 }
1183
1184 slf4jLogger.warn("");
1185 }
1186 }
1187
1188 private Object getLocation(Source source, File defaultLocation) {
1189 if (source != null) {
1190 return source.getLocation();
1191 }
1192 return defaultLocation;
1193 }
1194
1195 private MavenExecutionRequest populateRequest(CliRequest cliRequest) {
1196 return populateRequest(cliRequest, cliRequest.request);
1197 }
1198
1199 @SuppressWarnings("checkstyle:methodlength")
1200 private MavenExecutionRequest populateRequest(CliRequest cliRequest, MavenExecutionRequest request) {
1201 CommandLine commandLine = cliRequest.commandLine;
1202 String workingDirectory = cliRequest.workingDirectory;
1203 boolean quiet = cliRequest.quiet;
1204 boolean showErrors = cliRequest.showErrors;
1205
1206 String[] deprecatedOptions = {"up", "npu", "cpu", "npr"};
1207 for (String deprecatedOption : deprecatedOptions) {
1208 if (commandLine.hasOption(deprecatedOption)) {
1209 slf4jLogger.warn(
1210 "Command line option -{} is deprecated and will be removed in future Maven versions.",
1211 deprecatedOption);
1212 }
1213 }
1214
1215
1216
1217
1218
1219
1220 if (commandLine.hasOption(CLIManager.BATCH_MODE)) {
1221 request.setInteractiveMode(false);
1222 }
1223
1224 boolean noSnapshotUpdates = false;
1225 if (commandLine.hasOption(CLIManager.SUPRESS_SNAPSHOT_UPDATES)) {
1226 noSnapshotUpdates = true;
1227 }
1228
1229
1230
1231
1232
1233 List<String> goals = commandLine.getArgList();
1234
1235 boolean recursive = true;
1236
1237
1238 String reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
1239
1240 if (commandLine.hasOption(CLIManager.NON_RECURSIVE)) {
1241 recursive = false;
1242 }
1243
1244 if (commandLine.hasOption(CLIManager.FAIL_FAST)) {
1245 reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
1246 } else if (commandLine.hasOption(CLIManager.FAIL_AT_END)) {
1247 reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_AT_END;
1248 } else if (commandLine.hasOption(CLIManager.FAIL_NEVER)) {
1249 reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_NEVER;
1250 }
1251
1252 if (commandLine.hasOption(CLIManager.OFFLINE)) {
1253 request.setOffline(true);
1254 }
1255
1256 boolean updateSnapshots = false;
1257
1258 if (commandLine.hasOption(CLIManager.UPDATE_SNAPSHOTS)) {
1259 updateSnapshots = true;
1260 }
1261
1262 String globalChecksumPolicy = null;
1263
1264 if (commandLine.hasOption(CLIManager.CHECKSUM_FAILURE_POLICY)) {
1265 globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_FAIL;
1266 } else if (commandLine.hasOption(CLIManager.CHECKSUM_WARNING_POLICY)) {
1267 globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_WARN;
1268 }
1269
1270 File baseDirectory = new File(workingDirectory, "").getAbsoluteFile();
1271
1272
1273
1274
1275
1276 List<String> activeProfiles = new ArrayList<>();
1277
1278 List<String> inactiveProfiles = new ArrayList<>();
1279
1280 if (commandLine.hasOption(CLIManager.ACTIVATE_PROFILES)) {
1281 String[] profileOptionValues = commandLine.getOptionValues(CLIManager.ACTIVATE_PROFILES);
1282 if (profileOptionValues != null) {
1283 for (String profileOptionValue : profileOptionValues) {
1284 StringTokenizer profileTokens = new StringTokenizer(profileOptionValue, ",");
1285
1286 while (profileTokens.hasMoreTokens()) {
1287 String profileAction = profileTokens.nextToken().trim();
1288
1289 if (profileAction.startsWith("-") || profileAction.startsWith("!")) {
1290 inactiveProfiles.add(profileAction.substring(1));
1291 } else if (profileAction.startsWith("+")) {
1292 activeProfiles.add(profileAction.substring(1));
1293 } else {
1294 activeProfiles.add(profileAction);
1295 }
1296 }
1297 }
1298 }
1299 }
1300
1301 TransferListener transferListener;
1302
1303 if (quiet || cliRequest.commandLine.hasOption(CLIManager.NO_TRANSFER_PROGRESS)) {
1304 transferListener = new QuietMavenTransferListener();
1305 } else if (request.isInteractiveMode() && !cliRequest.commandLine.hasOption(CLIManager.LOG_FILE)) {
1306
1307
1308
1309
1310 transferListener = getConsoleTransferListener(cliRequest.commandLine.hasOption(CLIManager.DEBUG));
1311 } else {
1312 transferListener = getBatchTransferListener();
1313 }
1314
1315 ExecutionListener executionListener = new ExecutionEventLogger();
1316 if (eventSpyDispatcher != null) {
1317 executionListener = eventSpyDispatcher.chainListener(executionListener);
1318 }
1319
1320 String alternatePomFile = null;
1321 if (commandLine.hasOption(CLIManager.ALTERNATE_POM_FILE)) {
1322 alternatePomFile = commandLine.getOptionValue(CLIManager.ALTERNATE_POM_FILE);
1323 }
1324
1325 request.setBaseDirectory(baseDirectory)
1326 .setGoals(goals)
1327 .setSystemProperties(cliRequest.systemProperties)
1328 .setUserProperties(cliRequest.userProperties)
1329 .setReactorFailureBehavior(reactorFailureBehaviour)
1330 .setRecursive(recursive)
1331 .setShowErrors(showErrors)
1332 .addActiveProfiles(activeProfiles)
1333 .addInactiveProfiles(inactiveProfiles)
1334 .setExecutionListener(executionListener)
1335 .setTransferListener(transferListener)
1336 .setUpdateSnapshots(updateSnapshots)
1337 .setNoSnapshotUpdates(noSnapshotUpdates)
1338 .setGlobalChecksumPolicy(globalChecksumPolicy)
1339 .setMultiModuleProjectDirectory(cliRequest.multiModuleProjectDirectory);
1340
1341 if (alternatePomFile != null) {
1342 File pom = resolveFile(new File(alternatePomFile), workingDirectory);
1343 if (pom.isDirectory()) {
1344 pom = new File(pom, "pom.xml");
1345 }
1346
1347 request.setPom(pom);
1348 } else if (modelProcessor != null) {
1349 File pom = modelProcessor.locatePom(baseDirectory);
1350
1351 if (pom.isFile()) {
1352 request.setPom(pom);
1353 }
1354 }
1355
1356 if ((request.getPom() != null) && (request.getPom().getParentFile() != null)) {
1357 request.setBaseDirectory(request.getPom().getParentFile());
1358 }
1359
1360 if (commandLine.hasOption(CLIManager.RESUME_FROM)) {
1361 request.setResumeFrom(commandLine.getOptionValue(CLIManager.RESUME_FROM));
1362 }
1363
1364 if (commandLine.hasOption(CLIManager.PROJECT_LIST)) {
1365 String[] projectOptionValues = commandLine.getOptionValues(CLIManager.PROJECT_LIST);
1366
1367 List<String> inclProjects = new ArrayList<>();
1368 List<String> exclProjects = new ArrayList<>();
1369
1370 if (projectOptionValues != null) {
1371 for (String projectOptionValue : projectOptionValues) {
1372 StringTokenizer projectTokens = new StringTokenizer(projectOptionValue, ",");
1373
1374 while (projectTokens.hasMoreTokens()) {
1375 String projectAction = projectTokens.nextToken().trim();
1376
1377 if (projectAction.startsWith("-") || projectAction.startsWith("!")) {
1378 exclProjects.add(projectAction.substring(1));
1379 } else if (projectAction.startsWith("+")) {
1380 inclProjects.add(projectAction.substring(1));
1381 } else {
1382 inclProjects.add(projectAction);
1383 }
1384 }
1385 }
1386 }
1387
1388 request.setSelectedProjects(inclProjects);
1389 request.setExcludedProjects(exclProjects);
1390 }
1391
1392 if (commandLine.hasOption(CLIManager.ALSO_MAKE) && !commandLine.hasOption(CLIManager.ALSO_MAKE_DEPENDENTS)) {
1393 request.setMakeBehavior(MavenExecutionRequest.REACTOR_MAKE_UPSTREAM);
1394 } else if (!commandLine.hasOption(CLIManager.ALSO_MAKE)
1395 && commandLine.hasOption(CLIManager.ALSO_MAKE_DEPENDENTS)) {
1396 request.setMakeBehavior(MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM);
1397 } else if (commandLine.hasOption(CLIManager.ALSO_MAKE)
1398 && commandLine.hasOption(CLIManager.ALSO_MAKE_DEPENDENTS)) {
1399 request.setMakeBehavior(MavenExecutionRequest.REACTOR_MAKE_BOTH);
1400 }
1401
1402 String localRepoProperty = request.getUserProperties().getProperty(MavenCli.LOCAL_REPO_PROPERTY);
1403
1404
1405
1406 if (localRepoProperty == null) {
1407 localRepoProperty = request.getSystemProperties().getProperty(MavenCli.LOCAL_REPO_PROPERTY);
1408 }
1409
1410 if (localRepoProperty != null) {
1411 request.setLocalRepositoryPath(localRepoProperty);
1412 }
1413
1414 request.setCacheNotFound(true);
1415 request.setCacheTransferError(false);
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425 final String threadConfiguration =
1426 commandLine.hasOption(CLIManager.THREADS) ? commandLine.getOptionValue(CLIManager.THREADS) : null;
1427
1428 if (threadConfiguration != null) {
1429 int degreeOfConcurrency = calculateDegreeOfConcurrency(threadConfiguration);
1430 if (degreeOfConcurrency > 1) {
1431 request.setBuilderId("multithreaded");
1432 request.setDegreeOfConcurrency(degreeOfConcurrency);
1433 }
1434 }
1435
1436
1437
1438
1439 if (commandLine.hasOption(CLIManager.BUILDER)) {
1440 request.setBuilderId(commandLine.getOptionValue(CLIManager.BUILDER));
1441 }
1442
1443 return request;
1444 }
1445
1446 int calculateDegreeOfConcurrency(String threadConfiguration) {
1447 if (threadConfiguration.endsWith("C")) {
1448 threadConfiguration = threadConfiguration.substring(0, threadConfiguration.length() - 1);
1449
1450 if (!NumberUtils.isParsable(threadConfiguration)) {
1451 throw new IllegalArgumentException("Invalid threads core multiplier value: '" + threadConfiguration
1452 + "C'. Supported are int and float values ending with C.");
1453 }
1454
1455 float coreMultiplier = Float.parseFloat(threadConfiguration);
1456
1457 if (coreMultiplier <= 0.0f) {
1458 throw new IllegalArgumentException("Invalid threads core multiplier value: '" + threadConfiguration
1459 + "C'. Value must be positive.");
1460 }
1461
1462 int procs = Runtime.getRuntime().availableProcessors();
1463 int threads = (int) (coreMultiplier * procs);
1464 return threads == 0 ? 1 : threads;
1465 } else {
1466 if (!NumberUtils.isParsable(threadConfiguration)) {
1467 throw new IllegalArgumentException(
1468 "Invalid threads value: '" + threadConfiguration + "'. Supported are int values.");
1469 }
1470
1471 try {
1472 int threads = Integer.parseInt(threadConfiguration);
1473
1474 if (threads <= 0) {
1475 throw new IllegalArgumentException(
1476 "Invalid threads value: '" + threadConfiguration + "'. Value must be positive.");
1477 }
1478
1479 return threads;
1480 } catch (NumberFormatException e) {
1481 throw new IllegalArgumentException(
1482 "Invalid threads value: '" + threadConfiguration + "'. Supported are integer values.");
1483 }
1484 }
1485 }
1486
1487
1488
1489
1490
1491 static void populateProperties(CliRequest cliRequest, Properties systemProperties, Properties userProperties)
1492 throws InterpolationException {
1493
1494
1495
1496
1497
1498
1499
1500 Properties cliProperties = new Properties();
1501 if (cliRequest.commandLine.hasOption(CLIManager.SET_USER_PROPERTY)) {
1502 String[] defStrs = cliRequest.commandLine.getOptionValues(CLIManager.SET_USER_PROPERTY);
1503
1504 if (defStrs != null) {
1505 String name;
1506 String value;
1507 for (String property : defStrs) {
1508 int i = property.indexOf('=');
1509 if (i <= 0) {
1510 name = property.trim();
1511 value = "true";
1512 } else {
1513 name = property.substring(0, i).trim();
1514 value = property.substring(i + 1);
1515 }
1516 cliProperties.setProperty(name, value);
1517 }
1518 }
1519 }
1520
1521 EnvironmentUtils.addEnvVars(systemProperties);
1522 SystemProperties.addSystemProperties(systemProperties);
1523
1524 StringSearchInterpolator interpolator = createInterpolator(cliRequest, cliProperties, systemProperties);
1525 for (Map.Entry<Object, Object> e : cliProperties.entrySet()) {
1526 String name = (String) e.getKey();
1527 String value = interpolator.interpolate((String) e.getValue());
1528 userProperties.setProperty(name, value);
1529 }
1530
1531 systemProperties.putAll(userProperties);
1532
1533
1534
1535
1536
1537 userProperties.forEach((k, v) -> System.setProperty((String) k, (String) v));
1538
1539
1540
1541
1542
1543
1544 Properties buildProperties = CLIReportingUtils.getBuildProperties();
1545
1546 String mavenVersion = buildProperties.getProperty(CLIReportingUtils.BUILD_VERSION_PROPERTY);
1547 systemProperties.setProperty("maven.version", mavenVersion);
1548
1549 String mavenBuildVersion = CLIReportingUtils.createMavenVersionString(buildProperties);
1550 systemProperties.setProperty("maven.build.version", mavenBuildVersion);
1551 }
1552
1553 protected boolean isAcceptableRootDirectory(Path path) {
1554 return path != null && Files.isDirectory(path.resolve(DOT_MVN));
1555 }
1556
1557 protected Path searchAcceptableRootDirectory(Path path) {
1558 if (path == null) {
1559 return null;
1560 }
1561 if (isAcceptableRootDirectory(path)) {
1562 return path;
1563 }
1564 return searchAcceptableRootDirectory(path.getParent());
1565 }
1566
1567 protected static StringSearchInterpolator createInterpolator(CliRequest cliRequest, Properties... properties) {
1568 StringSearchInterpolator interpolator = new StringSearchInterpolator();
1569 interpolator.addValueSource(new AbstractValueSource(false) {
1570 @Override
1571 public Object getValue(String expression) {
1572 if ("session.topDirectory".equals(expression)) {
1573 Path topDirectory = cliRequest.topDirectory;
1574 if (topDirectory != null) {
1575 return topDirectory.toString();
1576 } else {
1577 throw new IllegalUseOfUndefinedProperty(expression);
1578 }
1579 } else if ("session.rootDirectory".equals(expression)) {
1580 Path rootDirectory = cliRequest.rootDirectory;
1581 if (rootDirectory != null) {
1582 return rootDirectory.toString();
1583 } else {
1584 throw new IllegalUseOfUndefinedProperty(expression);
1585 }
1586 }
1587 return null;
1588 }
1589 });
1590 interpolator.addValueSource(new AbstractValueSource(false) {
1591 @Override
1592 public Object getValue(String expression) {
1593 for (Properties props : properties) {
1594 Object val = props.getProperty(expression);
1595 if (val != null) {
1596 return val;
1597 }
1598 }
1599 return null;
1600 }
1601 });
1602 return interpolator;
1603 }
1604
1605 static class ExitException extends Exception {
1606 int exitCode;
1607
1608 ExitException(int exitCode) {
1609 this.exitCode = exitCode;
1610 }
1611 }
1612
1613 static class IllegalUseOfUndefinedProperty extends IllegalArgumentException {
1614 final String property;
1615
1616 IllegalUseOfUndefinedProperty(String property) {
1617 this.property = property;
1618 }
1619 }
1620
1621
1622
1623
1624
1625 protected TransferListener getConsoleTransferListener(boolean printResourceNames) {
1626 return new ConsoleMavenTransferListener(System.out, printResourceNames);
1627 }
1628
1629 protected TransferListener getBatchTransferListener() {
1630 return new Slf4jMavenTransferListener();
1631 }
1632
1633 protected void customizeContainer(PlexusContainer container) {}
1634
1635 protected ModelProcessor createModelProcessor(PlexusContainer container) throws ComponentLookupException {
1636 return container.lookup(ModelProcessor.class);
1637 }
1638 }