Search in sources :

Example 1 with SettingsBuilder

use of org.apache.maven.settings.building.SettingsBuilder in project drools by kiegroup.

the class MavenSettings method initSettings.

private static Settings initSettings(SettingsSource userSettingsSource) {
    SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    if (userSettingsSource != null) {
        request.setUserSettingsSource(userSettingsSource);
    }
    String mavenHome = System.getenv("M2_HOME");
    if (mavenHome != null) {
        File globalSettingsFile = new File(mavenHome + "/conf/settings.xml");
        if (globalSettingsFile.exists()) {
            request.setGlobalSettingsFile(globalSettingsFile);
        }
    } else {
        log.warn("Environment variable M2_HOME is not set");
    }
    request.setSystemProperties(System.getProperties());
    Settings settings = null;
    try {
        settings = settingsBuilder.build(request).getEffectiveSettings();
    } catch (SettingsBuildingException e) {
        throw new RuntimeException(e);
    }
    if (settings.getLocalRepository() == null) {
        String userHome = System.getProperty("user.home");
        if (userHome != null) {
            settings.setLocalRepository(userHome + "/.m2/repository");
        } else {
            log.error("Cannot find maven local repository");
        }
    }
    return settings;
}
Also used : SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) SettingsBuilder(org.apache.maven.settings.building.SettingsBuilder) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) File(java.io.File) Settings(org.apache.maven.settings.Settings)

Example 2 with SettingsBuilder

use of org.apache.maven.settings.building.SettingsBuilder in project pom-manipulation-ext by release-engineering.

the class Cli method parseSettings.

private Settings parseSettings(File settings) throws PlexusContainerException, ComponentLookupException, SettingsBuildingException {
    PlexusContainer container = new DefaultPlexusContainer();
    DefaultSettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
    settingsRequest.setUserSettingsFile(settings);
    settingsRequest.setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE);
    settingsRequest.setUserProperties(session.getUserProperties());
    settingsRequest.setSystemProperties(System.getProperties());
    SettingsBuilder settingsBuilder = container.lookup(SettingsBuilder.class);
    SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest);
    Settings effectiveSettings = settingsResult.getEffectiveSettings();
    ProfileSelector profileSelector = container.lookup(ProfileSelector.class);
    ProfileActivationContext profileActivationContext = new DefaultProfileActivationContext().setActiveProfileIds(effectiveSettings.getActiveProfiles());
    List<org.apache.maven.model.Profile> modelProfiles = new ArrayList<>();
    for (Profile profile : effectiveSettings.getProfiles()) {
        modelProfiles.add(SettingsUtils.convertFromSettingsProfile(profile));
    }
    List<org.apache.maven.model.Profile> activeModelProfiles = profileSelector.getActiveProfiles(modelProfiles, profileActivationContext, new ModelProblemCollector() {

        @Override
        public void add(ModelProblem.Severity severity, String message, InputLocation location, Exception cause) {
        // do nothing
        }
    });
    List<String> activeProfiles = new ArrayList<>();
    for (org.apache.maven.model.Profile profile : activeModelProfiles) {
        activeProfiles.add(profile.getId());
    }
    effectiveSettings.setActiveProfiles(activeProfiles);
    return effectiveSettings;
}
Also used : InputLocation(org.apache.maven.model.InputLocation) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) ArrayList(java.util.ArrayList) Profile(org.apache.maven.settings.Profile) SettingsBuilder(org.apache.maven.settings.building.SettingsBuilder) ModelProblem(org.apache.maven.model.building.ModelProblem) ProfileSelector(org.apache.maven.model.profile.ProfileSelector) Settings(org.apache.maven.settings.Settings) DefaultProfileActivationContext(org.apache.maven.model.profile.DefaultProfileActivationContext) ProfileActivationContext(org.apache.maven.model.profile.ProfileActivationContext) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) PlexusContainerException(org.codehaus.plexus.PlexusContainerException) RestException(org.commonjava.maven.ext.io.rest.exception.RestException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) ParseException(org.apache.commons.cli.ParseException) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException) ModelProblemCollector(org.apache.maven.model.building.ModelProblemCollector) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) DefaultProfileActivationContext(org.apache.maven.model.profile.DefaultProfileActivationContext)

Example 3 with SettingsBuilder

use of org.apache.maven.settings.building.SettingsBuilder in project acceptance-test-harness by jenkinsci.

the class ArtifactResolverUtil method resolve.

/**
 * @param artifact The artifact to be resolved
 *
 * @return artifact resolution result
 */
public ArtifactResult resolve(DefaultArtifact artifact) {
    Builder repoBuilder = new RemoteRepository.Builder("repo.jenkins-ci.org", "default", "https://repo.jenkins-ci.org/public/");
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    File userHome = new File(System.getProperty("user.home"));
    File userSettingsFile = new File(new File(userHome, ".m2"), "settings.xml");
    request.setUserSettingsFile(userSettingsFile);
    if (userSettingsFile.exists()) {
        LOGGER.config("Found maven settings file - " + userSettingsFile.getAbsolutePath());
        SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
        try {
            Settings settings = settingsBuilder.build(request).getEffectiveSettings();
            org.apache.maven.settings.Proxy mavenActiveproxy = settings.getActiveProxy();
            if (mavenActiveproxy != null) {
                LOGGER.config("Found maven proxy settings. Will use for artifact resolution");
                repoBuilder.setProxy(asProxy(mavenActiveproxy));
            } else {
                LOGGER.config("Did not find an active proxy in maven settings xml file");
            }
        } catch (SettingsBuildingException e) {
            LOGGER.log(Level.WARNING, "Could not find or load settings.xml to attempt to user proxy settings.", e);
        }
    }
    RemoteRepository repo = repoBuilder.build();
    ArtifactResult r;
    try {
        r = repoSystem.resolveArtifact(repoSystemSession, new ArtifactRequest(artifact, Arrays.asList(repo), null));
    } catch (ArtifactResolutionException e) {
        throw new RuntimeException("Could not resolve " + artifact + " from Maven repository", e);
    }
    LOGGER.config("Found " + r);
    return r;
}
Also used : SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuilder(org.apache.maven.settings.building.SettingsBuilder) AuthenticationBuilder(org.eclipse.aether.util.repository.AuthenticationBuilder) Builder(org.eclipse.aether.repository.RemoteRepository.Builder) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) SettingsBuilder(org.apache.maven.settings.building.SettingsBuilder) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) File(java.io.File) Settings(org.apache.maven.settings.Settings)

Aggregations

Settings (org.apache.maven.settings.Settings)3 DefaultSettingsBuildingRequest (org.apache.maven.settings.building.DefaultSettingsBuildingRequest)3 SettingsBuilder (org.apache.maven.settings.building.SettingsBuilder)3 SettingsBuildingException (org.apache.maven.settings.building.SettingsBuildingException)3 File (java.io.File)2 DefaultSettingsBuilderFactory (org.apache.maven.settings.building.DefaultSettingsBuilderFactory)2 ArrayList (java.util.ArrayList)1 ParseException (org.apache.commons.cli.ParseException)1 MavenExecutionRequestPopulationException (org.apache.maven.execution.MavenExecutionRequestPopulationException)1 InputLocation (org.apache.maven.model.InputLocation)1 ModelProblem (org.apache.maven.model.building.ModelProblem)1 ModelProblemCollector (org.apache.maven.model.building.ModelProblemCollector)1 DefaultProfileActivationContext (org.apache.maven.model.profile.DefaultProfileActivationContext)1 ProfileActivationContext (org.apache.maven.model.profile.ProfileActivationContext)1 ProfileSelector (org.apache.maven.model.profile.ProfileSelector)1 Profile (org.apache.maven.settings.Profile)1 SettingsBuildingResult (org.apache.maven.settings.building.SettingsBuildingResult)1 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)1 PlexusContainer (org.codehaus.plexus.PlexusContainer)1 PlexusContainerException (org.codehaus.plexus.PlexusContainerException)1