Example #1
0
  @SuppressWarnings("checkstyle:methodlength")
  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;
    }

    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;
    }

    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);

    eventSpyDispatcher.onEvent(settingsRequest);

    slf4jLogger.debug(
        "Reading global settings from "
            + getLocation(
                settingsRequest.getGlobalSettingsSource(),
                settingsRequest.getGlobalSettingsFile()));
    slf4jLogger.debug(
        "Reading user settings from "
            + getLocation(
                settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsFile()));

    SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest);

    eventSpyDispatcher.onEvent(settingsResult);

    executionRequestPopulator.populateFromSettings(
        cliRequest.request, settingsResult.getEffectiveSettings());

    if (!settingsResult.getProblems().isEmpty() && slf4jLogger.isWarnEnabled()) {
      slf4jLogger.warn("");
      slf4jLogger.warn("Some problems were encountered while building the effective settings");

      for (SettingsProblem problem : settingsResult.getProblems()) {
        slf4jLogger.warn(problem.getMessage() + " @ " + problem.getLocation());
      }

      slf4jLogger.warn("");
    }
  }
  /**
   * 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;
  }