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

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

Introduction

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

Prototype

public String getGroupId() 

Source Link

Usage

From source file:org.eclipse.m2e.editor.composites.DependencyLabelProvider.java

License:Open Source License

private String[] findManaged(DependenciesComposite.Dependency dep) {
    if (pomEditor != null) {
        MavenProject mp = pomEditor.getMavenProject();
        String version = null;//from   ww  w .ja v a 2 s  .  c om
        String scope = null;
        if (mp != null) {
            String id = mp.getGroupId() + ":" + mp.getArtifactId() + ":" + mp.getVersion();
            DependencyManagement dm = mp.getDependencyManagement();
            if (dm != null) {
                for (org.apache.maven.model.Dependency d : dm.getDependencies()) {
                    if (d.getGroupId().equals(dep.groupId) && d.getArtifactId().equals(dep.artifactId)) {
                        //based on location, try finding a match in the live Model
                        InputLocation location = d.getLocation("artifactId");
                        if (location != null) {
                            if (id.equals(location.getSource().getModelId())) {
                                version = d.getVersion();
                                scope = d.getScope();
                                break;
                            }
                        }
                        return new String[] { d.getVersion(), d.getScope() };
                    }
                }
            }
        }
        List<org.apache.maven.model.Dependency> dm = valueProvider.getValue();
        for (org.apache.maven.model.Dependency modelDep : dm) {
            String modelGroupId = modelDep.getGroupId();
            String modelArtifactId = modelDep.getArtifactId();
            String modelVersion = modelDep.getVersion();
            String modelScope = modelDep.getScope();
            if (modelGroupId != null && modelGroupId.equals(dep.groupId) && modelArtifactId != null
                    && modelArtifactId.equals(dep.artifactId)) {
                if (version != null && (modelVersion == null || modelVersion.contains("${"))) {
                    //prefer the resolved version to the model one if the model version as expressions..
                    return new String[] { version, modelScope == null ? scope : modelScope };
                }
                return new String[] { modelVersion, modelScope == null ? scope : modelScope };
            }
        }
    }
    return null;
}

From source file:org.eclipse.m2e.editor.dialogs.ManageDependenciesDialog.java

License:Open Source License

protected void computeResult() {
    MavenProject targetPOM = getTargetPOM();
    IMavenProjectFacade targetFacade = MavenPlugin.getMavenProjectRegistry()
            .getMavenProject(targetPOM.getGroupId(), targetPOM.getArtifactId(), targetPOM.getVersion());
    MavenProject currentPOM = projectHierarchy.getFirst();
    IMavenProjectFacade currentFacade = MavenPlugin.getMavenProjectRegistry()
            .getMavenProject(currentPOM.getGroupId(), currentPOM.getArtifactId(), currentPOM.getVersion());

    if (targetFacade == null || currentFacade == null) {
        return;//from   ww  w. j  a va2 s. c  o m
    }
    final boolean same = targetPOM.equals(currentPOM);

    final LinkedList<Dependency> modelDeps = getDependenciesList();

    /*
     * 1) Remove version values from the dependencies from the current POM
     * 2) Add dependencies to dependencyManagement of targetPOM
     */

    //First we remove the version from the original dependency
    final IFile current = currentFacade.getPom();
    final IFile target = targetFacade.getPom();
    Job perform = new Job("Updating POM file(s)") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                if (same) {
                    performOnDOMDocument(new OperationTuple(current, new CompoundOperation(
                            createManageOperation(modelDeps), createRemoveVersionOperation(modelDeps))));
                } else {
                    performOnDOMDocument(new OperationTuple(target, createManageOperation(modelDeps)),
                            new OperationTuple(current, createRemoveVersionOperation(modelDeps)));
                }
            } catch (Exception e) {
                LOG.error("Error updating managed dependencies", e);
                return new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID,
                        "Error updating managed dependencies", e);
            }
            return Status.OK_STATUS;
        }
    };
    perform.setUser(false);
    perform.setSystem(true);
    perform.schedule();
}

From source file:org.eclipse.m2e.editor.dialogs.ManageDependenciesDialog.java

License:Open Source License

protected void checkStatus(MavenProject targetProject, LinkedList<Dependency> selectedDependencies) {
    if (targetProject == null || selectedDependencies.isEmpty()) {
        updateStatus(new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID,
                Messages.ManageDependenciesDialog_emptySelectionError));
        return;//from   www  . j  av a  2 s .  co  m
    }
    boolean error = false;
    IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(
            targetProject.getGroupId(), targetProject.getArtifactId(), targetProject.getVersion());
    if (facade == null) {
        error = true;
        updateStatus(new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID,
                Messages.ManageDependenciesDialog_projectNotPresentError));
    } else {
        org.apache.maven.model.Model model = null;
        if (facade.getMavenProject() == null || facade.getMavenProject().getModel() == null) {
            try {
                model = MavenPlugin.getMavenModelManager().readMavenModel(facade.getPom());
            } catch (CoreException e) {
                Object[] arguments = { facade.getPom(), e.getLocalizedMessage() };
                String message = NLS.bind(Messages.ManageDependenciesDialog_pomReadingError, arguments);
                Status status = new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID, message);
                LOG.info(message, e);
                updateStatus(status);
                error = true;
            }
        } else {
            model = facade.getMavenProject().getModel();
        }
        if (model != null) {
            error = checkDependencies(model, getDependenciesList());
        }
    }

    if (!error) {
        clearStatus();
    }
}

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

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    Composite container = (Composite) super.createDialogArea(parent);

    Label lblLicenseNameFilter = new Label(container, SWT.NONE);
    lblLicenseNameFilter.setText(Messages.SelectSPDXLicenseDialog_lblLicenseNameFilter_text);

    final Text licenseFilter = new Text(container, SWT.BORDER | SWT.SEARCH);
    licenseFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label lblLicenses = new Label(container, SWT.NONE);
    lblLicenses.setText(Messages.SelectSPDXLicenseDialog_lblLicenses_text);

    final TableViewer licensesViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
    Table licensesTable = licensesViewer.getTable();
    licensesTable.addMouseListener(new MouseAdapter() {
        @Override//from www  .  jav a 2  s .  c o m
        public void mouseDoubleClick(MouseEvent e) {
            handleDoubleClick();
        }
    });
    licensesTable.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection selection = licensesViewer.getSelection();
            if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
                license = (SPDXLicense) ((IStructuredSelection) selection).getFirstElement();
            } else {
                license = null;
            }
            updateStatus();
        }
    });
    GridData gd_licensesTable = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd_licensesTable.heightHint = 400;
    licensesTable.setLayoutData(gd_licensesTable);
    licensesViewer.setContentProvider(new IStructuredContentProvider() {
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        public void dispose() {
        }

        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof Collection<?>) {
                return ((Collection<?>) inputElement).toArray();
            }
            return null;
        }
    });
    licensesViewer.setLabelProvider(new ILabelProvider() {
        public void removeListener(ILabelProviderListener listener) {
        }

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public String getText(Object element) {
            if (element instanceof SPDXLicense) {
                return ((SPDXLicense) element).getName();
            }
            return null;
        }

        public Image getImage(Object element) {
            return null;
        }
    });
    licensesViewer.setInput(SPDXLicense.getStandardLicenses());

    licenseFilter.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            String text = licenseFilter.getText();
            ViewerFilter[] filters;
            if (text != null && text.trim().length() > 0) {
                filters = new ViewerFilter[] { new LicenseFilter(text.trim()) };
            } else {
                filters = new ViewerFilter[] {};
            }
            licensesViewer.setFilters(filters);
        }
    });

    Label lblPomxml = new Label(container, SWT.NONE);
    lblPomxml.setText(Messages.SelectSPDXLicenseDialog_lblPomxml_text);

    final PomHierarchyComposite parentComposite = new PomHierarchyComposite(container, SWT.NONE);
    parentComposite.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDoubleClick(MouseEvent e) {
            handleDoubleClick();
        }
    });
    parentComposite.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = parentComposite.getSelection();
            if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
                MavenProject mavenProject = (MavenProject) ((IStructuredSelection) selection).getFirstElement();
                targetProject = MavenPlugin.getMavenProjectRegistry().getMavenProject(mavenProject.getGroupId(),
                        mavenProject.getArtifactId(), mavenProject.getVersion());
                updateStatus();
            }
        }
    });
    parentComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    parentComposite.computeHeirarchy(project, null); // FIXME proper progress monitor
    parentComposite.setSelection(new StructuredSelection(parentComposite.getHierarchy().get(0)));

    return container;
}

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 w  ww. j  av a2  s . c o  m
            }
        }
    }
    //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;/*from   ww  w. j a v a  2  s. c o m*/
    }
    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.refactoring.exclude.ExcludeArtifactRefactoring.java

License:Open Source License

protected IMavenProjectFacade getMavenProjectFacade(MavenProject mavenProject) {
    return MavenPlugin.getMavenProjectRegistry().getMavenProject(mavenProject.getGroupId(),
            mavenProject.getArtifactId(), mavenProject.getVersion());
}

From source file:org.eclipse.m2e.refactoring.exclude.ExcludeWizardPage.java

License:Open Source License

private static String toString(MavenProject project) {
    return NLS.bind("{0}:{1}:{2}", //$NON-NLS-1$
            new String[] { project.getGroupId(), project.getArtifactId(), project.getVersion() });
}

From source file:org.eclipse.m2e.tests.common.WorkspaceHelpers.java

License:Open Source License

public static String getModelId(MavenProject mavenProject) {
    return mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + ":" + mavenProject.getVersion();
}

From source file:org.eclipse.scada.build.helper.AbstractSetQualifierMojo.java

License:Open Source License

private void process(final Collection<MavenProject> projects, final MavenProject project) throws Exception {
    getLog().debug("Processing: " + project + " / " + project.getVersion());

    final String qualifier = getQualifier(project);
    final String version = makeVersion(project, qualifier);

    recordVersion("pom", String.format("%s:%s", project.getGroupId(), project.getArtifactId()), version);

    addChange(project.getFile(), new ModelModifier() {

        @Override/*from   www  . j a v a 2  s  . c  om*/
        public boolean apply(final Model model) {
            if (version != null && !version.equals(project.getVersion())) {
                getLog().info(String.format("Update from %s to %s on project %s", project.getVersion(), version,
                        project));
                model.setVersion(version);
                return true;
            }
            return false;
        }
    });

    addChange(project.getFile(), new ModelModifier() {

        @Override
        public boolean apply(final Model model) {
            boolean change = false;
            final Properties p = model.getProperties();

            getLog().debug("Project Properties: " + p);

            for (final String prop : AbstractSetQualifierMojo.this.qualifierProperties) {
                if (p.containsKey(prop)) {
                    getLog().info(String.format("%s: Setting property - %s -> %s", project, prop, qualifier));
                    p.put(prop, qualifier);
                    change = true;
                }
            }

            for (final Map.Entry<String, String> entry : AbstractSetQualifierMojo.this.additionalProperties
                    .entrySet()) {
                if (p.containsKey(entry.getKey())) {
                    if (entry.getValue() != null) {
                        getLog().info(String.format("%s: Setting property - %s -> %s", project, entry.getKey(),
                                entry.getValue()));
                        p.put(entry.getKey(), entry.getValue());
                        change = true;
                    } else {
                        getLog().info(String.format("%s: Removing property - %s", entry.getKey()));
                        p.remove(entry.getKey());
                        change = true;
                    }
                }
            }

            return change;
        }

        @Override
        public String toString() {
            return String.format("Change properties: " + project);
        };
    });

    addChange(project.getFile(), new ModelModifier() {

        @Override
        public boolean apply(final Model model) {
            boolean changed = false;
            for (final Dependency dep : model.getDependencies()) {
                changed |= syncDep(dep);
            }
            for (final Profile profile : model.getProfiles()) {
                for (final Dependency dep : profile.getDependencies()) {
                    changed |= syncDep(dep);
                }
            }
            return changed;
        }

        @Override
        public String toString() {
            return String.format("Change dependencies: " + project);
        };
    });

    // visit all modules that have this project as a parent
    this.helper.visitModulesWithParent(projects, project, new VisitorChange(this.changeManager) {
        @Override
        protected boolean performChange(final Model model) {
            getLog().debug(String.format("Update parent version in module: %s", model));
            model.getParent().setVersion(version);
            return true;
        }
    });

    checkNonRelativeParent(project);

    syncModule(project, version);
}