Skip to content

Commit

Permalink
Fix #543 - Additional "runtime" scoped dependencies support?
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 8919e56 commit b6d16d2
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 74 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 All @@ -46,16 +46,11 @@ public ClipboardParser(String text) {
for (int i = 0; i < dependencies.getLength(); i++) {
Node item = dependencies.item(i);
if (item instanceof Element) {
Element element = (Element) item;
String groupId = getTextFor("groupId", element, "");
String artifactId = getTextFor("artifactId", element, "");
String version = getTextFor("version", element, "");
String classifier = getTextFor("classifier", element, "");
String type = getTextFor("type", element, MavenTargetLocation.DEFAULT_DEPENDENCY_SCOPE);
this.dependencies
.add(new MavenTargetDependency(groupId, artifactId, version, type, classifier));
parseElement((Element) item);
}

}
if (this.dependencies.isEmpty()) {
parseElement(doc.getDocumentElement());
}
} catch (Exception e) {
// we can't use the clipboard content then...
Expand All @@ -64,6 +59,16 @@ public ClipboardParser(String text) {
}
}

private void parseElement(Element element) {
String groupId = getTextFor("groupId", element, "");
String artifactId = getTextFor("artifactId", element, "");
String version = getTextFor("version", element, "");
String classifier = getTextFor("classifier", element, "");
String type = getTextFor("type", element, Artifact.SCOPE_COMPILE);
this.dependencies
.add(new MavenTargetDependency(groupId, artifactId, version, type, classifier));
}

private String getTextFor(String element, Element doc, String defaultValue) {
NodeList nl = doc.getElementsByTagName(element);
Node item = nl.item(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -59,8 +60,11 @@

public class MavenTargetLocationWizard extends Wizard implements ITargetLocationWizard {

private static final List<String> MAVEN_SCOPES = List.of(Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_IMPORT);

private MavenTargetLocation targetLocation;
private CCombo scope;
private Button[] scopes;
private ComboViewer metadata;
private ComboViewer include;
private ITargetDefinition targetDefinition;
Expand Down Expand Up @@ -92,7 +96,6 @@ public MavenTargetLocationWizard(MavenTargetLocation targetLocation) {
targetLocation == null ? Messages.MavenTargetLocationWizard_1 : Messages.MavenTargetLocationWizard_2) {

private Link editInstructionsButton;
private Label scopeDescriptionLabel;

private Label includeLabel;
private Label scopeLabel;
Expand Down Expand Up @@ -133,7 +136,7 @@ public void modifyText(ModifyEvent e) {
createIncludeCombo(composite);
scopeLabel = new Label(composite, SWT.NONE);
scopeLabel.setText(Messages.MavenTargetLocationWizard_10);
createScopeCombo(composite);
createScopes(composite);
includeSource = createCheckBox(composite, Messages.MavenTargetLocationWizard_8);
createFeature = createCheckBox(composite, Messages.MavenTargetLocationWizard_13);
createFeature.addSelectionListener(new SelectionListener() {
Expand All @@ -149,11 +152,12 @@ public void widgetDefaultSelected(SelectionEvent e) {
}
});
if (targetLocation != null) {
String dependencyScope = targetLocation.getDependencyScope();
if (dependencyScope == null || dependencyScope.isBlank()) {
dependencyScope = Artifact.SCOPE_COMPILE;
Collection<String> dependencyScopes = targetLocation.getDependencyScopes();
for (int i = 0; i < scopes.length; i++) {
if (dependencyScopes.contains(MAVEN_SCOPES.get(i))) {
scopes[i].setSelection(true);
}
}
scope.setText(dependencyScope);
metadata.setSelection(new StructuredSelection(targetLocation.getMetadataMode()));
include.setSelection(new StructuredSelection(targetLocation.getDependencyDepth()));
bndInstructions = targetLocation.getInstructions(null);
Expand Down Expand Up @@ -204,26 +208,30 @@ public String getText(Object element) {
include.addSelectionChangedListener(e -> updateUI());
}

private void createScopeCombo(Composite parent) {
private void createScopes(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.horizontalSpacing = 20;
GridLayout layout = new GridLayout(MAVEN_SCOPES.size() + 1, false);
layout.horizontalSpacing = 10;
layout.marginWidth = 0;
layout.marginHeight = 0;
composite.setLayout(layout);
scope = combo(new CCombo(composite, SWT.READ_ONLY | SWT.BORDER | SWT.FLAT));
scopeDescriptionLabel = new Label(composite, SWT.NONE);
scopeDescriptionLabel.setText(Messages.MavenTargetLocationWizard_15);
scope.add(Artifact.SCOPE_COMPILE);
scope.add(Artifact.SCOPE_PROVIDED);
scope.add(Artifact.SCOPE_TEST);
scope.addModifyListener(new ModifyListener() {
scopes = new Button[MAVEN_SCOPES.size()];
for (int i = 0; i < scopes.length; i++) {
scopes[i] = new Button(composite, SWT.CHECK);
scopes[i].setText(MAVEN_SCOPES.get(i));
scopes[i].addSelectionListener(new SelectionListener() {

@Override
public void widgetSelected(SelectionEvent e) {
updateUI();
}

@Override
public void modifyText(ModifyEvent e) {
updateUI();
}
});
@Override
public void widgetDefaultSelected(SelectionEvent e) {

}
});
}
}

private void createMetadataCombo(Composite parent) {
Expand Down Expand Up @@ -274,13 +282,15 @@ private void updateUI() {
editInstructionsButton.setVisible(
metadata.getStructuredSelection().getFirstElement() == MissingMetadataMode.GENERATE);
if (include.getStructuredSelection().getFirstElement() == DependencyDepth.NONE) {
scopeDescriptionLabel.setVisible(false);
scope.setEnabled(false);
for (Button button : scopes) {
button.setEnabled(false);
}
scopeLabel.setEnabled(false);
} else {
scopeLabel.setEnabled(true);
scope.setEnabled(true);
scopeDescriptionLabel.setVisible(true);
for (Button button : scopes) {
button.setEnabled(true);
}
}
getContainer().updateButtons();
}
Expand Down Expand Up @@ -384,10 +394,16 @@ public boolean performFinish() {
}
@SuppressWarnings("restriction")
IFeature f = createFeature ? featureModel.getFeature() : null;
Collection<String> selectedScopes = new LinkedHashSet<>();
for (int i = 0; i < scopes.length; i++) {
if (scopes[i].getSelection()) {
selectedScopes.add(MAVEN_SCOPES.get(i));
}
}
DependencyDepth depth = (DependencyDepth) include.getStructuredSelection().getFirstElement();
MavenTargetLocation location = new MavenTargetLocation(locationLabel.getText(), dependencyEditor.getRoots(),
repositoryList, (MissingMetadataMode) metadata.getStructuredSelection().getFirstElement(), depth,
scope.getText(), includeSource.getSelection(), list, excludes, f);
selectedScopes, includeSource.getSelection(), list, excludes, f);
if (iscreate) {
targetLocation = location;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ MavenTargetLocationWizard_8=Include Artifact Sources
MavenTargetLocationWizard_12=Artifacts are resolved against the workspace <a href="#maven">maven configuration</a>,\nyou can <a href="#configure">configure</a> additional artifact repositories if required.\n
MavenTargetLocationWizard_14=Label (optional)
MavenTargetLocationWizard_9=Missing OSGi-Manifest
MavenTargetLocationWizard_10=Dependencies scope
MavenTargetLocationWizard_10=Dependencies scopes
MavenTargetLocationWizard_17=Dependencies depth
MavenTargetRepositoryEditor_1=New Repository...
ClipboardParser_1=Clipboard content was ignored: {0}
MavenTargetLocationWizard_15=Specify the scope for included dependencies
MavenTargetLocationWizard_16=Specify the depth for included dependencies
MavenTargetLocationWizard_20=<a>Edit instructions</a>
MavenTargetLocationWizard_21=Edit the default instructions used in case of necessary manifest generation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2021 Christoph Läubrich
* Copyright (c) 2018, 2022 Christoph Läubrich
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.m2e.pde;

import java.util.Collection;
import java.util.List;

import org.apache.maven.RepositoryUtils;
Expand All @@ -37,17 +38,18 @@ final class DependencyNodeGenerator implements ICallable<PreorderNodeListGenerat
private final Artifact artifact;
private final List<ArtifactRepository> repositories;
private final MavenTargetDependency root;
private String dependencyScope;
private Collection<String> dependencyScopes;
private MavenTargetLocation parent;
private DependencyDepth dependencyDepth;

DependencyNodeGenerator(MavenTargetDependency root, Artifact artifact, DependencyDepth dependencyDepth,
String dependencyScope, List<ArtifactRepository> repositories, MavenTargetLocation parent) {
Collection<String> dependencyScopes, List<ArtifactRepository> repositories,
MavenTargetLocation parent) {
this.artifact = artifact;
this.dependencyDepth = dependencyDepth;
this.repositories = repositories;
this.root = root;
this.dependencyScope = dependencyScope;
this.dependencyScopes = dependencyScopes;
this.parent = parent;
}

Expand All @@ -68,7 +70,7 @@ public PreorderNodeListGenerator call(IMavenExecutionContext context, IProgressM
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot(node);
dependencyRequest
.setFilter(new MavenTargetDependencyFilter(dependencyDepth, dependencyScope));
.setFilter(new MavenTargetDependencyFilter(dependencyDepth, dependencyScopes));
repoSystem.resolveDependencies(context.getRepositorySession(), dependencyRequest);
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept(nlg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Christoph Läubrich
* Copyright (c) 2022 Christoph Läubrich
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -18,6 +18,7 @@
import static org.apache.maven.artifact.Artifact.SCOPE_SYSTEM;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;

import java.util.Collection;
import java.util.List;

import org.eclipse.aether.graph.Dependency;
Expand All @@ -27,12 +28,12 @@
public class MavenTargetDependencyFilter implements DependencyFilter {

private static final String[] VALID_EXTENSIONS = { "jar", "pom" };
private String locationScope;
private Collection<String> locationScopes;
private DependencyDepth dependencyDepth;

public MavenTargetDependencyFilter(DependencyDepth dependencyDepth, String scope) {
public MavenTargetDependencyFilter(DependencyDepth dependencyDepth, Collection<String> dependencyScopes) {
this.dependencyDepth = dependencyDepth;
this.locationScope = scope;
this.locationScopes = dependencyScopes;
}

@Override
Expand All @@ -55,18 +56,27 @@ private boolean isValidScope(Dependency dependency) {
if (dependecyScope == null || dependecyScope.isBlank()) {
return true;
}
if (locationScope == null || locationScope.isBlank() || SCOPE_COMPILE.equalsIgnoreCase(locationScope)) {
if (locationScopes.isEmpty()) {
return SCOPE_COMPILE.equalsIgnoreCase(dependecyScope);
}
if (SCOPE_PROVIDED.equalsIgnoreCase(locationScope)) {
return SCOPE_PROVIDED.equalsIgnoreCase(dependecyScope) || SCOPE_COMPILE.equalsIgnoreCase(dependecyScope)
|| SCOPE_SYSTEM.equalsIgnoreCase(dependecyScope) || SCOPE_RUNTIME.equalsIgnoreCase(dependecyScope);
}
if (SCOPE_TEST.equalsIgnoreCase(locationScope)) {
return SCOPE_TEST.equalsIgnoreCase(dependecyScope) || SCOPE_COMPILE.equalsIgnoreCase(dependecyScope)
|| SCOPE_PROVIDED.equalsIgnoreCase(dependecyScope) || SCOPE_SYSTEM.equalsIgnoreCase(dependecyScope)
|| SCOPE_RUNTIME.equalsIgnoreCase(dependecyScope);
for (String locationScope : locationScopes) {
if (dependecyScope.equalsIgnoreCase(locationScope)) {
return true;
}
}
return false;
}

static final Collection<String> expandScope(String scope) {
if (scope == null || scope.isBlank() || SCOPE_COMPILE.equalsIgnoreCase(scope)) {
return List.of(SCOPE_COMPILE);
}
if (SCOPE_PROVIDED.equalsIgnoreCase(scope)) {
return List.of(SCOPE_PROVIDED, SCOPE_COMPILE, SCOPE_SYSTEM, SCOPE_RUNTIME);
}
if (SCOPE_TEST.equalsIgnoreCase(scope)) {
return List.of(SCOPE_TEST, SCOPE_COMPILE, SCOPE_PROVIDED, SCOPE_SYSTEM, SCOPE_RUNTIME);
}
return List.of();
}
}
Loading

0 comments on commit b6d16d2

Please sign in to comment.