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)));
}
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());
}
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()]));
}
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();
}
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();
}
Aggregations