Search in sources :

Example 6 with Operation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation in project m2e-core by eclipse-m2e.

the class AddPluginAction method run.

@Override
public void run(IAction action) {
    IFile file = getPomFileFromPomEditorOrViewSelection();
    if (file == null) {
        return;
    }
    MavenProject mp = null;
    IProject prj = file.getProject();
    if (prj != null && IMavenConstants.POM_FILE_NAME.equals(file.getProjectRelativePath().toString())) {
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(prj);
        if (facade != null) {
            mp = facade.getMavenProject();
        }
    }
    MavenRepositorySearchDialog dialog = MavenRepositorySearchDialog.createSearchPluginDialog(getShell(), Messages.AddPluginAction_searchDialog_title, mp, prj, false);
    if (dialog.open() == Window.OK) {
        final IndexedArtifactFile indexedArtifactFile = (IndexedArtifactFile) dialog.getFirstResult();
        if (indexedArtifactFile != null) {
            try {
                performOnDOMDocument(new OperationTuple(file, (Operation) document -> {
                    Element pluginsEl = getChild(document.getDocumentElement(), BUILD, PLUGINS);
                    PomHelper.createPlugin(pluginsEl, indexedArtifactFile.group, indexedArtifactFile.artifact, indexedArtifactFile.version);
                }));
            } catch (Exception ex) {
                // $NON-NLS-1$
                log.error("Can't add plugin to " + file, ex);
            }
        }
    }
}
Also used : MavenRepositorySearchDialog(org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog) IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) MavenProject(org.apache.maven.project.MavenProject) IndexedArtifactFile(org.eclipse.m2e.core.internal.index.IndexedArtifactFile) Element(org.w3c.dom.Element) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IProject(org.eclipse.core.resources.IProject)

Example 7 with Operation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation in project m2e-core by eclipse-m2e.

the class LifecycleMappingResolution method createOperation.

private Operation createOperation(List<IMarker> markers) {
    List<LifecycleMappingOperation> lst = new ArrayList<>();
    for (IMarker m : markers) {
        // $NON-NLS-1$
        String pluginGroupId = m.getAttribute(IMavenConstants.MARKER_ATTR_GROUP_ID, "");
        // $NON-NLS-1$
        String pluginArtifactId = m.getAttribute(IMavenConstants.MARKER_ATTR_ARTIFACT_ID, "");
        // $NON-NLS-1$
        String pluginVersion = m.getAttribute(IMavenConstants.MARKER_ATTR_VERSION, "");
        // $NON-NLS-1$
        String[] goals = new String[] { m.getAttribute(IMavenConstants.MARKER_ATTR_GOAL, "") };
        lst.add(new LifecycleMappingOperation(pluginGroupId, pluginArtifactId, pluginVersion, action, goals));
    }
    return new CompoundOperation(lst.toArray(new Operation[lst.size()]));
}
Also used : LifecycleMappingOperation(org.eclipse.m2e.core.ui.internal.editing.LifecycleMappingOperation) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) ArrayList(java.util.ArrayList) IMarker(org.eclipse.core.resources.IMarker) LifecycleMappingOperation(org.eclipse.m2e.core.ui.internal.editing.LifecycleMappingOperation) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation)

Example 8 with Operation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation in project m2e-core by eclipse-m2e.

the class ExcludeArtifactRefactoring method checkFinalConditions0.

RefactoringStatus checkFinalConditions0(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (hierarchy == null || exclusionPoint == null) {
        return RefactoringStatus.createFatalErrorStatus(Messages.ExcludeArtifactRefactoring_unableToLocateProject);
    }
    changes = new ArrayList<>();
    Set<ArtifactKey> locatedKeys = new HashSet<>();
    List<IStatus> statuses = new ArrayList<>();
    SubMonitor monitor = SubMonitor.convert(pm, 3);
    List<Operation> exclusionOp = new ArrayList<>();
    // Exclusion point
    for (Entry<Dependency, Set<ArtifactKey>> entry : getDependencyExcludes(exclusionPoint, monitor.newChild(1)).entrySet()) {
        locatedKeys.addAll(entry.getValue());
        Dependency dependency = entry.getKey();
        if (contains(entry.getValue(), dependency)) {
            exclusionOp.add(new RemoveDependencyOperation(dependency));
        } else {
            for (ArtifactKey key : entry.getValue()) {
                if (!hasExclusion(exclusionPoint, dependency, key)) {
                    exclusionOp.add(new AddExclusionOperation(dependency, key));
                }
            }
        }
    }
    // Below exclusion point - pull up dependency to exclusion point
    for (ParentHierarchyEntry project : getWorkspaceDescendants()) {
        List<Operation> operations = new ArrayList<>();
        for (Entry<Dependency, Set<ArtifactKey>> entry : getDependencyExcludes(project, monitor.newChild(1)).entrySet()) {
            locatedKeys.addAll(entry.getValue());
            Dependency dependency = entry.getKey();
            operations.add(new RemoveDependencyOperation(dependency));
            if (!contains(entry.getValue(), dependency)) {
                if (!hasDependency(exclusionPoint, dependency)) {
                    exclusionOp.add(new AddDependencyOperation(dependency));
                }
                for (ArtifactKey key : entry.getValue()) {
                    if (!hasExclusion(exclusionPoint, dependency, key)) {
                        exclusionOp.add(new AddExclusionOperation(dependency, key));
                    }
                }
            }
        }
        if (operations.size() > 0) {
            IFile pom = project.getResource();
            changes.add(PomHelper.createChange(pom, new CompoundOperation(operations.toArray(new Operation[operations.size()])), getName(pom)));
        }
    }
    // Above exclusion - Add dep to exclusionPoint
    for (ParentHierarchyEntry project : getWorkspaceAncestors()) {
        for (Entry<Dependency, Set<ArtifactKey>> entry : getDependencyExcludes(project, monitor.newChild(1)).entrySet()) {
            locatedKeys.addAll(entry.getValue());
            Dependency dependency = entry.getKey();
            if (contains(entry.getValue(), dependency)) {
                IFile pom = project.getResource();
                if (pom != null) {
                    statuses.add(new Status(IStatus.INFO, PLUGIN_ID, NLS.bind(Messages.ExcludeArtifactRefactoring_removeDependencyFrom, toString(dependency), pom.getFullPath())));
                    changes.add(PomHelper.createChange(pom, new RemoveDependencyOperation(dependency), getName(pom)));
                }
            } else {
                exclusionOp.add(new AddDependencyOperation(dependency));
                for (ArtifactKey key : entry.getValue()) {
                    if (!hasExclusion(exclusionPoint, dependency, key)) {
                        exclusionOp.add(new AddExclusionOperation(dependency, key));
                    }
                }
            }
        }
    }
    if (!exclusionOp.isEmpty()) {
        IFile pom = exclusionPoint.getResource();
        changes.add(PomHelper.createChange(pom, new CompoundOperation(exclusionOp.toArray(new Operation[exclusionOp.size()])), getName(pom)));
    }
    if (statuses.size() == 1) {
        return RefactoringStatus.create(statuses.get(0));
    } else if (statuses.size() > 1) {
        return RefactoringStatus.create(new MultiStatus(PLUGIN_ID, 0, statuses.toArray(new IStatus[statuses.size()]), Messages.ExcludeArtifactRefactoring_errorCreatingRefactoring, null));
    } else if (locatedKeys.isEmpty()) {
        return RefactoringStatus.createFatalErrorStatus(Messages.ExcludeArtifactRefactoring_noTargets);
    } else if (locatedKeys.size() != excludes.length) {
        StringBuilder sb = new StringBuilder();
        for (ArtifactKey key : excludes) {
            if (!locatedKeys.contains(key)) {
                sb.append(key.toString()).append(',');
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        return RefactoringStatus.createErrorStatus(NLS.bind(Messages.ExcludeArtifactRefactoring_failedToLocateArtifact, sb.toString()));
    }
    return new RefactoringStatus();
}
Also used : ParentHierarchyEntry(org.eclipse.m2e.core.ui.internal.util.ParentHierarchyEntry) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) HashSet(java.util.HashSet) Set(java.util.Set) IFile(org.eclipse.core.resources.IFile) AddDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.AddDependencyOperation) RemoveDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.RemoveDependencyOperation) ArrayList(java.util.ArrayList) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) AddDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.AddDependencyOperation) RemoveDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.RemoveDependencyOperation) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) Dependency(org.apache.maven.model.Dependency) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) HashSet(java.util.HashSet)

Example 9 with Operation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation in project m2e-core by eclipse-m2e.

the class MavenModuleWizard method performFinish.

/**
 * Performs the "finish" action.
 */
@Override
public boolean performFinish() {
    // First of all, we extract all the information from the wizard pages.
    // Note that this should not be done inside the operation we will run
    // since many of the wizard pages' methods can only be invoked from within
    // the SWT event dispatcher thread. However, the operation spawns a new
    // separate thread to perform the actual work, i.e. accessing SWT elements
    // from within that thread would lead to an exception.
    final String moduleName = parentPage.getModuleName();
    // Get the location where to create the project. For some reason, when using
    // the default workspace location for a project, we have to pass null
    // instead of the actual location.
    final IPath location = parentPage.getParentContainer().getLocation();
    final IFile parentPom = parentPage.getPom();
    Job job;
    if (parentPage.isSimpleProject()) {
        final Model model = artifactPage.getModel();
        if (model.getParent() != null) {
            Parent par = model.getParent();
            String relPath = location.makeRelativeTo(location.append(moduleName)).toOSString();
            if (!"..".equals(relPath)) {
                // $NON-NLS-1$
                par.setRelativePath(relPath);
            }
            // #335331 remove current model's version and groupId if equal to parent, to prevent showing a warning marker
            if (par.getGroupId() != null && par.getGroupId().equals(model.getGroupId())) {
                model.setGroupId(null);
            }
            if (par.getVersion() != null && par.getVersion().equals(model.getVersion())) {
                model.setVersion(null);
            }
        }
        final String[] folders = artifactPage.getFolders();
        job = new AbstractCreateMavenProjectJob(NLS.bind(Messages.wizardProjectJobCreatingProject, moduleName)) {

            @Override
            protected List<IProject> doCreateMavenProjects(IProgressMonitor monitor) throws CoreException {
                setProperty(IProgressConstants.ACTION_PROPERTY, new OpenMavenConsoleAction());
                String projectName = ProjectConfigurationManager.getProjectName(importConfiguration, model);
                IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                // XXX respect parent's setting for separate projects for modules
                // XXX should run update sources on parent instead of creating new module project
                MavenPlugin.getProjectConfigurationManager().createSimpleProject(project, location.append(moduleName), model, folders, importConfiguration, new MavenProjectWorkspaceAssigner(workingSets), monitor);
                setModule(projectName);
                return Arrays.asList(project);
            }
        };
    } else {
        Model model = parametersPage.getModel();
        final Archetype archetype = archetypePage.getArchetype();
        final String groupId = model.getGroupId();
        final String artifactId = model.getArtifactId();
        final String version = model.getVersion();
        final String javaPackage = parametersPage.getJavaPackage();
        final Properties properties = parametersPage.getProperties();
        job = new AbstractCreateMavenProjectJob(NLS.bind(Messages.wizardProjectJobCreating, archetype.getArtifactId())) {

            @Override
            protected List<IProject> doCreateMavenProjects(IProgressMonitor monitor) throws CoreException {
                List<IProject> projects = M2EUIPluginActivator.getDefault().getArchetypeManager().getGenerator().createArchetypeProjects(location, // 
                new MavenArchetype(archetype), // 
                groupId, // 
                artifactId, // 
                version, // 
                javaPackage, properties, importConfiguration, new MavenProjectWorkspaceAssigner(workingSets), monitor);
                setModule(moduleName);
                return projects;
            }
        };
    }
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            final IStatus result = event.getResult();
            if (result.isOK()) {
                if (!isEditor) {
                    // add the <module> element to the parent pom
                    try {
                        performOnDOMDocument(new OperationTuple(parentPom, (Operation) document -> {
                            Element root = document.getDocumentElement();
                            Element modules = getChild(root, "modules");
                            if (findChild(modules, "module", textEquals(moduleName)) == null) {
                                format(createElementWithText(modules, "module", moduleName));
                            }
                        }));
                    } catch (Exception e) {
                        // $NON-NLS-1$
                        LOG.error("Cannot add module to parent POM", e);
                    }
                }
            } else {
                Display.getDefault().asyncExec(() -> // 
                MessageDialog.openError(// 
                getShell(), // 
                NLS.bind(Messages.wizardProjectJobFailed, moduleName), result.getMessage()));
            }
        }
    });
    job.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
    job.schedule();
    if (isEditor) {
        try {
            job.join();
        } catch (InterruptedException ex) {
        // ignore
        }
    }
    return true;
}
Also used : Arrays(java.util.Arrays) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) LoggerFactory(org.slf4j.LoggerFactory) CoreException(org.eclipse.core.runtime.CoreException) Parent(org.apache.maven.model.Parent) PomEdits.getChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild) IStatus(org.eclipse.core.runtime.IStatus) OpenMavenConsoleAction(org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction) PomEdits.performOnDOMDocument(org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument) IProject(org.eclipse.core.resources.IProject) IPath(org.eclipse.core.runtime.IPath) Composite(org.eclipse.swt.widgets.Composite) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) PomEdits.findChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild) IFile(org.eclipse.core.resources.IFile) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) PomEdits.format(org.eclipse.m2e.core.ui.internal.editing.PomEdits.format) Properties(java.util.Properties) Logger(org.slf4j.Logger) Archetype(org.apache.maven.archetype.catalog.Archetype) Job(org.eclipse.core.runtime.jobs.Job) NLS(org.eclipse.osgi.util.NLS) ProjectConfigurationManager(org.eclipse.m2e.core.internal.project.ProjectConfigurationManager) Display(org.eclipse.swt.widgets.Display) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) List(java.util.List) Element(org.w3c.dom.Element) PomEdits.textEquals(org.eclipse.m2e.core.ui.internal.editing.PomEdits.textEquals) PomEdits.createElementWithText(org.eclipse.m2e.core.ui.internal.editing.PomEdits.createElementWithText) M2EUIPluginActivator(org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator) MavenImages(org.eclipse.m2e.core.ui.internal.MavenImages) INewWizard(org.eclipse.ui.INewWizard) Messages(org.eclipse.m2e.core.ui.internal.Messages) MavenPlugin(org.eclipse.m2e.core.MavenPlugin) IProgressConstants(org.eclipse.ui.progress.IProgressConstants) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) SelectionListener(org.eclipse.swt.events.SelectionListener) Model(org.apache.maven.model.Model) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) Parent(org.apache.maven.model.Parent) Archetype(org.apache.maven.archetype.catalog.Archetype) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) Element(org.w3c.dom.Element) Properties(java.util.Properties) List(java.util.List) Job(org.eclipse.core.runtime.jobs.Job) IPath(org.eclipse.core.runtime.IPath) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) OpenMavenConsoleAction(org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction) MavenArchetype(org.eclipse.m2e.core.ui.internal.archetype.MavenArchetype) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Model(org.apache.maven.model.Model)

Aggregations

Operation (org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation)9 OperationTuple (org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple)6 IFile (org.eclipse.core.resources.IFile)5 Element (org.w3c.dom.Element)5 ArrayList (java.util.ArrayList)4 IProject (org.eclipse.core.resources.IProject)3 IStatus (org.eclipse.core.runtime.IStatus)3 ArtifactKey (org.eclipse.m2e.core.embedder.ArtifactKey)3 IndexedArtifactFile (org.eclipse.m2e.core.internal.index.IndexedArtifactFile)3 IMavenProjectFacade (org.eclipse.m2e.core.project.IMavenProjectFacade)3 MavenRepositorySearchDialog (org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog)3 Document (org.w3c.dom.Document)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Dependency (org.apache.maven.model.Dependency)2 MavenProject (org.apache.maven.project.MavenProject)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2