Search in sources :

Example 1 with SettingsBuildingResult

use of org.apache.maven.settings.building.SettingsBuildingResult in project kie-wb-common by kiegroup.

the class AFSettingsXmlConfigurationProcessor method process.

@Override
public void process(AFCliRequest cliRequest) throws Exception {
    CommandLine commandLine = cliRequest.getCommandLine();
    String workingDirectory = cliRequest.getWorkingDirectory();
    MavenExecutionRequest request = cliRequest.getRequest();
    Path userSettingsFile;
    if (commandLine.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) {
        userSettingsFile = Paths.get(commandLine.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS));
        userSettingsFile = resolvePath(userSettingsFile, workingDirectory);
        if (!Files.isRegularFile(userSettingsFile)) {
            throw new FileNotFoundException("The specified user settings file does not exist: " + userSettingsFile);
        }
    } else {
        userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
    }
    Path globalSettingsFile;
    if (commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
        globalSettingsFile = Paths.get(commandLine.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS));
        globalSettingsFile = resolvePath(globalSettingsFile, workingDirectory);
        if (!Files.isRegularFile(globalSettingsFile)) {
            throw new FileNotFoundException("The specified global settings file does not exist: " + globalSettingsFile);
        }
    } else {
        globalSettingsFile = DEFAULT_GLOBAL_SETTINGS_FILE;
    }
    request.setGlobalSettingsFile(globalSettingsFile.toFile());
    request.setUserSettingsFile(userSettingsFile.toFile());
    AFSettingsBuildingRequest settingsRequest = new AFSettingsBuildingRequest();
    settingsRequest.setGlobalSettingsFile(globalSettingsFile.toFile());
    settingsRequest.setUserSettingsFile(userSettingsFile.toFile());
    settingsRequest.setSystemProperties(cliRequest.getSystemProperties());
    settingsRequest.setUserProperties(cliRequest.getUserProperties());
    if (request.getEventSpyDispatcher() != null) {
        request.getEventSpyDispatcher().onEvent(settingsRequest);
    }
    logger.debug("Reading global settings from " + getLocation(settingsRequest.getGlobalSettingsSource(), settingsRequest.getGlobalSettingsPath()));
    logger.debug("Reading user settings from " + getLocation(settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsPath()));
    SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest);
    if (request.getEventSpyDispatcher() != null) {
        request.getEventSpyDispatcher().onEvent(settingsResult);
    }
    populateFromSettings(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("");
    }
}
Also used : Path(java.nio.file.Path) CommandLine(org.apache.commons.cli.CommandLine) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) FileNotFoundException(java.io.FileNotFoundException) SettingsProblem(org.apache.maven.settings.building.SettingsProblem)

Example 2 with SettingsBuildingResult

use of org.apache.maven.settings.building.SettingsBuildingResult in project karaf by apache.

the class MavenConfigurationSupport method readSettings.

/**
 * Reads on demand <code>settings.xml</code> file - without password decryption. Also
 * collects declared servers by ID.
 * @param settingsFile
 */
protected synchronized Settings readSettings(File settingsFile) throws SettingsBuildingException {
    if (!settingsFile.isFile() || !settingsFile.canRead()) {
        return null;
    }
    try {
        DefaultSettingsBuilderFactory factory = new DefaultSettingsBuilderFactory();
        DefaultSettingsBuilder builder = factory.newInstance();
        SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
        request.setUserSettingsFile(settingsFile);
        SettingsBuildingResult result = builder.build(request);
        if (result.getProblems().size() > 0) {
            for (SettingsProblem problem : result.getProblems()) {
                System.err.println(problem);
            }
            return null;
        } else {
            Settings settings = result.getEffectiveSettings();
            if (settings.getServers() != null) {
                for (Server server : settings.getServers()) {
                    servers.put(server.getId(), server);
                }
            }
            return settings;
        }
    } catch (Throwable e) {
        System.err.println(e.getMessage());
        LOG.error(e.getMessage(), e);
        throw e;
    }
}
Also used : Server(org.apache.maven.settings.Server) 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) SettingsProblem(org.apache.maven.settings.building.SettingsProblem) Settings(org.apache.maven.settings.Settings) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuildingRequest(org.apache.maven.settings.building.SettingsBuildingRequest)

Example 3 with SettingsBuildingResult

use of org.apache.maven.settings.building.SettingsBuildingResult in project bazel by bazelbuild.

the class MavenServerFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, RepositoryFunctionException {
    String repository = (String) skyKey.argument();
    Rule repositoryRule = null;
    try {
        repositoryRule = RepositoryFunction.getRule(repository, env);
    } catch (RepositoryNotFoundException ex) {
    // Ignored. We throw a new one below.
    }
    BlazeDirectories directories = PrecomputedValue.BLAZE_DIRECTORIES.get(env);
    if (env.valuesMissing()) {
        return null;
    }
    String serverName;
    String url;
    Map<String, FileValue> settingsFiles;
    boolean foundRepoRule = repositoryRule != null && repositoryRule.getRuleClass().equals(MavenServerRule.NAME);
    if (!foundRepoRule) {
        if (repository.equals(MavenServerValue.DEFAULT_ID)) {
            settingsFiles = getDefaultSettingsFile(directories, env);
            serverName = MavenServerValue.DEFAULT_ID;
            url = MavenConnector.getMavenCentralRemote().getUrl();
        } else {
            throw new RepositoryFunctionException(new IOException("Could not find maven repository " + repository), Transience.TRANSIENT);
        }
    } else {
        WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(repositoryRule);
        serverName = repositoryRule.getName();
        try {
            url = mapper.get("url", Type.STRING);
            if (!mapper.isAttributeValueExplicitlySpecified("settings_file")) {
                settingsFiles = getDefaultSettingsFile(directories, env);
            } else {
                PathFragment settingsFilePath = new PathFragment(mapper.get("settings_file", Type.STRING));
                RootedPath settingsPath = RootedPath.toRootedPath(directories.getWorkspace().getRelative(settingsFilePath), PathFragment.EMPTY_FRAGMENT);
                FileValue fileValue = (FileValue) env.getValue(FileValue.key(settingsPath));
                if (fileValue == null) {
                    return null;
                }
                if (!fileValue.exists()) {
                    throw new RepositoryFunctionException(new IOException("Could not find settings file " + settingsPath), Transience.TRANSIENT);
                }
                settingsFiles = ImmutableMap.<String, FileValue>builder().put(USER_KEY, fileValue).build();
            }
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    }
    if (settingsFiles == null) {
        return null;
    }
    Fingerprint fingerprint = new Fingerprint();
    try {
        for (Map.Entry<String, FileValue> entry : settingsFiles.entrySet()) {
            fingerprint.addString(entry.getKey());
            Path path = entry.getValue().realRootedPath().asPath();
            if (path.exists()) {
                fingerprint.addBoolean(true);
                fingerprint.addBytes(path.getDigest());
            } else {
                fingerprint.addBoolean(false);
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    byte[] fingerprintBytes = fingerprint.digestAndReset();
    if (settingsFiles.isEmpty()) {
        return new MavenServerValue(serverName, url, new Server(), fingerprintBytes);
    }
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    if (settingsFiles.containsKey(SYSTEM_KEY)) {
        request.setGlobalSettingsFile(settingsFiles.get(SYSTEM_KEY).realRootedPath().asPath().getPathFile());
    }
    if (settingsFiles.containsKey(USER_KEY)) {
        request.setUserSettingsFile(settingsFiles.get(USER_KEY).realRootedPath().asPath().getPathFile());
    }
    DefaultSettingsBuilder builder = (new DefaultSettingsBuilderFactory()).newInstance();
    SettingsBuildingResult result;
    try {
        result = builder.build(request);
    } catch (SettingsBuildingException e) {
        throw new RepositoryFunctionException(new IOException("Error parsing settings files: " + e.getMessage()), Transience.TRANSIENT);
    }
    if (!result.getProblems().isEmpty()) {
        throw new RepositoryFunctionException(new IOException("Errors interpreting settings file: " + Arrays.toString(result.getProblems().toArray())), Transience.PERSISTENT);
    }
    Settings settings = result.getEffectiveSettings();
    Server server = settings.getServer(serverName);
    server = server == null ? new Server() : server;
    return new MavenServerValue(serverName, url, server, fingerprintBytes);
}
Also used : FileValue(com.google.devtools.build.lib.skyframe.FileValue) Server(org.apache.maven.settings.Server) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Settings(org.apache.maven.settings.Settings) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Fingerprint(com.google.devtools.build.lib.util.Fingerprint) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) RepositoryNotFoundException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryNotFoundException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) DefaultSettingsBuilder(org.apache.maven.settings.building.DefaultSettingsBuilder) MavenServerRule(com.google.devtools.build.lib.bazel.rules.workspace.MavenServerRule) Rule(com.google.devtools.build.lib.packages.Rule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) Nullable(javax.annotation.Nullable)

Example 4 with SettingsBuildingResult

use of org.apache.maven.settings.building.SettingsBuildingResult 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 5 with SettingsBuildingResult

use of org.apache.maven.settings.building.SettingsBuildingResult 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)

Aggregations

SettingsBuildingResult (org.apache.maven.settings.building.SettingsBuildingResult)7 DefaultSettingsBuildingRequest (org.apache.maven.settings.building.DefaultSettingsBuildingRequest)6 Settings (org.apache.maven.settings.Settings)5 DefaultSettingsBuilder (org.apache.maven.settings.building.DefaultSettingsBuilder)5 DefaultSettingsBuilderFactory (org.apache.maven.settings.building.DefaultSettingsBuilderFactory)5 SettingsBuildingException (org.apache.maven.settings.building.SettingsBuildingException)3 SettingsBuildingRequest (org.apache.maven.settings.building.SettingsBuildingRequest)3 File (java.io.File)2 Profile (org.apache.maven.settings.Profile)2 Server (org.apache.maven.settings.Server)2 SettingsProblem (org.apache.maven.settings.building.SettingsProblem)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 Rule (com.google.devtools.build.lib.packages.Rule)1 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)1 RepositoryNotFoundException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryNotFoundException)1 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)1 FileValue (com.google.devtools.build.lib.skyframe.FileValue)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1