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