private void initialize(CliRequest cliRequest) { if (cliRequest.stdout == null) { cliRequest.stdout = System.out; } if (cliRequest.stderr == null) { cliRequest.stderr = System.err; } if (logger == null) { logger = new PrintStreamLogger(cliRequest.stdout); } else { logger.setStream(cliRequest.stdout); } if (cliRequest.workingDirectory == null) { cliRequest.workingDirectory = System.getProperty("user.dir"); } // // Make sure the Maven home directory is an absolute path to save us from confusion with say // drive-relative // Windows paths. // String mavenHome = System.getProperty("maven.home"); if (mavenHome != null) { System.setProperty("maven.home", new File(mavenHome).getAbsolutePath()); } }
private void settings(CliRequest cliRequest) throws Exception { File userSettingsFile; if (cliRequest.commandLine.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) { userSettingsFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS)); userSettingsFile = resolveFile(userSettingsFile, cliRequest.workingDirectory); if (!userSettingsFile.isFile()) { throw new FileNotFoundException( "The specified user settings file does not exist: " + userSettingsFile); } } else { userSettingsFile = DEFAULT_USER_SETTINGS_FILE; } logger.debug("Reading user settings from " + userSettingsFile); File globalSettingsFile; if (cliRequest.commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) { globalSettingsFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS)); globalSettingsFile = resolveFile(globalSettingsFile, cliRequest.workingDirectory); if (!globalSettingsFile.isFile()) { throw new FileNotFoundException( "The specified global settings file does not exist: " + globalSettingsFile); } } else { globalSettingsFile = DEFAULT_GLOBAL_SETTINGS_FILE; } logger.debug("Reading global settings from " + globalSettingsFile); cliRequest.request.setGlobalSettingsFile(globalSettingsFile); cliRequest.request.setUserSettingsFile(userSettingsFile); SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest(); settingsRequest.setGlobalSettingsFile(globalSettingsFile); settingsRequest.setUserSettingsFile(userSettingsFile); settingsRequest.setSystemProperties(cliRequest.systemProperties); settingsRequest.setUserProperties(cliRequest.userProperties); SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest); executionRequestPopulator.populateFromSettings( cliRequest.request, settingsResult.getEffectiveSettings()); if (!settingsResult.getProblems().isEmpty() && logger.isWarnEnabled()) { logger.warn(""); logger.warn("Some problems were encountered while building the effective settings"); for (SettingsProblem problem : settingsResult.getProblems()) { logger.warn(problem.getMessage() + " @ " + problem.getLocation()); } logger.warn(""); } }
private void commands(CliRequest cliRequest) { if (cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.SHOW_VERSION)) { CLIReportingUtils.showVersion(cliRequest.stdout); } if (cliRequest.showErrors) { logger.info("Error stacktraces are turned on."); } // // TODO: move checksum policies to // if (MavenExecutionRequest.CHECKSUM_POLICY_WARN.equals( cliRequest.request.getGlobalChecksumPolicy())) { logger.info("Disabling strict checksum verification on all artifact downloads."); } else if (MavenExecutionRequest.CHECKSUM_POLICY_FAIL.equals( cliRequest.request.getGlobalChecksumPolicy())) { logger.info("Enabling strict checksum verification on all artifact downloads."); } }
private void logSummary( ExceptionSummary summary, Map<String, String> references, String indent, boolean showErrors) { String referenceKey = ""; if (StringUtils.isNotEmpty(summary.getReference())) { referenceKey = references.get(summary.getReference()); if (referenceKey == null) { referenceKey = "[Help " + (references.size() + 1) + "]"; references.put(summary.getReference(), referenceKey); } } String msg = summary.getMessage(); if (StringUtils.isNotEmpty(referenceKey)) { if (msg.indexOf('\n') < 0) { msg += " -> " + referenceKey; } else { msg += "\n-> " + referenceKey; } } String[] lines = msg.split("(\r\n)|(\r)|(\n)"); for (int i = 0; i < lines.length; i++) { String line = indent + lines[i].trim(); if (i == lines.length - 1 && (showErrors || (summary.getException() instanceof InternalErrorException))) { logger.error(line, summary.getException()); } else { logger.error(line); } } indent += " "; for (ExceptionSummary child : summary.getChildren()) { logSummary(child, references, indent, showErrors); } }
// // Logging needs to be handled in a standard way at the container level. // private void logging(CliRequest cliRequest) { cliRequest.debug = cliRequest.commandLine.hasOption(CLIManager.DEBUG); cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption(CLIManager.QUIET); cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.ERRORS); if (cliRequest.debug) { cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG); } else if (cliRequest.quiet) { // TODO: we need to do some more work here. Some plugins use sys out or log errors at info // level. // Ideally, we could use Warn across the board cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR); // TODO:Additionally, we can't change the mojo level because the component key includes the // version and // it isn't known ahead of time. This seems worth changing. } else { cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_INFO); } logger.setThreshold(cliRequest.request.getLoggingLevel()); if (cliRequest.commandLine.hasOption(CLIManager.LOG_FILE)) { File logFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.LOG_FILE)); logFile = resolveFile(logFile, cliRequest.workingDirectory); try { cliRequest.fileStream = new PrintStream(logFile); logger.setStream(cliRequest.fileStream); } catch (FileNotFoundException e) { cliRequest.stderr.println(e); logger.setStream(cliRequest.stdout); } } else { logger.setStream(cliRequest.stdout); } cliRequest.request.setExecutionListener(new ExecutionEventLogger(logger)); }
private int execute(CliRequest cliRequest) { MavenExecutionResult result = maven.execute(cliRequest.request); if (result.hasExceptions()) { ExceptionHandler handler = new DefaultExceptionHandler(); Map<String, String> references = new LinkedHashMap<String, String>(); MavenProject project = null; for (Throwable exception : result.getExceptions()) { ExceptionSummary summary = handler.handleException(exception); logSummary(summary, references, "", cliRequest.showErrors); if (project == null && exception instanceof LifecycleExecutionException) { project = ((LifecycleExecutionException) exception).getProject(); } } logger.error(""); if (!cliRequest.showErrors) { logger.error("To see the full stack trace of the errors, re-run Maven with the -e switch."); } if (!logger.isDebugEnabled()) { logger.error("Re-run Maven using the -X switch to enable full debug logging."); } if (!references.isEmpty()) { logger.error(""); logger.error( "For more information about the errors and possible solutions" + ", please read the following articles:"); for (Map.Entry<String, String> entry : references.entrySet()) { logger.error(entry.getValue() + " " + entry.getKey()); } } if (project != null && !project.equals(result.getTopologicallySortedProjects().get(0))) { logger.error(""); logger.error("After correcting the problems, you can resume the build with the command"); logger.error(" mvn <goals> -rf :" + project.getArtifactId()); } if (MavenExecutionRequest.REACTOR_FAIL_NEVER.equals( cliRequest.request.getReactorFailureBehavior())) { logger.info("Build failures were ignored."); return 0; } else { return 1; } } else { return 0; } }