|
14 | 14 |
|
15 | 15 | import java.util.ArrayList; |
16 | 16 | import java.util.Collection; |
| 17 | +import java.util.Comparator; |
17 | 18 | import java.util.HashSet; |
18 | 19 | import java.util.LinkedList; |
19 | 20 | import java.util.List; |
|
35 | 36 | import org.eclipse.aether.resolution.ArtifactDescriptorResult; |
36 | 37 | import org.eclipse.aether.resolution.ArtifactRequest; |
37 | 38 | 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; |
38 | 43 |
|
39 | 44 | /** |
40 | 45 | * Collector to collect (and filter) all transitive dependencies of a maven |
@@ -108,18 +113,47 @@ public DependencyResult collect(MavenRootDependency root) throws RepositoryExcep |
108 | 113 | while (!queue.isEmpty()) { |
109 | 114 | ArtifactDescriptor current = queue.poll(); |
110 | 115 | 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 | + } |
116 | 131 | } |
117 | 132 | } |
118 | 133 | } |
119 | 134 | } |
120 | 135 | return new DependencyResult(depth, artifacts, rootDescriptor.node(), nodes); |
121 | 136 | } |
122 | 137 |
|
| 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 | + |
123 | 157 | /** |
124 | 158 | * This method reads the artifact descriptor and resolves the artifact. |
125 | 159 | * |
@@ -199,4 +233,9 @@ private static boolean isClassified(MavenRootDependency root) { |
199 | 233 | return classifier != null && !classifier.isBlank(); |
200 | 234 | } |
201 | 235 |
|
| 236 | + private static boolean isVersionRanged(Dependency dependency) { |
| 237 | + String version = dependency.getArtifact().getVersion(); |
| 238 | + return version != null && version.startsWith("(") || version.startsWith("["); |
| 239 | + } |
| 240 | + |
202 | 241 | } |
0 commit comments