Example usage for org.apache.maven.project MavenProject getInjectedProfileIds

List of usage examples for org.apache.maven.project MavenProject getInjectedProfileIds

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getInjectedProfileIds.

Prototype

public Map<String, List<String>> getInjectedProfileIds() 

Source Link

Document

Gets the identifiers of all profiles that contributed to this project's effective model.

Usage

From source file:org.eclipse.m2e.editor.xml.internal.MarkerLocationService.java

License:Open Source License

private static void checkManagedDependencies(IMavenMarkerManager mavenMarkerManager, Element root,
        IResource pomFile, MavenProject mavenproject, String type, IStructuredDocument document)
        throws CoreException {
    List<Element> candidates = new ArrayList<Element>();

    Element dependencies = findChild(root, PomEdits.DEPENDENCIES);
    if (dependencies != null) {
        for (Element el : findChilds(dependencies, PomEdits.DEPENDENCY)) {
            Element version = findChild(el, PomEdits.VERSION);
            if (version != null) {
                candidates.add(el);/*from   ww w  .  j a va  2s  .  c om*/
            }
        }
    }
    //we should also consider <dependencies> section in the profiles, but profile are optional and so is their
    // dependencyManagement section.. that makes handling our markers more complex.
    // see MavenProject.getInjectedProfileIds() for a list of currently active profiles in effective pom
    String currentProjectKey = mavenproject.getGroupId() + ":" + mavenproject.getArtifactId() + ":" //$NON-NLS-1$//$NON-NLS-2$
            + mavenproject.getVersion();
    List<String> activeprofiles = mavenproject.getInjectedProfileIds().get(currentProjectKey);
    //remember what profile we found the dependency in.
    Map<Element, String> candidateProfile = new HashMap<Element, String>();
    Element profiles = findChild(root, PomEdits.PROFILES);
    if (profiles != null) {
        for (Element profile : findChilds(profiles, PomEdits.PROFILE)) {
            String idString = getTextValue(findChild(profile, PomEdits.ID));
            if (idString != null && activeprofiles.contains(idString)) {
                dependencies = findChild(profile, PomEdits.DEPENDENCIES);
                if (dependencies != null) {
                    for (Element el : findChilds(dependencies, PomEdits.DEPENDENCY)) {
                        Element version = findChild(el, PomEdits.VERSION);
                        if (version != null) {
                            candidates.add(el);
                            candidateProfile.put(el, idString);
                        }
                    }
                }
            }
        }
    }
    //collect the managed dep ids
    Map<String, String> managed = new HashMap<String, String>();
    DependencyManagement dm = mavenproject.getDependencyManagement();
    if (dm != null) {
        List<Dependency> deps = dm.getDependencies();
        if (deps != null) {
            for (Dependency dep : deps) {
                if (dep.getVersion() != null) { //#335366
                    //shall we be using geManagementkey() here? but it contains also the type, not only the gr+art ids..
                    managed.put(dep.getGroupId() + ":" + dep.getArtifactId(), dep.getVersion()); //$NON-NLS-1$
                }
            }
        }
    }

    //now we have all the candidates, match them against the effective managed set 
    for (Element dep : candidates) {
        Element version = findChild(dep, PomEdits.VERSION);
        String grpString = getTextValue(findChild(dep, PomEdits.GROUP_ID));
        String artString = getTextValue(findChild(dep, PomEdits.ARTIFACT_ID));
        String versionString = getTextValue(version);
        if (grpString != null && artString != null && versionString != null) {
            String id = grpString + ":" + artString; //$NON-NLS-1$
            if (managed.containsKey(id)) {
                String managedVersion = managed.get(id);
                if (version instanceof IndexedRegion) {
                    IndexedRegion off = (IndexedRegion) version;
                    if (lookForIgnoreMarker(document, version, off, IMavenConstants.MARKER_IGNORE_MANAGED)) {
                        continue;
                    }

                    IMarker mark = mavenMarkerManager.addMarker(pomFile, type,
                            NLS.bind(org.eclipse.m2e.core.internal.Messages.MavenMarkerManager_managed_title,
                                    managedVersion, artString),
                            document.getLineOfOffset(off.getStartOffset()) + 1, IMarker.SEVERITY_WARNING);
                    mark.setAttribute(IMavenConstants.MARKER_ATTR_EDITOR_HINT,
                            IMavenConstants.EDITOR_HINT_MANAGED_DEPENDENCY_OVERRIDE);
                    mark.setAttribute(IMarker.CHAR_START, off.getStartOffset());
                    mark.setAttribute(IMarker.CHAR_END, off.getEndOffset());
                    mark.setAttribute("problemType", "pomhint"); //only imporant in case we enable the generic xml quick fixes //$NON-NLS-1$ //$NON-NLS-2$
                    //add these attributes to easily and deterministicaly find the declaration in question
                    mark.setAttribute("groupId", grpString); //$NON-NLS-1$
                    mark.setAttribute("artifactId", artString); //$NON-NLS-1$
                    String profile = candidateProfile.get(dep);
                    if (profile != null) {
                        mark.setAttribute("profile", profile); //$NON-NLS-1$
                    }
                }
            }
        }
    }
}

From source file:org.eclipse.m2e.editor.xml.internal.MarkerLocationService.java

License:Open Source License

private static void checkManagedPlugins(IMavenMarkerManager mavenMarkerManager, Element root, IResource pomFile,
        MavenProject mavenproject, String type, IStructuredDocument document) throws CoreException {
    List<Element> candidates = new ArrayList<Element>();
    Element build = findChild(root, PomEdits.BUILD);
    if (build == null) {
        return;//w  w w .  jav a  2 s  .com
    }
    Element plugins = findChild(build, PomEdits.PLUGINS);
    if (plugins != null) {
        for (Element el : findChilds(plugins, PomEdits.PLUGIN)) {
            Element version = findChild(el, PomEdits.VERSION);
            if (version != null) {
                candidates.add(el);
            }
        }
    }
    //we should also consider <plugins> section in the profiles, but profile are optional and so is their
    // pluginManagement section.. that makes handling our markers more complex.
    // see MavenProject.getInjectedProfileIds() for a list of currently active profiles in effective pom
    String currentProjectKey = mavenproject.getGroupId() + ":" + mavenproject.getArtifactId() + ":" //$NON-NLS-1$//$NON-NLS-2$
            + mavenproject.getVersion();
    List<String> activeprofiles = mavenproject.getInjectedProfileIds().get(currentProjectKey);
    //remember what profile we found the dependency in.
    Map<Element, String> candidateProfile = new HashMap<Element, String>();
    Element profiles = findChild(root, PomEdits.PROFILES);
    if (profiles != null) {
        for (Element profile : findChilds(profiles, PomEdits.PROFILE)) {
            String idString = getTextValue(findChild(profile, PomEdits.ID));
            if (idString != null && activeprofiles.contains(idString)) {
                build = findChild(profile, PomEdits.BUILD);
                if (build == null) {
                    continue;
                }
                plugins = findChild(build, PomEdits.PLUGINS);
                if (plugins != null) {
                    for (Element el : findChilds(plugins, PomEdits.PLUGIN)) {
                        Element version = findChild(el, PomEdits.VERSION);
                        if (version != null) {
                            candidates.add(el);
                            candidateProfile.put(el, idString);
                        }
                    }
                }
            }
        }
    }
    //collect the managed plugin ids
    Map<String, String> managed = new HashMap<String, String>();
    PluginManagement pm = mavenproject.getPluginManagement();
    if (pm != null) {
        List<Plugin> plgs = pm.getPlugins();
        if (plgs != null) {
            for (Plugin plg : plgs) {
                InputLocation loc = plg.getLocation("version");
                //#350203 skip plugins defined in the superpom
                if (loc != null) {
                    managed.put(plg.getKey(), plg.getVersion());
                }
            }
        }
    }

    //now we have all the candidates, match them against the effective managed set 
    for (Element dep : candidates) {
        String grpString = getTextValue(findChild(dep, PomEdits.GROUP_ID)); //$NON-NLS-1$
        if (grpString == null) {
            grpString = "org.apache.maven.plugins"; //$NON-NLS-1$
        }
        String artString = getTextValue(findChild(dep, PomEdits.ARTIFACT_ID)); //$NON-NLS-1$
        Element version = findChild(dep, PomEdits.VERSION); //$NON-NLS-1$
        String versionString = getTextValue(version);
        if (artString != null && versionString != null) {
            String id = Plugin.constructKey(grpString, artString);
            if (managed.containsKey(id)) {
                String managedVersion = managed.get(id);
                if (version instanceof IndexedRegion) {
                    IndexedRegion off = (IndexedRegion) version;
                    if (lookForIgnoreMarker(document, version, off, IMavenConstants.MARKER_IGNORE_MANAGED)) {
                        continue;
                    }

                    IMarker mark = mavenMarkerManager.addMarker(pomFile, type,
                            NLS.bind(org.eclipse.m2e.core.internal.Messages.MavenMarkerManager_managed_title,
                                    managedVersion, artString),
                            document.getLineOfOffset(off.getStartOffset()) + 1, IMarker.SEVERITY_WARNING);
                    mark.setAttribute(IMavenConstants.MARKER_ATTR_EDITOR_HINT,
                            IMavenConstants.EDITOR_HINT_MANAGED_PLUGIN_OVERRIDE);
                    mark.setAttribute(IMarker.CHAR_START, off.getStartOffset());
                    mark.setAttribute(IMarker.CHAR_END, off.getEndOffset());
                    mark.setAttribute("problemType", "pomhint"); //only imporant in case we enable the generic xml quick fixes //$NON-NLS-1$ //$NON-NLS-2$
                    //add these attributes to easily and deterministicaly find the declaration in question
                    mark.setAttribute("groupId", grpString); //$NON-NLS-1$
                    mark.setAttribute("artifactId", artString); //$NON-NLS-1$
                    String profile = candidateProfile.get(dep);
                    if (profile != null) {
                        mark.setAttribute("profile", profile); //$NON-NLS-1$
                    }
                }
            }
        }
    }
}

From source file:org.eclipse.m2e.profiles.core.internal.management.ProfileManager.java

License:Open Source License

public List<ProfileData> getProfileDatas(IMavenProjectFacade facade, IProgressMonitor monitor)
        throws CoreException {
    if (facade == null) {
        return Collections.emptyList();
    }/*from w  w  w. j a v a 2 s  . c o m*/

    ResolverConfiguration resolverConfiguration = MavenPlugin.getProjectConfigurationManager()
            .getResolverConfiguration(facade.getProject());

    List<String> configuredProfiles = toList(resolverConfiguration.getSelectedProfiles());

    MavenProject mavenProject = facade.getMavenProject(monitor);

    List<Model> modelHierarchy = new ArrayList<>();

    getModelHierarchy(modelHierarchy, mavenProject.getModel(), monitor);

    List<Profile> availableProfiles = collectAvailableProfiles(modelHierarchy, monitor);

    final Map<Profile, Boolean> availableSettingsProfiles = getAvailableSettingsProfiles();

    availableProfiles.addAll(availableSettingsProfiles.keySet());

    List<ProfileData> statuses = new ArrayList<ProfileData>();

    Map<String, List<String>> allActiveProfiles = mavenProject.getInjectedProfileIds();

    for (Profile p : availableProfiles) {
        String pId = p.getId();
        ProfileData status = new ProfileData(pId);
        boolean isDisabled = configuredProfiles.contains("!" + pId);
        if (isActive(pId, allActiveProfiles)) {
            status.setActivationState(ProfileState.Active);
        } else if (isDisabled) {
            status.setActivationState(ProfileState.Disabled);
        }
        boolean isUserSelected = isDisabled || configuredProfiles.contains(pId);

        status.setUserSelected(isUserSelected);

        Boolean isAutoActiveSettingProfile = availableSettingsProfiles.get(p);
        boolean isAutoActive = (isAutoActiveSettingProfile != null && isAutoActiveSettingProfile)
                || (status.getActivationState().isActive() && !isUserSelected);
        status.setAutoActive(isAutoActive);
        status.setSource(findSource(p, modelHierarchy));
        statuses.add(status);
    }

    return Collections.unmodifiableList(statuses);
}

From source file:org.hudsonci.maven.eventspy_30.handler.ProfileLogger.java

License:Open Source License

@SuppressWarnings("unused")
public static void log(final ExecutionEvent event) {
    if (disabled)
        return;/*from w w  w .jav  a  2s.  c o m*/

    for (MavenProject project : event.getSession().getProjects()) {
        log.debug("*** Examining profiles for {}.", project.getName());
        logProfileList(project.getActiveProfiles(), "active");
        logProfileList(project.getModel().getProfiles(), "model");

        //logProfiles( event.getSession().getProjectBuildingRequest().getProfiles(), "ProjectBuildingRequest" );
        logProfileList(project.getProjectBuildingRequest().getProfiles(), "ProjectBuildingRequest");

        log.debug("InjectedProfileIds");
        for (Entry<String, List<String>> entry : project.getInjectedProfileIds().entrySet()) {
            log.debug("  from {} are {}", entry.getKey(), entry.getValue());
        }

        Settings settings = event.getSession().getSettings();
        logSettingsProfileList(settings.getProfiles(), "session-settings");

        log.debug("Collected projects: {}", project.getCollectedProjects());
        log.debug("Project references: {}", project.getProjectReferences());
    }
}

From source file:org.hudsonci.maven.eventspy_30.ProfileCollector.java

License:Open Source License

/**
 * Non-instantiable, use {@link #collect(MavenProject)}.
 *//*from  w ww .  ja v a 2s . c o  m*/
private ProfileCollector(MavenProject project) {
    this.initiatingProject = project;
    this.activeProfiles = getActiveProfileIds(project.getInjectedProfileIds());
    this.container = new ArrayList<ResolvedProfile>();
}