コード例 #1
0
ファイル: MavenCli.java プロジェクト: sonatype/maven-demo
  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("");
    }
  }
コード例 #2
0
  /**
   * Builds Maven settings from request.
   *
   * @param request The request for new settings
   */
  public Settings buildSettings(SettingsBuildingRequest request) {
    SettingsBuildingResult result;
    try {
      SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();

      if (request.getGlobalSettingsFile() != null) {
        if (log.isLoggable(Level.FINE)) {
          log.fine(
              "Using "
                  + request.getGlobalSettingsFile().getAbsolutePath()
                  + " to get global Maven settings.xml");
        }
      }
      final File userSettingsFile = request.getUserSettingsFile();
      if (userSettingsFile != null) {
        if (log.isLoggable(Level.FINE)) {
          log.fine(
              "Using " + userSettingsFile.getAbsolutePath() + " to get user Maven settings.xml");
        }

        // Maven will not check the format passed in (any XML will do), so let's ensure we have a
        // settings.xml by checking just the top-level element
        final XMLStreamReader reader;
        try {
          reader =
              XMLInputFactory.newInstance()
                  .createXMLStreamReader(new FileInputStream(userSettingsFile));
          // get the first element name
          while (reader.hasNext()) {
            if (reader.next() == XMLStreamConstants.START_ELEMENT) {
              break;
            }
          }
          final String topLevel = reader.getLocalName();

          if (!"settings".equals(topLevel)) {
            throw new InvalidConfigurationFileException(
                "Invalid format settings.xml found: " + userSettingsFile);
          }
        } catch (final FileNotFoundException e) {
          // Ignore at this level
        } catch (final XMLStreamException xmlse) {
          throw new RuntimeException(
              "Could not check file format of specified settings.xml: " + userSettingsFile, xmlse);
        }
      }

      result = builder.build(request);
    }
    // wrap exception message
    catch (SettingsBuildingException e) {
      StringBuilder sb =
          new StringBuilder("Found ")
              .append(e.getProblems().size())
              .append(
                  " problems while building settings.xml model from both global Maven configuration file")
              .append(request.getGlobalSettingsFile())
              .append(" and/or user configuration file: ")
              .append(request.getUserSettingsFile())
              .append("\n");

      int counter = 1;
      for (SettingsProblem problem : e.getProblems()) {
        sb.append(counter++).append("/ ").append(problem).append("\n");
      }

      throw new InvalidConfigurationFileException(sb.toString());
    }

    // get settings object and update it according to property values
    Settings settings = result.getEffectiveSettings();
    settings = enrichWithLocalRepository(settings);
    settings = enrichWithOfflineMode(settings);
    return settings;
  }
コード例 #3
0
ファイル: MavenSystemImpl.java プロジェクト: sammyt/mvnsh
    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("");
      }
    }