org.apache.maven.settings.building.DefaultSettingsBuildingRequest Java Examples

The following examples show how to use org.apache.maven.settings.building.DefaultSettingsBuildingRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MavenSettingsReader.java    From spring-init with Apache License 2.0 6 votes vote down vote up
private Settings loadSettings() {
	File settingsFile = new File(this.homeDir, ".m2/settings.xml");
	if (settingsFile.exists()) {
		log.debug("Reading settings from: " + settingsFile);
	}
	else {
		log.debug("No settings found at: " + settingsFile);
	}
	SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
	request.setUserSettingsFile(settingsFile);
	request.setSystemProperties(System.getProperties());
	try {
		return new DefaultSettingsBuilderFactory().newInstance().build(request)
				.getEffectiveSettings();
	}
	catch (SettingsBuildingException ex) {
		throw new IllegalStateException(
				"Failed to build settings from " + settingsFile, ex);
	}
}
 
Example #2
Source File: Utils.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Settings loadMavenSettings()
    throws SettingsBuildingException {
  // http://stackoverflow.com/questions/27818659/loading-mavens-settings-xml-for-jcabi-aether-to-use
  SettingsBuildingRequest settingsBuildingRequest =
      new DefaultSettingsBuildingRequest();
  settingsBuildingRequest.setSystemProperties(System.getProperties());
  settingsBuildingRequest.setUserSettingsFile(new File(settingsXml));
  settingsBuildingRequest.setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE);

  SettingsBuildingResult settingsBuildingResult;
  DefaultSettingsBuilderFactory mvnSettingBuilderFactory =
      new DefaultSettingsBuilderFactory();
  DefaultSettingsBuilder settingsBuilder =
      mvnSettingBuilderFactory.newInstance();
  settingsBuildingResult = settingsBuilder.build(settingsBuildingRequest);

  Settings effectiveSettings = settingsBuildingResult.getEffectiveSettings();
  return effectiveSettings;
}
 
Example #3
Source File: MavenSettingsReader.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
private Settings loadSettings() {
	File settingsFile = new File(this.homeDir, ".m2/settings.xml");
	if (settingsFile.exists()) {
		log.info("Reading settings from: " + settingsFile);
	}
	else {
		log.info("No settings found at: " + settingsFile);
	}
	SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
	request.setUserSettingsFile(settingsFile);
	request.setSystemProperties(System.getProperties());
	try {
		return new DefaultSettingsBuilderFactory().newInstance().build(request)
				.getEffectiveSettings();
	}
	catch (SettingsBuildingException ex) {
		throw new IllegalStateException(
				"Failed to build settings from " + settingsFile, ex);
	}
}
 
Example #4
Source File: MavenPluginRepository.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
private Settings loadDefaultUserSettings() {
	String userHome = System.getProperty("user.home");
    File userMavenConfigurationHome = new File(userHome, ".m2");
    String envM2Home = System.getenv("M2_HOME");
    File DEFAULT_USER_SETTINGS_FILE = new File(userMavenConfigurationHome, "settings.xml");
    File DEFAULT_GLOBAL_SETTINGS_FILE = new File(System.getProperty("maven.home", envM2Home != null ? envM2Home : ""), "conf/settings.xml");
    SettingsBuildingRequest settingsBuildingRequest = new DefaultSettingsBuildingRequest();
    settingsBuildingRequest.setSystemProperties(System.getProperties());
    settingsBuildingRequest.setUserSettingsFile(DEFAULT_USER_SETTINGS_FILE);
    settingsBuildingRequest.setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE);

    DefaultSettingsBuilderFactory mvnSettingBuilderFactory = new DefaultSettingsBuilderFactory();
    DefaultSettingsBuilder settingsBuilder = mvnSettingBuilderFactory.newInstance();
    try {
		SettingsBuildingResult settingsBuildingResult = settingsBuilder.build(settingsBuildingRequest);
		Settings settings = settingsBuildingResult.getEffectiveSettings();
		return settings;
	} catch (SettingsBuildingException e) {
		e.printStackTrace();
	}
    return null;
}
 
Example #5
Source File: MavenRepositoriesTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testCentralWithProfiles() throws Exception
{
   SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
   SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
   settingsRequest.setUserSettingsFile(new File("src/test/resources/profiles/settings.xml"));
   Settings settings = settingsBuilder.build(settingsRequest).getEffectiveSettings();
   MavenContainer container = new MavenContainer();
   List<RemoteRepository> remoteRepositories = MavenRepositories.getRemoteRepositories(container, settings);
   Assert.assertEquals(2, remoteRepositories.size());
   Assert.assertEquals("test-repository", remoteRepositories.get(1).getId());
   
   List<RemoteRepository> centralRepos = remoteRepositories.stream().filter(repo -> repo.getId().equals("central")).collect(Collectors.toList());
   Assert.assertEquals(1, centralRepos.size());
   Assert.assertEquals(MavenRepositories.MAVEN_CENTRAL_REPO, centralRepos.get(0).getUrl());
}
 
Example #6
Source File: ConfigurationHelperTest.java    From LicenseScout with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a settings instance for testing.
 * @return a settings instance
 * @throws SettingsBuildingException
 */
private Settings createSettings() throws SettingsBuildingException {
    DefaultSettingsBuilderFactory settingsBuilderFactory = new DefaultSettingsBuilderFactory();
    SettingsBuilder settingsBuilder = settingsBuilderFactory.newInstance();
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    final File userSettingsFile = new File("src/test/resources/ConfigurationHelperTest/settings.xml");
    request.setUserSettingsFile(userSettingsFile);
    SettingsBuildingResult settingsBuildingResult = settingsBuilder.build(request);
    return settingsBuildingResult.getEffectiveSettings();
}
 
Example #7
Source File: MavenMvnSettings.java    From galleon with Apache License 2.0 5 votes vote down vote up
private static Settings buildMavenSettings(Path settingsPath) throws ArtifactException {
    SettingsBuildingRequest settingsBuildingRequest = new DefaultSettingsBuildingRequest();
    settingsBuildingRequest.setSystemProperties(System.getProperties());
    settingsBuildingRequest.setUserSettingsFile(settingsPath.toFile());
    SettingsBuildingResult settingsBuildingResult;
    DefaultSettingsBuilderFactory mvnSettingBuilderFactory = new DefaultSettingsBuilderFactory();
    DefaultSettingsBuilder settingsBuilder = mvnSettingBuilderFactory.newInstance();
    try {
        settingsBuildingResult = settingsBuilder.build(settingsBuildingRequest);
    } catch (SettingsBuildingException ex) {
        throw new ArtifactException(ex.getLocalizedMessage());
    }

    return settingsBuildingResult.getEffectiveSettings();
}
 
Example #8
Source File: MavenDependencyUtil.java    From DevToolBox with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * maven仓库处理
 *
 * @return
 * @throws Exception
 */
private static ArtifactRepository localRepository() throws Exception {
    if (mavenHome == null) {
        getMavenHome();
    }
    SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    request.setGlobalSettingsFile(new File(mavenHome, "/conf/settings.xml"));

    DefaultSettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();
    SettingsBuildingResult ret = builder.build(request);

    String path = ret.getEffectiveSettings().getLocalRepository();
    path = path == null ? String.format("%s%s", System.getProperty("user.home"), "\\.m2\\repository") : path;
    return FACTORY.createArtifactRepository("local", "file://" + path, (ArtifactRepositoryLayout) new DefaultRepositoryLayout(), null, null);
}
 
Example #9
Source File: BootstrapMavenContext.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public Settings getEffectiveSettings() throws BootstrapMavenException {
    if (settings != null) {
        return settings;
    }

    final Settings effectiveSettings;
    try {
        final SettingsBuildingResult result = new DefaultSettingsBuilderFactory()
                .newInstance().build(new DefaultSettingsBuildingRequest()
                        .setSystemProperties(System.getProperties())
                        .setUserSettingsFile(getUserSettings())
                        .setGlobalSettingsFile(getGlobalSettings()));
        final List<SettingsProblem> problems = result.getProblems();
        if (!problems.isEmpty()) {
            for (SettingsProblem problem : problems) {
                switch (problem.getSeverity()) {
                    case ERROR:
                    case FATAL:
                        throw new BootstrapMavenException("Settings problem encountered at " + problem.getLocation(),
                                problem.getException());
                    default:
                        log.warn("Settings problem encountered at " + problem.getLocation(), problem.getException());
                }
            }
        }
        effectiveSettings = result.getEffectiveSettings();
    } catch (SettingsBuildingException e) {
        throw new BootstrapMavenException("Failed to initialize Maven repository settings", e);
    }
    return settings = effectiveSettings;
}
 
Example #10
Source File: DefaultSettingsBuildingRequestHandler.java    From pipeline-maven-plugin with MIT License 5 votes vote down vote up
@Override
public boolean _handle(DefaultSettingsBuildingRequest request) {

    Xpp3Dom root = new Xpp3Dom("DefaultSettingsBuildingRequest");
    root.setAttribute("class", request.getClass().getName());
    root.addChild(newElement("userSettingsFile", request.getUserSettingsFile()));
    root.addChild(newElement("globalSettings", request.getGlobalSettingsFile()));

    reporter.print(root);
    return true;
}
 
Example #11
Source File: MavenRepositoriesTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testMirrorCentralWithoutProfiles() throws Exception
{
   SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
   SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
   settingsRequest.setUserSettingsFile(new File("src/test/resources/profiles/mirror-settings.xml"));
   Settings settings = settingsBuilder.build(settingsRequest).getEffectiveSettings();
   MavenContainer container = new MavenContainer();
   List<RemoteRepository> remoteRepositories = MavenRepositories.getRemoteRepositories(container, settings);
   Assert.assertThat(remoteRepositories.size(), equalTo(1));
   Assert.assertThat(remoteRepositories.get(0).getId(), equalTo("central"));
   Assert.assertThat(remoteRepositories.get(0).getUrl(),
            equalTo("http://repo.cloudbees.com/content/repositories/central/"));
}
 
Example #12
Source File: SettingsIO.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
public void update( Settings settings, File settingsFile )
    throws ManipulationException
{
    try
    {
        Settings defaultSettings = new Settings();

        if ( settingsFile.exists() )
        {
            DefaultSettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
            settingsRequest.setGlobalSettingsFile( settingsFile );
            defaultSettings = settingsBuilder.build( settingsRequest ).getEffectiveSettings();
        }

        for ( Profile profile : settings.getProfiles() )
        {
            defaultSettings.getProfiles().removeIf( profile1 -> profile1.getId().equals( profile.getId() ) );
            defaultSettings.addProfile( profile );
        }
        for ( String activeProfile : settings.getActiveProfiles() )
        {
            defaultSettings.getActiveProfiles().removeIf( s -> s.equals( activeProfile ) );
            defaultSettings.addActiveProfile( activeProfile );
        }
        for ( Mirror mirror : settings.getMirrors() )
        {
            defaultSettings.addMirror( mirror );
        }
        for ( Proxy proxy : settings.getProxies() )
        {
            defaultSettings.addProxy( proxy );
        }
        for ( Server server : settings.getServers() )
        {
            defaultSettings.addServer( server );
        }
        for ( String pluginGroup : settings.getPluginGroups() )
        {
            defaultSettings.addPluginGroup( pluginGroup );
        }
        if ( settings.getLocalRepository() != null )
        {
            defaultSettings.setLocalRepository( settings.getLocalRepository() );
        }

        write( defaultSettings, settingsFile );
    }
    catch ( SettingsBuildingException e )
    {
        throw new ManipulationException( "Failed to build existing settings.xml for repo removal backup.", settingsFile, e.getMessage(), e );
    }
}
 
Example #13
Source File: MavenContainer.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
public Settings getSettings()
{
   try
   {
      SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
      SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
      String userSettingsLocation = System.getProperty(ALT_USER_SETTINGS_XML_LOCATION);
      // TeamCity sets the settings.xml in a different system property
      String teamCitySettingsLocation = System.getProperty("teamcity.maven.userSettings.path");
      if (userSettingsLocation != null)
      {
         settingsRequest.setUserSettingsFile(new File(userSettingsLocation));
      }
      else if (teamCitySettingsLocation != null)
      {
         settingsRequest.setUserSettingsFile(new File(teamCitySettingsLocation));
      }
      else
      {
         settingsRequest.setUserSettingsFile(new File(getUserHomeDir(), "/.m2/settings.xml"));
      }
      String globalSettingsLocation = System.getProperty(ALT_GLOBAL_SETTINGS_XML_LOCATION);
      if (globalSettingsLocation != null)
      {
         settingsRequest.setGlobalSettingsFile(new File(globalSettingsLocation));
      }
      else
      {
         if (M2_HOME != null)
         {
            settingsRequest.setGlobalSettingsFile(new File(M2_HOME, "/conf/settings.xml"));
         }
      }
      SettingsBuildingResult settingsBuildingResult = settingsBuilder.build(settingsRequest);
      Settings effectiveSettings = settingsBuildingResult.getEffectiveSettings();

      if (effectiveSettings.getLocalRepository() == null)
      {
         String userRepositoryLocation = System.getProperty(ALT_LOCAL_REPOSITORY_LOCATION);
         if (userRepositoryLocation != null)
         {
            effectiveSettings.setLocalRepository(userRepositoryLocation);
         }
         else
         {
            effectiveSettings.setLocalRepository(getUserHomePath() + "/.m2/repository");
         }
      }

      return effectiveSettings;
   }
   catch (SettingsBuildingException e)
   {
      throw new RuntimeException(e);
   }
}