Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle version range in dependencies for target locations #1964

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@

public class OSGiMetadataGenerationTest extends AbstractMavenTargetTest {

@Test
public void testVersionRanges() throws Exception {
ITargetLocation target = resolveMavenTarget(
"""
<location includeDependencyDepth="infinite" includeDependencyScopes="compile,provided,runtime" includeSource="true" label="cucumber" missingManifest="generate" type="Maven">
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.21.1</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
""");
assertStatusOk(getTargetStatus(target));
}

@Test
public void testBadDependencyInChain() throws Exception {
ITargetLocation target = resolveMavenTarget("""
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.m2e.pde.target/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: M2E PDE Integration
Bundle-SymbolicName: org.eclipse.m2e.pde.target;singleton:=true
Bundle-Version: 2.1.1.qualifier
Bundle-Version: 2.1.2.qualifier
Automatic-Module-Name: org.eclipse.m2e.pde.target
Bundle-RequiredExecutionEnvironment: JavaSE-21
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.27.0,4.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -35,6 +36,10 @@
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.eclipse.aether.version.Version;

/**
* Collector to collect (and filter) all transitive dependencies of a maven
Expand Down Expand Up @@ -108,18 +113,47 @@ public DependencyResult collect(MavenRootDependency root) throws RepositoryExcep
while (!queue.isEmpty()) {
ArtifactDescriptor current = queue.poll();
for (Dependency dependency : current.dependencies()) {
if (isValidDependency(dependency) && collected.add(getId(dependency))) {
ArtifactDescriptor dependencyDescriptor = readArtifactDescriptor(dependency, current.node(),
artifacts, nodes);
if (dependencyDescriptor != null) {
queue.add(dependencyDescriptor);
if (isValidDependency(dependency)) {
if (isVersionRanged(dependency)) {
ArtifactDescriptor dependencyDescriptor = resolveHighestVersion(dependency, current.node(),
artifacts, nodes);
if (dependencyDescriptor != null
&& collected.add(getId(dependencyDescriptor.node().getDependency()))) {
queue.add(dependencyDescriptor);
}
}
if (collected.add(getId(dependency))) {
ArtifactDescriptor dependencyDescriptor = readArtifactDescriptor(dependency, current.node(),
artifacts, nodes);
if (dependencyDescriptor != null) {
queue.add(dependencyDescriptor);
}
}
}
}
}
return new DependencyResult(depth, artifacts, rootDescriptor.node(), nodes);
}

private ArtifactDescriptor resolveHighestVersion(Dependency dependency, DependencyNode parent,
Collection<RepositoryArtifact> artifacts, List<DependencyNode> nodes)
throws VersionRangeResolutionException {
Artifact artifact = dependency.getArtifact();
VersionRangeRequest request = new VersionRangeRequest(artifact, repositories, "");
VersionRangeResult result = repoSystem.resolveVersionRange(repositorySession, request);
List<Version> list = result.getVersions().stream().sorted(Comparator.reverseOrder()).toList();
for (Version version : list) {
Artifact setVersion = artifact.setVersion(version.toString());
dependency = dependency.setArtifact(setVersion);
try {
return readArtifactDescriptor(dependency, parent, artifacts, nodes);
} catch (RepositoryException e) {
// we need to try the next version then!
}
}
return null;
}

/**
* This method reads the artifact descriptor and resolves the artifact.
*
Expand Down Expand Up @@ -199,4 +233,9 @@ private static boolean isClassified(MavenRootDependency root) {
return classifier != null && !classifier.isBlank();
}

private static boolean isVersionRanged(Dependency dependency) {
String version = dependency.getArtifact().getVersion();
return version != null && version.startsWith("(") || version.startsWith("[");
}

}
Loading