Example #1
0
    private void init() throws Exception {
      log.debug("Initializing");

      // Make sure maven.home is absolute to avoid confusion on windows
      String mavenHome = System.getProperty(MAVEN_HOME);
      if (mavenHome != null) {
        System.setProperty(MAVEN_HOME, new File(mavenHome).getAbsolutePath());
      }

      // Setup defaults
      if (config.getClassWorld() == null) {
        config.setClassWorld(plexus.getClassWorld());
      }

      if (config.getBaseDirectory() == null) {
        config.setBaseDirectory(new File(System.getProperty("user.dir")));
      }

      StreamSet streams = config.getStreams();
      if (streams == null) {
        streams = StreamSet.system();
      }
      config.setStreams(streams);

      // Configure logging
      this.logger = config.getLogger();
      if (logger == null) {
        logger = new PrintStreamLogger(streams.out);
      } else {
        logger.setStream(streams.out);
      }
      config.setLogger(logger);

      int level = MavenExecutionRequest.LOGGING_LEVEL_INFO;
      if (config.isDebug()) {
        level = MavenExecutionRequest.LOGGING_LEVEL_DEBUG;
      } else if (config.isQuiet()) {
        level = MavenExecutionRequest.LOGGING_LEVEL_ERROR;
      }
      logger.setThreshold(level);

      File logFile = config.getLogFile();
      if (logFile != null) {
        logFile = resolveFile(logFile, config.getBaseDirectory());

        try {
          logStream = new PrintStream(logFile);
          logger.setStream(logStream);
        } catch (FileNotFoundException e) {
          log.warn("Failed to open logging stream for file: " + logFile, e);
          logger.setStream(streams.out);
        }
      }

      // Setup the container
      this.container = createContainer();
      log.debug("Using container: {}", container);
    }
Example #2
0
    private void logSummary(
        final ExceptionSummary summary,
        final Map<String, String> references,
        String indent,
        final boolean showErrors) {
      assert summary != null;

      String referenceKey = "";

      // TODO: i18n

      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 = indent + summary.getMessage();

      if (StringUtils.isNotEmpty(referenceKey)) {
        if (msg.indexOf('\n') < 0) {
          msg += " -> " + referenceKey;
        } else {
          msg += '\n' + indent + "-> " + referenceKey;
        }
      }

      if (showErrors) {
        //noinspection ThrowableResultOfMethodCallIgnored
        logger.error(msg, summary.getException());
      } else {
        logger.error(msg);
      }

      indent += "  ";

      for (ExceptionSummary child : summary.getChildren()) {
        logSummary(child, references, indent, showErrors);
      }
    }
Example #3
0
    protected void configureContainer(final DefaultPlexusContainer c) throws Exception {
      assert c != null;

      c.setLoggerManager(new MavenLoggerManager(config.getLogger()));
      c.getLoggerManager().setThresholds(logger.getThreshold());

      // If there is a configuration delegate then call it
      if (config.getDelegate() != null) {
        config.getDelegate().configure(c);
      }
    }
Example #4
0
    private int doExecute(final MavenExecutionRequest request) throws Exception {
      assert request != null;
      assert config != null;

      if (config.isDebug() || config.isShowVersion()) {
        CLIReportingUtils.showVersion(config.getStreams().out);
      }

      //
      // TODO: i18n all of this
      //

      if (request.isShowErrors()) {
        logger.info("Error stack-traces are turned on.");
      }
      if (MavenExecutionRequest.CHECKSUM_POLICY_WARN.equals(request.getGlobalChecksumPolicy())) {
        logger.info("Disabling strict checksum verification on all artifact downloads.");
      } else if (MavenExecutionRequest.CHECKSUM_POLICY_FAIL.equals(
          request.getGlobalChecksumPolicy())) {
        logger.info("Enabling strict checksum verification on all artifact downloads.");
      }

      if (log.isDebugEnabled()) {
        log.debug("Executing request: {}", Yarn.render(request, Yarn.Style.MULTI));
      }

      MavenExecutionResult result;
      Maven maven = container.lookup(Maven.class);
      try {
        result = maven.execute(request);
      } finally {
        container.release(maven);
      }

      if (!result.hasExceptions()) {
        return 0;
      }
      // else process exceptions

      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, "", request.isShowErrors());

        if (project == null && exception instanceof LifecycleExecutionException) {
          project = ((LifecycleExecutionException) exception).getProject();
        }
      }

      logger.error("");

      if (!request.isShowErrors()) {
        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(request.getReactorFailureBehavior())) {
        logger.info("Build failures were ignored.");
        return 0;
      } else {
        return 1;
      }
    }
Example #5
0
    private void configureRequest(final MavenExecutionRequest request) throws Exception {
      assert request != null;
      assert config != null;

      File dir = new File(config.getBaseDirectory(), "").getAbsoluteFile();
      request.setBaseDirectory(dir);

      // HACK: Some bits need user.dir to be set, or use un-rooted File's :-(
      System.setProperty("user.dir", dir.getAbsolutePath());

      // Configure profiles
      for (String profile : config.getProfiles()) {
        profile = profile.trim();

        if (profile.startsWith("-") || profile.startsWith("!")) {
          request.addInactiveProfile(profile.substring(1));
        } else if (profile.startsWith("+")) {
          request.addActiveProfile(profile.substring(1));
        } else {
          request.addActiveProfile(profile);
        }
      }

      // Configure user toolchains
      File userToolchainsFile = request.getUserToolchainsFile();
      if (userToolchainsFile != null) {
        userToolchainsFile = resolveFile(userToolchainsFile, config.getBaseDirectory());
      } else {
        userToolchainsFile = DEFAULT_USER_TOOLCHAINS_FILE;
      }
      request.setUserToolchainsFile(userToolchainsFile);

      // Configure the pom
      File alternatePomFile = config.getPomFile();
      if (alternatePomFile != null) {
        request.setPom(resolveFile(alternatePomFile, config.getBaseDirectory()));
      } else if (request.getPom() != null && !request.getPom().isAbsolute()) {
        request.setPom(request.getPom().getAbsoluteFile());
      }

      if ((request.getPom() != null) && (request.getPom().getParentFile() != null)) {
        request.setBaseDirectory(request.getPom().getParentFile());
      } else if (request.getPom() == null && request.getBaseDirectory() != null) {
        ModelProcessor modelProcessor = container.lookup(ModelProcessor.class);
        try {
          File pom = modelProcessor.locatePom(new File(request.getBaseDirectory()));
          request.setPom(pom);
        } finally {
          container.release(modelProcessor);
        }
      } else if (request.getBaseDirectory() == null) {
        request.setBaseDirectory(config.getBaseDirectory());
      }

      // Configure the local repo path
      String localRepoPath = request.getUserProperties().getProperty(LOCAL_REPO);
      if (localRepoPath == null) {
        localRepoPath = request.getSystemProperties().getProperty(LOCAL_REPO);
      }
      if (localRepoPath != null) {
        request.setLocalRepositoryPath(localRepoPath);
      }

      // Setup the xfr listener
      ArtifactTransferListener transferListener;
      if (request.isInteractiveMode()) {
        transferListener = new ConsoleMavenTransferListener(config.getStreams().out);
      } else {
        transferListener = new BatchModeMavenTransferListener(config.getStreams().out);
      }
      transferListener.setShowChecksumEvents(false);
      request.setTransferListener(transferListener);

      // Configure request logging
      request.setLoggingLevel(logger.getThreshold());
      request.setExecutionListener(new ExecutionEventLogger(terminal.get(), logger));
    }
Example #6
0
    private void configureSettings(final MavenExecutionRequest request) throws Exception {
      assert request != null;
      assert config != null;

      File userSettingsFile = config.getSettingsFile();
      if (userSettingsFile != null) {
        userSettingsFile = resolveFile(userSettingsFile, config.getBaseDirectory());
        if (!userSettingsFile.isFile()) {
          throw new FileNotFoundException(
              "The specified user settings file does not exist: " + userSettingsFile); // TODO: i18n
        }
      } else {
        userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
      }

      logger.debug("Reading user settings from: " + userSettingsFile);
      request.setUserSettingsFile(userSettingsFile);

      File globalSettingsFile = config.getGlobalSettingsFile();
      if (globalSettingsFile != null) {
        globalSettingsFile = resolveFile(globalSettingsFile, config.getBaseDirectory());
        if (!globalSettingsFile.isFile()) {
          throw new FileNotFoundException(
              "The specified global settings file does not exist: "
                  + globalSettingsFile); // TODO: i18n
        }
      } else {
        globalSettingsFile = DEFAULT_GLOBAL_SETTINGS_FILE;
      }

      logger.debug("Reading global settings from: " + globalSettingsFile);
      request.setGlobalSettingsFile(globalSettingsFile);

      configureProperties(request);

      SettingsBuildingRequest settingsRequest =
          new DefaultSettingsBuildingRequest()
              .setGlobalSettingsFile(globalSettingsFile)
              .setUserSettingsFile(userSettingsFile)
              .setSystemProperties(request.getSystemProperties())
              .setUserProperties(request.getUserProperties());

      SettingsBuildingResult settingsResult;
      SettingsBuilder settingsBuilder = container.lookup(SettingsBuilder.class);
      try {
        settingsResult = settingsBuilder.build(settingsRequest);
      } finally {
        container.release(settingsBuilder);
      }

      // NOTE: This will nuke some details from the request; profiles, online, etc... :-(
      MavenExecutionRequestPopulator populator =
          container.lookup(MavenExecutionRequestPopulator.class);
      try {
        populator.populateFromSettings(request, settingsResult.getEffectiveSettings());
      } finally {
        container.release(populator);
      }

      if (!settingsResult.getProblems().isEmpty() && logger.isWarnEnabled()) {
        logger.warn("");
        logger.warn(
            "Some problems were encountered while building the effective settings"); // TODO: i18n

        for (SettingsProblem problem : settingsResult.getProblems()) {
          logger.warn(problem.getMessage() + " @ " + problem.getLocation()); // TODO: i18n
        }

        logger.warn("");
      }
    }