Пример #1
0
  // TODO: need to externalize CliRequest
  public int doMain(CliRequest cliRequest) {
    PlexusContainer localContainer = null;
    try {
      initialize(cliRequest);
      cli(cliRequest);
      logging(cliRequest);
      version(cliRequest);
      properties(cliRequest);
      localContainer = container(cliRequest);
      commands(cliRequest);
      settings(cliRequest);
      toolchains(cliRequest);
      populateRequest(cliRequest);
      encryption(cliRequest);
      repository(cliRequest);
      return execute(cliRequest);
    } catch (ExitException e) {
      return e.exitCode;
    } catch (UnrecognizedOptionException e) {
      // pure user error, suppress stack trace
      return 1;
    } catch (BuildAbort e) {
      CLIReportingUtils.showError(slf4jLogger, "ABORTED", e, cliRequest.showErrors);

      return 2;
    } catch (Exception e) {
      CLIReportingUtils.showError(slf4jLogger, "Error executing Maven.", e, cliRequest.showErrors);

      return 1;
    } finally {
      if (localContainer != null) {
        localContainer.dispose();
      }
    }
  }
Пример #2
0
  static void populateProperties(
      CommandLine commandLine, Properties systemProperties, Properties userProperties) {
    EnvironmentUtils.addEnvVars(systemProperties);

    // ----------------------------------------------------------------------
    // Options that are set on the command line become system properties
    // and therefore are set in the session properties. System properties
    // are most dominant.
    // ----------------------------------------------------------------------

    if (commandLine.hasOption(CLIManager.SET_SYSTEM_PROPERTY)) {
      String[] defStrs = commandLine.getOptionValues(CLIManager.SET_SYSTEM_PROPERTY);

      if (defStrs != null) {
        for (String defStr : defStrs) {
          setCliProperty(defStr, userProperties);
        }
      }
    }

    SystemProperties.addSystemProperties(systemProperties);

    // ----------------------------------------------------------------------
    // Properties containing info about the currently running version of Maven
    // These override any corresponding properties set on the command line
    // ----------------------------------------------------------------------

    Properties buildProperties = CLIReportingUtils.getBuildProperties();

    String mavenVersion = buildProperties.getProperty(CLIReportingUtils.BUILD_VERSION_PROPERTY);
    systemProperties.setProperty("maven.version", mavenVersion);

    String mavenBuildVersion = CLIReportingUtils.createMavenVersionString(buildProperties);
    systemProperties.setProperty("maven.build.version", mavenBuildVersion);
  }
Пример #3
0
  private void cli(CliRequest cliRequest) throws Exception {
    //
    // Parsing errors can happen during the processing of the arguments and we prefer not having to
    // check if
    // the logger is null and construct this so we can use an SLF4J logger everywhere.
    //
    slf4jLogger = new Slf4jStdoutLogger();

    CLIManager cliManager = new CLIManager();

    try {
      cliRequest.commandLine = cliManager.parse(cliRequest.args);
    } catch (ParseException e) {
      System.err.println("Unable to parse command line options: " + e.getMessage());
      cliManager.displayHelp(System.out);
      throw e;
    }

    if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
      cliManager.displayHelp(System.out);
      throw new ExitException(0);
    }

    if (cliRequest.commandLine.hasOption(CLIManager.VERSION)) {
      System.out.println(CLIReportingUtils.showVersion());
      throw new ExitException(0);
    }
  }
Пример #4
0
 public String getVersion() {
   ByteArrayOutputStream buff = new ByteArrayOutputStream();
   PrintStream out = new PrintStream(buff);
   CLIReportingUtils.showVersion(out);
   Closer.close(out);
   return new String(buff.toByteArray());
 }
Пример #5
0
  // TODO: need to externalize CliRequest
  public int doMain(CliRequest cliRequest) {
    try {
      initialize(cliRequest);
      // Need to process cli options first to get possible logging options
      cli(cliRequest);
      logging(cliRequest);
      commands(cliRequest);
      properties(cliRequest);
      container(cliRequest);
      settings(cliRequest);
      populateRequest(cliRequest);
      encryption(cliRequest);
      return execute(cliRequest);
    } catch (ExitException e) {
      return e.exitCode;
    } catch (UnrecognizedOptionException e) {
      // pure user error, suppress stack trace
      return 1;
    } catch (Exception e) {
      CLIReportingUtils.showError(logger, "Error executing Maven.", e, cliRequest.showErrors);

      return 1;
    } finally {
      if (cliRequest.fileStream != null) {
        cliRequest.fileStream.close();
      }
    }
  }
Пример #6
0
    public int execute(final MavenExecutionRequest request) throws Exception {
      assert request != null;

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

      try {
        return doExecute(request);
      } catch (Exception e) {
        CLIReportingUtils.showError(
            logger, "Error executing Maven.", e, request.isShowErrors()); // TODO: i81n
        return 1;
      } finally {
        container.dispose();

        Closer.close(logStream);
      }
    }
Пример #7
0
  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.");
    }
  }
Пример #8
0
  //
  // Every bit of information taken from the CLI should be processed here.
  //
  private void cli(CliRequest cliRequest) throws Exception {
    CLIManager cliManager = new CLIManager();

    try {
      cliRequest.commandLine = cliManager.parse(cliRequest.args);
    } catch (ParseException e) {
      cliRequest.stderr.println("Unable to parse command line options: " + e.getMessage());
      cliManager.displayHelp(cliRequest.stdout);
      throw e;
    }

    // TODO: these should be moved out of here. Wrong place.
    //
    if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
      cliManager.displayHelp(cliRequest.stdout);
      throw new ExitException(0);
    }

    if (cliRequest.commandLine.hasOption(CLIManager.VERSION)) {
      CLIReportingUtils.showVersion(cliRequest.stdout);
      throw new ExitException(0);
    }
  }
Пример #9
0
  private PlexusContainer container(CliRequest cliRequest) throws Exception {
    if (cliRequest.classWorld == null) {
      cliRequest.classWorld =
          new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader());
    }

    DefaultPlexusContainer container;

    ContainerConfiguration cc =
        new DefaultContainerConfiguration()
            .setClassWorld(cliRequest.classWorld)
            .setRealm(setupContainerRealm(cliRequest))
            .setClassPathScanning(PlexusConstants.SCANNING_INDEX)
            .setAutoWiring(true)
            .setName("maven");

    container =
        new DefaultPlexusContainer(
            cc,
            new AbstractModule() {
              protected void configure() {
                bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
              }
            });

    // NOTE: To avoid inconsistencies, we'll use the TCCL exclusively for lookups
    container.setLookupRealm(null);

    container.setLoggerManager(plexusLoggerManager);

    customizeContainer(container);

    container.getLoggerManager().setThresholds(cliRequest.request.getLoggingLevel());

    Thread.currentThread().setContextClassLoader(container.getContainerRealm());

    eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);

    DefaultEventSpyContext eventSpyContext = new DefaultEventSpyContext();
    Map<String, Object> data = eventSpyContext.getData();
    data.put("plexus", container);
    data.put("workingDirectory", cliRequest.workingDirectory);
    data.put("systemProperties", cliRequest.systemProperties);
    data.put("userProperties", cliRequest.userProperties);
    data.put("versionProperties", CLIReportingUtils.getBuildProperties());
    eventSpyDispatcher.init(eventSpyContext);

    // refresh logger in case container got customized by spy
    slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName());

    maven = container.lookup(Maven.class);

    executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);

    modelProcessor = createModelProcessor(container);

    settingsBuilder = container.lookup(SettingsBuilder.class);

    toolchainsBuilder = container.lookup(ToolchainsBuilder.class);

    dispatcher = (DefaultSecDispatcher) container.lookup(SecDispatcher.class, "maven");

    return container;
  }
Пример #10
0
 private void version(CliRequest cliRequest) {
   if (cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.SHOW_VERSION)) {
     System.out.println(CLIReportingUtils.showVersion());
   }
 }
Пример #11
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;
      }
    }