Example usage for org.apache.maven.model.profile ProfileSelector getActiveProfiles

List of usage examples for org.apache.maven.model.profile ProfileSelector getActiveProfiles

Introduction

In this page you can find the example usage for org.apache.maven.model.profile ProfileSelector getActiveProfiles.

Prototype

List<Profile> getActiveProfiles(Collection<Profile> profiles, ProfileActivationContext context,
        ModelProblemCollector problems);

Source Link

Document

Determines the profiles which are active in the specified activation context.

Usage

From source file:org.commonjava.maven.ext.cli.Cli.java

License:Apache License

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));
    }/*from   ww w .  j  a  v  a 2s  .  c om*/
    List<org.apache.maven.model.Profile> activeModelProfiles = profileSelector.getActiveProfiles(modelProfiles,
            profileActivationContext, new ModelProblemCollector() {

                @Override
                public void add(ModelProblemCollectorRequest modelProblemCollectorRequest) {
                    // do nothing
                }
            });
    List<String> activeProfiles = new ArrayList<>();
    for (org.apache.maven.model.Profile profile : activeModelProfiles) {
        activeProfiles.add(profile.getId());
    }
    effectiveSettings.setActiveProfiles(activeProfiles);

    return effectiveSettings;
}

From source file:org.commonjava.maven.ext.manip.Cli.java

License:Apache License

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);

    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));
    }//from w  ww  .jav a  2s.  c  o m
    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;
}

From source file:org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionImpl.java

License:Apache License

private List<RemoteRepository> getRemoteRepositories() throws IllegalStateException {
    // disable repositories if working offline
    if (isOffline()) {
        log.log(Level.FINE, "No remote repositories will be available, working in offline mode");
        return Collections.emptyList();
    }//from  ww  w . j  a v a2s.co  m
    // the first repository defined is the first where we search
    Set<RemoteRepository> enhancedRepos = new LinkedHashSet<RemoteRepository>();

    // add repositories we defined by hand
    enhancedRepos.addAll(additionalRemoteRepositories);

    ProfileSelector selector = new SettingsXmlProfileSelector();
    LogModelProblemCollector problems = new LogModelProblemCollector();
    List<Profile> activeProfiles = selector.getActiveProfiles(MavenConverter.asProfiles(settings.getProfiles()),
            new ProfileActivationContext() {

                @Override
                public Map<String, String> getUserProperties() {
                    return Collections.emptyMap();
                }

                @SuppressWarnings({ "unchecked", "rawtypes" })
                @Override
                public Map<String, String> getSystemProperties() {
                    return new HashMap<String, String>((Map) SecurityActions.getProperties());
                }

                @Override
                public File getProjectDirectory() {
                    return new File(SecurityActions.getProperty("user.dir"));
                }

                @Override
                public Map<String, String> getProjectProperties() {
                    // TODO can we put here other values?
                    return Collections.emptyMap();
                }

                @Override
                public List<String> getInactiveProfileIds() {
                    return Collections.emptyList();
                }

                @Override
                public List<String> getActiveProfileIds() {
                    return settings.getActiveProfiles();
                }
            }, problems);

    if (problems.hasSevereFailures()) {
        throw new IllegalStateException("Unable to get active profiles from Maven settings.");
    }

    for (Profile p : activeProfiles) {
        for (Repository repository : p.getRepositories()) {
            RemoteRepository repo = MavenConverter.asRemoteRepository(repository);
            // add remote repository from model only if not overridden by code
            if (!isIdIncluded(additionalRemoteRepositories, repo)) {
                enhancedRepos.add(repo);
            }
        }
    }

    // add remote repositories
    for (RemoteRepository repo : remoteRepositories) {
        // add remote repository from model only if not overridden by code
        if (!isIdIncluded(additionalRemoteRepositories, repo)) {
            enhancedRepos.add(repo);
        }
    }

    // add maven central if selected but if not overridden by API
    if (useMavenCentralRepository) {
        if (!isIdIncluded(additionalRemoteRepositories, MAVEN_CENTRAL)) {
            enhancedRepos.add(MAVEN_CENTRAL);
        }
    } else {
        List<RemoteRepository> reposToRemove = new ArrayList<RemoteRepository>();
        // Attempt a remove

        for (final RemoteRepository repo : enhancedRepos) {
            // Because there are a lot of aliases for Maven Central, we have to approximate that anything named
            // "central" with URL containing "maven" is what we're looking to ban. For instance Central could be
            // http://repo.maven.apache.org/maven2 or http://repo1.maven.org/maven2
            final String repoUrl = repo.getUrl();
            if ((repoUrl.contains("maven.org") || repoUrl.contains("apache.org"))
                    && repo.getId().equalsIgnoreCase(MAVEN_CENTRAL_NAME)) {
                // remote this "might-be" central repository, but only if not specified by hand
                if (!isIdIncluded(additionalRemoteRepositories, MAVEN_CENTRAL)) {
                    reposToRemove.add(repo);
                }
            }
        }
        // We have to search on URL criteria, because .equals on RemoteRepository is too strict for us to call a
        // simple remove operation on the enhancedRepos Collection
        enhancedRepos.removeAll(reposToRemove);
    }

    // use mirrors if any to do the mirroring stuff
    DefaultMirrorSelector dms = new DefaultMirrorSelector();
    // fill in mirrors
    for (Mirror mirror : settings.getMirrors()) {
        // Repository manager flag is set to false
        // Maven does not support specifying it in the settings.xml
        dms.add(mirror.getId(), mirror.getUrl(), mirror.getLayout(), false, mirror.getMirrorOf(),
                mirror.getMirrorOfLayouts());
    }

    Set<RemoteRepository> mirroredRepos = new LinkedHashSet<RemoteRepository>();
    for (RemoteRepository repository : enhancedRepos) {
        RemoteRepository mirror = dms.getMirror(repository);
        if (mirror != null) {
            mirroredRepos.add(mirror);
        } else {
            mirroredRepos.add(repository);
        }
    }

    final Set<RemoteRepository> authorizedRepos = new LinkedHashSet<RemoteRepository>();
    for (RemoteRepository remoteRepository : mirroredRepos) {
        final RemoteRepository.Builder builder = new RemoteRepository.Builder(remoteRepository);

        // add authentication if <server> was provided in settings.xml file
        Server server = settings.getServer(remoteRepository.getId());
        if (server != null) {
            final AuthenticationBuilder authenticationBuilder = new AuthenticationBuilder()
                    .addUsername(server.getUsername()).addPassword(server.getPassword())
                    .addPrivateKey(server.getPrivateKey(), server.getPassphrase());
            builder.setAuthentication(authenticationBuilder.build());
        }

        authorizedRepos.add(builder.build());
    }

    if (log.isLoggable(Level.FINER)) {
        for (RemoteRepository repository : authorizedRepos) {
            log.finer(
                    "Repository " + repository.getUrl() + " have been made available for artifact resolution");
        }
    }

    return new ArrayList<RemoteRepository>(authorizedRepos);
}