Search in sources :

Example 11 with SettingsBuildingException

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

the class Cli method createSession.

private void createSession(File target, File settings) {
    try {
        PlexusContainer container = new DefaultPlexusContainer();
        pomIO = container.lookup(PomIO.class);
        session = container.lookup(ManipulationSession.class);
        manipulationManager = container.lookup(ManipulationManager.class);
        final MavenExecutionRequest req = new DefaultMavenExecutionRequest().setUserProperties(System.getProperties()).setUserProperties(userProps).setRemoteRepositories(Collections.<ArtifactRepository>emptyList());
        ArtifactRepository ar = null;
        if (settings == null) {
            // No, this is not a typo. If current default is null, supply new local and global.
            // This function passes in settings to make it easier to test.
            this.settings = settings = new File(System.getProperty("user.home"), ".m2/settings.xml");
            ar = new MavenArtifactRepository();
            ar.setUrl("file://" + System.getProperty("user.home") + "/.m2/repository");
            req.setLocalRepository(ar);
        }
        req.setUserSettingsFile(settings);
        req.setGlobalSettingsFile(settings);
        MavenExecutionRequestPopulator executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
        executionRequestPopulator.populateFromSettings(req, parseSettings(settings));
        executionRequestPopulator.populateDefaults(req);
        if (ar != null) {
            ar.setUrl("file://" + req.getLocalRepositoryPath());
        }
        if (userProps != null && userProps.containsKey("maven.repo.local")) {
            if (ar == null) {
                ar = new MavenArtifactRepository();
            }
            ar.setUrl("file://" + userProps.getProperty("maven.repo.local"));
            req.setLocalRepository(ar);
        }
        final MavenSession mavenSession = new MavenSession(container, null, req, new DefaultMavenExecutionResult());
        mavenSession.getRequest().setPom(target);
        session.setMavenSession(mavenSession);
    } catch (ComponentLookupException e) {
        logger.debug("Caught problem instantiating ", e);
        System.err.println("Unable to start Cli subsystem");
        System.exit(100);
    } catch (PlexusContainerException e) {
        logger.debug("Caught problem instantiating ", e);
        System.err.println("Unable to start Cli subsystem");
        System.exit(100);
    } catch (SettingsBuildingException e) {
        logger.debug("Caught problem parsing settings file ", e);
        System.err.println("Unable to parse settings.xml file");
        System.exit(100);
    } catch (MavenExecutionRequestPopulationException e) {
        logger.debug("Caught problem populating maven request from settings file ", e);
        System.err.println("Unable to create maven execution request from settings.xml file");
        System.exit(100);
    }
}
Also used : PlexusContainerException(org.codehaus.plexus.PlexusContainerException) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ManipulationManager(org.commonjava.maven.ext.core.ManipulationManager) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) PomIO(org.commonjava.maven.ext.io.PomIO) MavenSession(org.apache.maven.execution.MavenSession) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) File(java.io.File) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 12 with SettingsBuildingException

use of org.apache.maven.settings.building.SettingsBuildingException in project fabric8 by jboss-fuse.

the class MavenConfigurationImpl method buildSettings.

private Settings buildSettings(String localRepoPath, String settingsPath, boolean useFallbackRepositories) {
    Settings settings;
    if (settingsPath == null) {
        settings = new Settings();
    } else {
        DefaultSettingsBuilderFactory factory = new DefaultSettingsBuilderFactory();
        DefaultSettingsBuilder builder = factory.newInstance();
        SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
        request.setUserSettingsFile(new File(settingsPath));
        try {
            SettingsBuildingResult result = builder.build(request);
            settings = result.getEffectiveSettings();
        } catch (SettingsBuildingException exc) {
            throw new AssertionError("cannot build settings", exc);
        }
    }
    if (useFallbackRepositories) {
        Profile fallbackProfile = new Profile();
        Repository central = new Repository();
        central.setId("central");
        central.setUrl("http://repo1.maven.org/maven2");
        fallbackProfile.setId("fallback");
        fallbackProfile.setRepositories(Arrays.asList(central));
        settings.addProfile(fallbackProfile);
        settings.addActiveProfile("fallback");
    }
    if (localRepoPath != null) {
        settings.setLocalRepository(localRepoPath);
    }
    return settings;
}
Also used : SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Repository(org.apache.maven.settings.Repository) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) DefaultSettingsBuilder(org.apache.maven.settings.building.DefaultSettingsBuilder) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) File(java.io.File) Settings(org.apache.maven.settings.Settings) Profile(org.apache.maven.settings.Profile) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuildingRequest(org.apache.maven.settings.building.SettingsBuildingRequest)

Example 13 with SettingsBuildingException

use of org.apache.maven.settings.building.SettingsBuildingException 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", "http://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.debug("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.debug("Found maven proxy settings. Will use for artifact resolution");
                repoBuilder.setProxy(asProxy(mavenActiveproxy));
            } else {
                LOGGER.debug("Did not find an active proxy in maven settings xml file");
            }
        } catch (SettingsBuildingException e) {
            LOGGER.warn("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.debug("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)

Example 14 with SettingsBuildingException

use of org.apache.maven.settings.building.SettingsBuildingException in project sts4 by spring-projects.

the class MavenBridge method getSettings.

public synchronized Settings getSettings(final boolean force_reload) throws MavenException {
    // MUST NOT use createRequest!
    File userSettingsFile = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE;
    if (mavenConfiguration.getUserSettingsFile() != null) {
        userSettingsFile = new File(mavenConfiguration.getUserSettingsFile());
    }
    boolean reload = force_reload || settings == null;
    if (!reload && userSettingsFile != null) {
        reload = userSettingsFile.lastModified() != settings_timestamp || userSettingsFile.length() != settings_length;
    }
    if (reload) {
        // TODO: Can't that delegate to buildSettings()?
        SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
        // 440696 guard against ConcurrentModificationException
        Properties systemProperties = new Properties();
        copyProperties(systemProperties, System.getProperties());
        request.setSystemProperties(systemProperties);
        if (mavenConfiguration.getGlobalSettingsFile() != null) {
            request.setGlobalSettingsFile(new File(mavenConfiguration.getGlobalSettingsFile()));
        }
        if (userSettingsFile != null) {
            request.setUserSettingsFile(userSettingsFile);
        }
        try {
            settings = lookup(SettingsBuilder.class).build(request).getEffectiveSettings();
        } catch (SettingsBuildingException ex) {
            String msg = "Could not read settings.xml, assuming default values";
            log.error(msg, ex);
            /*
				 * NOTE: This method provides input for various other core
				 * functions, just bailing out would make m2e highly unusuable.
				 * Instead, we fail gracefully and just ignore the broken
				 * settings, using defaults.
				 */
            settings = new Settings();
        }
        if (userSettingsFile != null) {
            settings_length = userSettingsFile.length();
            settings_timestamp = userSettingsFile.lastModified();
        }
    }
    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) Properties(java.util.Properties) File(java.io.File) Settings(org.apache.maven.settings.Settings) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuildingRequest(org.apache.maven.settings.building.SettingsBuildingRequest)

Example 15 with SettingsBuildingException

use of org.apache.maven.settings.building.SettingsBuildingException in project maven-plugins by apache.

the class AbstractInvokerMojo method mergeSettings.

/**
 * Merge the settings file
 *
 * @param interpolatedSettingsFile The interpolated settings file.
 * @return The merged settings file.
 * @throws MojoExecutionException Fail the build in case the merged settings file can't be created.
 */
private File mergeSettings(File interpolatedSettingsFile) throws MojoExecutionException {
    File mergedSettingsFile;
    Settings mergedSettings = this.settings;
    if (mergeUserSettings) {
        if (interpolatedSettingsFile != null) {
            // Have to merge the specified settings file (dominant) and the one of the invoking Maven process
            try {
                SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
                request.setGlobalSettingsFile(interpolatedSettingsFile);
                Settings dominantSettings = settingsBuilder.build(request).getEffectiveSettings();
                Settings recessiveSettings = cloneSettings();
                SettingsUtils.merge(dominantSettings, recessiveSettings, TrackableBase.USER_LEVEL);
                mergedSettings = dominantSettings;
                getLog().debug("Merged specified settings file with settings of invoking process");
            } catch (SettingsBuildingException e) {
                throw new MojoExecutionException("Could not read specified settings file", e);
            }
        }
    }
    if (this.settingsFile != null && !mergeUserSettings) {
        mergedSettingsFile = interpolatedSettingsFile;
    } else {
        try {
            mergedSettingsFile = writeMergedSettingsFile(mergedSettings);
        } catch (IOException e) {
            throw new MojoExecutionException("Could not create temporary file for invoker settings.xml", e);
        }
    }
    return mergedSettingsFile;
}
Also used : SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) IOException(java.io.IOException) File(java.io.File) Settings(org.apache.maven.settings.Settings) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuildingRequest(org.apache.maven.settings.building.SettingsBuildingRequest)

Aggregations

SettingsBuildingException (org.apache.maven.settings.building.SettingsBuildingException)15 File (java.io.File)12 DefaultSettingsBuildingRequest (org.apache.maven.settings.building.DefaultSettingsBuildingRequest)12 Settings (org.apache.maven.settings.Settings)9 SettingsBuildingRequest (org.apache.maven.settings.building.SettingsBuildingRequest)8 DefaultSettingsBuilderFactory (org.apache.maven.settings.building.DefaultSettingsBuilderFactory)5 IOException (java.io.IOException)3 Profile (org.apache.maven.settings.Profile)3 SettingsBuilder (org.apache.maven.settings.building.SettingsBuilder)3 SettingsBuildingResult (org.apache.maven.settings.building.SettingsBuildingResult)3 Map (java.util.Map)2 MavenExecutionRequestPopulationException (org.apache.maven.execution.MavenExecutionRequestPopulationException)2 DefaultSettingsBuilder (org.apache.maven.settings.building.DefaultSettingsBuilder)2 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)2 PlexusContainer (org.codehaus.plexus.PlexusContainer)2 PlexusContainerException (org.codehaus.plexus.PlexusContainerException)2 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)1 MavenServerRule (com.google.devtools.build.lib.bazel.rules.workspace.MavenServerRule)1