Search in sources :

Example 1 with CompoundOperation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation in project m2e-core-tests by tesla.

the class ManageDependenciesDialogTest method testSamePOM.

@Test
public void testSamePOM() throws Exception {
    IStructuredModel model = loadModels("projects/same", new String[] { "parent/pom.xml", "child/pom.xml" }).get("child");
    assertNotNull(model);
    IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(TEST_GROUP_ID + ".same", "child", TEST_VERSION);
    MavenProject project = facade.getMavenProject(monitor);
    assertEquals(project.getArtifactId(), "child");
    LinkedList<MavenProject> hierarchy = new LinkedList<>();
    hierarchy.add(project);
    LinkedList<Dependency> dependencies = new LinkedList<>();
    assertNotNull(project.getOriginalModel().getDependencies().get(0));
    dependencies.add(project.getOriginalModel().getDependencies().get(0));
    IDOMModel tempModel = createTempModel(model);
    tempModel.aboutToChangeModel();
    new CompoundOperation(ManageDependenciesDialog.createManageOperation(dependencies), ManageDependenciesDialog.createRemoveVersionOperation(dependencies)).process(tempModel.getDocument());
    tempModel.changedModel();
    // now assert..
    List<Element> deps = findChilds(findChild(tempModel.getDocument().getDocumentElement(), DEPENDENCIES), DEPENDENCY);
    assertEquals(1, deps.size());
    Element dep = deps.get(0);
    assertNotNull(dep);
    assertEquals("test", getTextValue(findChild(dep, PomEdits.GROUP_ID)));
    assertEquals("a", getTextValue(findChild(dep, ARTIFACT_ID)));
    assertNull(findChild(dep, PomEdits.VERSION));
    Element dm = findChild(tempModel.getDocument().getDocumentElement(), DEPENDENCY_MANAGEMENT);
    assertNotNull(dm);
    deps = findChilds(findChild(dm, DEPENDENCIES), DEPENDENCY);
    assertEquals(1, deps.size());
    dep = deps.get(0);
    Element g = findChild(dep, PomEdits.GROUP_ID);
    assertNotNull(g);
    assertEquals("test", getTextValue(g));
    assertEquals("a", getTextValue(findChild(dep, ARTIFACT_ID)));
    assertEquals("1.0", getTextValue(findChild(dep, PomEdits.VERSION)));
}
Also used : MavenProject(org.apache.maven.project.MavenProject) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) Element(org.w3c.dom.Element) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Dependency(org.apache.maven.model.Dependency) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with CompoundOperation

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

the class DependencySetRefactoring method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    List<Operation> operations = new ArrayList<>();
    for (ArtifactKey key : keys) {
        operations.add(new OneDependency(key));
    }
    CompoundOperation compound = new CompoundOperation(operations.toArray(new Operation[0]));
    return PomHelper.createChange(file, compound, getName());
}
Also used : ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) ArrayList(java.util.ArrayList) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation)

Example 3 with CompoundOperation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation 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 4 with CompoundOperation

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation 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 5 with CompoundOperation

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

the class ManageDependenciesDialog method computeResult.

@Override
protected void computeResult() {
    final ParentHierarchyEntry currentPOM = getCurrentPOM();
    final ParentHierarchyEntry targetPOM = getTargetPOM();
    final IFile current = currentPOM.getResource();
    final IFile target = targetPOM.getResource();
    if (target == null || current == null) {
        return;
    }
    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
    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();
}
Also used : ParentHierarchyEntry(org.eclipse.m2e.core.ui.internal.util.ParentHierarchyEntry) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) Dependency(org.apache.maven.model.Dependency) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

CompoundOperation (org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation)7 Dependency (org.apache.maven.model.Dependency)5 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 MavenProject (org.apache.maven.project.MavenProject)3 IMavenProjectFacade (org.eclipse.m2e.core.project.IMavenProjectFacade)3 Operation (org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)3 Test (org.junit.Test)3 Element (org.w3c.dom.Element)3 IFile (org.eclipse.core.resources.IFile)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 ArtifactKey (org.eclipse.m2e.core.embedder.ArtifactKey)2 ParentHierarchyEntry (org.eclipse.m2e.core.ui.internal.util.ParentHierarchyEntry)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 IMarker (org.eclipse.core.resources.IMarker)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1