Skip to content

Commit

Permalink
Fix eclipse-m2e#517 - support removing of child location roots
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Läubrich <[email protected]>
  • Loading branch information
laeubi committed Feb 4, 2022
1 parent b6d16d2 commit beaac63
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.maven.artifact.Artifact;
import org.eclipse.m2e.pde.MavenTargetDependency;
import org.eclipse.m2e.pde.MavenTargetLocation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -64,7 +64,7 @@ private void parseElement(Element element) {
String artifactId = getTextFor("artifactId", element, "");
String version = getTextFor("version", element, "");
String classifier = getTextFor("classifier", element, "");
String type = getTextFor("type", element, Artifact.SCOPE_COMPILE);
String type = getTextFor("type", element, MavenTargetLocation.DEFAULT_PACKAGE_TYPE);
this.dependencies
.add(new MavenTargetDependency(groupId, artifactId, version, type, classifier));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,41 @@ public boolean canEnable(ITargetDefinition target, TreePath treePath) {
return false;
}

@Override
public boolean canRemove(ITargetDefinition target, TreePath treePath) {
Object segment = treePath.getFirstSegment();
if (segment instanceof MavenTargetLocation) {
Object lastSegment = treePath.getLastSegment();
if (lastSegment instanceof MavenTargetDependency) {
return true;
}
}
return false;
}

@Override
public IStatus remove(ITargetDefinition target, TreePath[] treePaths) {
ITargetLocation[] targetLocations = target.getTargetLocations();
for (TreePath treePath : treePaths) {
Object segment = treePath.getFirstSegment();
if (segment instanceof MavenTargetLocation) {
MavenTargetLocation mavenTargetLocation = (MavenTargetLocation) segment;
Object lastSegment = treePath.getLastSegment();
if (lastSegment instanceof MavenTargetDependency) {
MavenTargetDependency mavenTargetDependency = (MavenTargetDependency) lastSegment;
for (int i = 0; i < targetLocations.length; i++) {
ITargetLocation location = targetLocations[i];
if (location == mavenTargetLocation) {
targetLocations[i] = mavenTargetLocation.withoutRoot(mavenTargetDependency);
}
}
}
}
}
target.setTargetLocations(targetLocations);
return Status.OK_STATUS;
}

@Override
public IStatus toggle(ITargetDefinition target, TreePath[] treePaths) {
int toggled = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public class MavenTargetLocation extends AbstractBundleContainer {
public static final String ATTRIBUTE_INSTRUCTIONS_REFERENCE = "reference";

public static final String ATTRIBUTE_DEPENDENCY_DEPTH = "includeDependencyDepth";
@Deprecated
public static final String ATTRIBUTE_DEPENDENCY_SCOPE = "includeDependencyScope";

public static final String ATTRIBUTE_DEPENDENCY_SCOPES = "includeDependencyScopes";
public static final String ATTRIBUTE_INCLUDE_SOURCE = "includeSource";
public static final String ATTRIBUTE_MISSING_META_DATA = "missingManifest";
Expand Down Expand Up @@ -373,10 +372,6 @@ public List<MavenTargetDependency> getRoots() {
return roots;
}

public MavenTargetLocation withInstructions(Collection<BNDInstructions> instructions) {
return new MavenTargetLocation(label, roots, extraRepositories, metadataMode, dependencyDepth, dependencyScopes,
includeSource, instructions, excludedArtifacts, featureTemplate);
}

public IFeature getFeatureTemplate() {
return featureTemplate;
Expand Down Expand Up @@ -613,4 +608,17 @@ public Collection<BNDInstructions> getInstructions() {
return Collections.unmodifiableCollection(instructionsMap.values());
}

public MavenTargetLocation withInstructions(Collection<BNDInstructions> instructions) {
return new MavenTargetLocation(label, roots.stream().map(MavenTargetDependency::copy).collect(Collectors.toList()), extraRepositories, metadataMode, dependencyDepth, dependencyScopes,
includeSource, instructions, excludedArtifacts, featureTemplate);
}

public MavenTargetLocation withoutRoot(MavenTargetDependency toRemove) {
return new MavenTargetLocation(label,
roots.stream().filter(root -> root != toRemove).map(root -> root.copy()).collect(Collectors.toList()),
extraRepositories,
metadataMode, dependencyDepth, dependencyScopes, includeSource, instructionsMap.values(),
excludedArtifacts, featureTemplate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

@SuppressWarnings("restriction")
public class MavenTargetLocationFactory implements ITargetLocationFactory {
// For backward compat
private static final String ATTRIBUTE_DEPENDENCY_SCOPE = "includeDependencyScope";

@Override
public ITargetLocation getTargetLocation(String type, String serializedXML) throws CoreException {
Expand All @@ -52,8 +54,7 @@ public ITargetLocation getTargetLocation(String type, String serializedXML) thro
// fall back to safe default
mode = MissingMetadataMode.ERROR;
}
@SuppressWarnings("deprecation")
String dependencyScope = location.getAttribute(MavenTargetLocation.ATTRIBUTE_DEPENDENCY_SCOPE);
String dependencyScope = location.getAttribute(ATTRIBUTE_DEPENDENCY_SCOPE);
List<MavenTargetDependency> dependencies = new ArrayList<>();
List<MavenTargetRepository> repositories = new ArrayList<>();

Expand Down

0 comments on commit beaac63

Please sign in to comment.