Skip to content

Commit 46c0304

Browse files
committed
Handle version range in dependencies for target locations
Currently if a dependency uses a version range this fails to resolve the target as maven-resolver tries to use the version "as-is" THis now adds a testcase and a fix to resolve to the highest possible version in such case.
1 parent fca5d73 commit 46c0304

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

org.eclipse.m2e.pde.target.tests/src/org/eclipse/m2e/pde/target/tests/OSGiMetadataGenerationTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@
4343

4444
public class OSGiMetadataGenerationTest extends AbstractMavenTargetTest {
4545

46+
@Test
47+
public void testVersionRanges() throws Exception {
48+
ITargetLocation target = resolveMavenTarget(
49+
"""
50+
<location includeDependencyDepth="infinite" includeDependencyScopes="compile,provided,runtime" includeSource="true" label="cucumber" missingManifest="generate" type="Maven">
51+
<dependencies>
52+
<dependency>
53+
<groupId>io.cucumber</groupId>
54+
<artifactId>cucumber-java</artifactId>
55+
<version>7.21.1</version>
56+
<type>jar</type>
57+
</dependency>
58+
</dependencies>
59+
</location>
60+
""");
61+
assertStatusOk(getTargetStatus(target));
62+
}
63+
4664
@Test
4765
public void testBadDependencyInChain() throws Exception {
4866
ITargetLocation target = resolveMavenTarget("""

org.eclipse.m2e.pde.target/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: M2E PDE Integration
44
Bundle-SymbolicName: org.eclipse.m2e.pde.target;singleton:=true
5-
Bundle-Version: 2.1.1.qualifier
5+
Bundle-Version: 2.1.2.qualifier
66
Automatic-Module-Name: org.eclipse.m2e.pde.target
77
Bundle-RequiredExecutionEnvironment: JavaSE-21
88
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.27.0,4.0.0)",

org.eclipse.m2e.pde.target/src/org/eclipse/m2e/pde/target/shared/MavenDependencyCollector.java

+44-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.ArrayList;
1616
import java.util.Collection;
17+
import java.util.Comparator;
1718
import java.util.HashSet;
1819
import java.util.LinkedList;
1920
import java.util.List;
@@ -35,6 +36,10 @@
3536
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
3637
import org.eclipse.aether.resolution.ArtifactRequest;
3738
import org.eclipse.aether.resolution.ArtifactResult;
39+
import org.eclipse.aether.resolution.VersionRangeRequest;
40+
import org.eclipse.aether.resolution.VersionRangeResolutionException;
41+
import org.eclipse.aether.resolution.VersionRangeResult;
42+
import org.eclipse.aether.version.Version;
3843

3944
/**
4045
* Collector to collect (and filter) all transitive dependencies of a maven
@@ -108,18 +113,47 @@ public DependencyResult collect(MavenRootDependency root) throws RepositoryExcep
108113
while (!queue.isEmpty()) {
109114
ArtifactDescriptor current = queue.poll();
110115
for (Dependency dependency : current.dependencies()) {
111-
if (isValidDependency(dependency) && collected.add(getId(dependency))) {
112-
ArtifactDescriptor dependencyDescriptor = readArtifactDescriptor(dependency, current.node(),
113-
artifacts, nodes);
114-
if (dependencyDescriptor != null) {
115-
queue.add(dependencyDescriptor);
116+
if (isValidDependency(dependency)) {
117+
if (isVersionRanged(dependency)) {
118+
ArtifactDescriptor dependencyDescriptor = resolveHighestVersion(dependency, current.node(),
119+
artifacts, nodes);
120+
if (dependencyDescriptor != null
121+
&& collected.add(getId(dependencyDescriptor.node().getDependency()))) {
122+
queue.add(dependencyDescriptor);
123+
}
124+
}
125+
if (collected.add(getId(dependency))) {
126+
ArtifactDescriptor dependencyDescriptor = readArtifactDescriptor(dependency, current.node(),
127+
artifacts, nodes);
128+
if (dependencyDescriptor != null) {
129+
queue.add(dependencyDescriptor);
130+
}
116131
}
117132
}
118133
}
119134
}
120135
return new DependencyResult(depth, artifacts, rootDescriptor.node(), nodes);
121136
}
122137

138+
private ArtifactDescriptor resolveHighestVersion(Dependency dependency, DependencyNode parent,
139+
Collection<RepositoryArtifact> artifacts, List<DependencyNode> nodes)
140+
throws VersionRangeResolutionException {
141+
Artifact artifact = dependency.getArtifact();
142+
VersionRangeRequest request = new VersionRangeRequest(artifact, repositories, "");
143+
VersionRangeResult result = repoSystem.resolveVersionRange(repositorySession, request);
144+
List<Version> list = result.getVersions().stream().sorted(Comparator.reverseOrder()).toList();
145+
for (Version version : list) {
146+
Artifact setVersion = artifact.setVersion(version.toString());
147+
dependency = dependency.setArtifact(setVersion);
148+
try {
149+
return readArtifactDescriptor(dependency, parent, artifacts, nodes);
150+
} catch (RepositoryException e) {
151+
// we need to try the next version then!
152+
}
153+
}
154+
return null;
155+
}
156+
123157
/**
124158
* This method reads the artifact descriptor and resolves the artifact.
125159
*
@@ -199,4 +233,9 @@ private static boolean isClassified(MavenRootDependency root) {
199233
return classifier != null && !classifier.isBlank();
200234
}
201235

236+
private static boolean isVersionRanged(Dependency dependency) {
237+
String version = dependency.getArtifact().getVersion();
238+
return version != null && version.startsWith("(") || version.startsWith("[");
239+
}
240+
202241
}

0 commit comments

Comments
 (0)