private static Settings buildSettings(
      SettingsBuilder builder,
      MavenServerSettings settings,
      Properties systemProperties,
      Properties userProperties)
      throws RemoteException {
    SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
    settingsRequest.setGlobalSettingsFile(settings.getGlobalSettingsFile());
    settingsRequest.setUserSettingsFile(settings.getUserSettingsFile());
    settingsRequest.setSystemProperties(systemProperties);
    settingsRequest.setUserProperties(userProperties);

    Settings result = new Settings();
    try {
      result = builder.build(settingsRequest).getEffectiveSettings();
    } catch (SettingsBuildingException e) {
      Maven3ServerGlobals.getLogger().info(e);
    }

    if (settings.getLocalRepository() != null) {
      result.setLocalRepository(settings.getLocalRepository().getPath());
    }

    if (result.getLocalRepository() == null) {
      result.setLocalRepository(
          new File(SystemProperties.getUserHome(), ".m2/repository").getPath());
    }

    return result;
  }
Esempio n. 2
0
  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("");
    }
  }
Esempio n. 3
0
  /**
   * Loads default Maven settings from standard location or from a location specified by a property
   *
   * @return
   */
  public Settings buildDefaultSettings() {
    SettingsBuildingRequest request =
        new DefaultSettingsBuildingRequest().setSystemProperties(SecurityActions.getProperties());

    String altUserSettings = SecurityActions.getProperty(ALT_USER_SETTINGS_XML_LOCATION);
    String altGlobalSettings = SecurityActions.getProperty(ALT_GLOBAL_SETTINGS_XML_LOCATION);

    request.setUserSettingsFile(new File(DEFAULT_USER_SETTINGS_PATH));
    // set alternate files
    if (altUserSettings != null && altUserSettings.length() > 0) {
      request.setUserSettingsFile(new File(altUserSettings));
    }

    if (altGlobalSettings != null && altGlobalSettings.length() > 0) {
      request.setGlobalSettingsFile(new File(altGlobalSettings));
    }

    return buildSettings(request);
  }
Esempio n. 4
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;
  }