Skip to content

Commit b682ce5

Browse files
committed
Align implementation with Tycho
1 parent 7596c21 commit b682ce5

File tree

8 files changed

+186
-113
lines changed

8 files changed

+186
-113
lines changed

org.eclipse.m2e.pde.target/src/org/eclipse/m2e/pde/target/DependencyNodeGenerator.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

org.eclipse.m2e.pde.target/src/org/eclipse/m2e/pde/target/MavenTargetLocation.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.maven.RepositoryUtils;
4343
import org.apache.maven.artifact.repository.ArtifactRepository;
4444
import org.apache.maven.project.MavenProject;
45+
import org.eclipse.aether.RepositoryException;
4546
import org.eclipse.aether.RepositorySystem;
4647
import org.eclipse.aether.RepositorySystemSession;
4748
import org.eclipse.aether.artifact.Artifact;
@@ -68,8 +69,12 @@
6869
import org.eclipse.m2e.core.internal.MavenPluginActivator;
6970
import org.eclipse.m2e.core.project.IMavenProjectFacade;
7071
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
72+
import org.eclipse.m2e.pde.target.shared.AdditionalRepository;
7173
import org.eclipse.m2e.pde.target.shared.DependencyDepth;
7274
import org.eclipse.m2e.pde.target.shared.DependencyResult;
75+
import org.eclipse.m2e.pde.target.shared.MavenDependencyCollector;
76+
import org.eclipse.m2e.pde.target.shared.MavenRootDependency;
77+
import org.eclipse.m2e.pde.target.shared.RepositoryArtifact;
7378
import org.eclipse.pde.core.IModel;
7479
import org.eclipse.pde.core.target.ITargetDefinition;
7580
import org.eclipse.pde.core.target.TargetBundle;
@@ -228,7 +233,7 @@ public List<MavenTargetRepository> getExtraRepositories() {
228233

229234
private Artifact resolveDependency(MavenTargetDependency root, IMaven maven, List<ArtifactRepository> repositories,
230235
TargetBundles targetBundles, CacheManager cacheManager, IProgressMonitor monitor) throws CoreException {
231-
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
236+
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
232237
IMavenProjectRegistry registry = MavenPlugin.getMavenProjectRegistry();
233238
IMavenProjectFacade workspaceProject = registry.getMavenProject(root.getGroupId(), root.getArtifactId(),
234239
root.getVersion());
@@ -241,36 +246,33 @@ private Artifact resolveDependency(MavenTargetDependency root, IMaven maven, Lis
241246
root.getVersion(), root.getType(), root.getClassifier(), repositories, subMonitor.split(80)));
242247
}
243248
if (artifact != null) {
244-
DependencyDepth depth = dependencyDepth;
245-
if (isClassified(artifact)) {
246-
// a classified artifact can not have any dependencies and will actually include
247-
// the ones from the main artifact.
248-
// if the user really wants this it is possible to include the pom typed
249-
// artifact or the main artifact in the list
250-
depth = DependencyDepth.NONE;
251-
}
252-
if (isPomType(artifact) && depth == DependencyDepth.NONE) {
253-
// fetching only the pom but no dependencies does not makes much sense...
254-
depth = DependencyDepth.DIRECT;
255-
}
249+
MavenRootDependency dependency = new MavenRootDependency(artifact.getGroupId(), artifact.getArtifactId(),
250+
artifact.getVersion(), artifact.getClassifier(), artifact.getExtension());
251+
DependencyDepth depth = MavenDependencyCollector.getEffectiveDepth(dependency,
252+
dependencyDepth);
256253
SubMonitor split = subMonitor.split(20);
257254
if (depth == DependencyDepth.DIRECT || depth == DependencyDepth.INFINITE) {
258-
ICallable<DependencyResult> callable = DependencyNodeGenerator.create(root, artifact, depth,
259-
dependencyScopes, repositories, this);
255+
// We should resolve the main artifact from the collector, but because this
256+
// is currently separate we already have the additional repos added. see
257+
// org.eclipse.tycho.core.resolver.MavenTargetDefinitionContent.resolveRoot(MavenDependencyCollector,
258+
// MavenDependency)
259+
Collection<AdditionalRepository> extra = new ArrayList<>();
260+
ICallable<DependencyResult> callable = create(root, dependency, dependencyDepth, dependencyScopes,
261+
repositories, this, extra);
260262
DependencyResult dependecies;
261263
if (workspaceProject == null) {
262264
dependecies = maven.createExecutionContext().execute(callable, subMonitor);
263265
} else {
264266
dependecies = registry.execute(workspaceProject, callable, subMonitor);
265267
}
266-
List<Artifact> artifacts = dependecies.artifacts();
268+
List<RepositoryArtifact> artifacts = dependecies.artifacts();
267269
split.setWorkRemaining(artifacts.size());
268-
for (Artifact a : artifacts) {
269-
if (a.getFile() == null) {
270+
for (RepositoryArtifact a : artifacts) {
271+
if (a.artifact().getFile() == null) {
270272
// this is a filtered dependency
271273
continue;
272274
}
273-
addBundleForArtifact(a, cacheManager, maven, targetBundles, split.split(1));
275+
addBundleForArtifact(a.artifact(), cacheManager, maven, targetBundles, split.split(1));
274276
}
275277
targetBundles.dependencyNodes.put(root, dependecies.nodes());
276278
} else {
@@ -281,9 +283,26 @@ private Artifact resolveDependency(MavenTargetDependency root, IMaven maven, Lis
281283
return artifact;
282284
}
283285

284-
private boolean isClassified(Artifact artifact) {
285-
String classifier = artifact.getClassifier();
286-
return classifier != null && !classifier.isEmpty();
286+
static ICallable<DependencyResult> create(MavenTargetDependency root, MavenRootDependency dependency,
287+
DependencyDepth dependencyDepth, Collection<String> dependencyScopes,
288+
@SuppressWarnings("deprecation") List<ArtifactRepository> repositories, MavenTargetLocation parent,
289+
Collection<AdditionalRepository> extra) {
290+
return (context, monitor) -> {
291+
try {
292+
MavenDependencyCollector collector = new MavenDependencyCollector(
293+
MavenPluginActivator.getDefault().getRepositorySystem(), context.getRepositorySession(),
294+
RepositoryUtils.toRepos(repositories), extra, dependencyDepth, dependencyScopes);
295+
DependencyResult result = collector.collect(dependency);
296+
DependencyNode node = result.root();
297+
node.setData(MavenTargetLocation.DEPENDENCYNODE_PARENT, parent);
298+
node.setData(MavenTargetLocation.DEPENDENCYNODE_ROOT, root);
299+
return result;
300+
} catch (RepositoryException e) {
301+
throw new CoreException(Status.error("Resolving dependencies failed", e));
302+
} catch (RuntimeException e) {
303+
throw new CoreException(Status.error("Internal error", e));
304+
}
305+
};
287306
}
288307

289308
private boolean isPomType(Artifact artifact) {

org.eclipse.m2e.pde.target/src/org/eclipse/m2e/pde/target/TargetBundles.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ Optional<MavenTargetBundle> getTargetBundle(MavenTargetDependency dependency) {
6666
}
6767

6868
public void addBundle(Artifact artifact, TargetBundle bundle) {
69-
bundles.put(artifact, bundle);
69+
7070
File file = artifact.getFile();
71-
if (file != null) {
72-
artifacts.put(file, artifact);
71+
if (file == null) {
72+
bundles.put(artifact, bundle);
73+
} else {
74+
if (artifacts.put(file, artifact) == null) {
75+
bundles.put(artifact, bundle);
76+
}
7377
}
7478
}
7579

@@ -84,4 +88,5 @@ public Stream<Entry<Artifact, TargetBundle>> bundles() {
8488
public Optional<Artifact> getArtifact(File file) {
8589
return Optional.ofNullable(artifacts.get(file));
8690
}
91+
8792
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Christoph Läubrich and others
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* https://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Christoph Läubrich - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.m2e.pde.target.shared;
14+
15+
public record AdditionalRepository(String id, String url) {
16+
17+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import java.util.List;
1616

17-
import org.eclipse.aether.artifact.Artifact;
1817
import org.eclipse.aether.graph.DependencyNode;
1918

20-
public record DependencyResult(List<Artifact> artifacts, DependencyNode root, List<DependencyNode> nodes) {
19+
public record DependencyResult(DependencyDepth depth, List<RepositoryArtifact> artifacts, DependencyNode root,
20+
List<DependencyNode> nodes) {
2121

2222
}

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

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
import java.util.List;
2020
import java.util.Queue;
2121
import java.util.Set;
22+
import java.util.stream.Stream;
2223

2324
import org.eclipse.aether.RepositoryException;
2425
import org.eclipse.aether.RepositorySystem;
2526
import org.eclipse.aether.RepositorySystemSession;
2627
import org.eclipse.aether.artifact.Artifact;
28+
import org.eclipse.aether.artifact.DefaultArtifact;
2729
import org.eclipse.aether.graph.DefaultDependencyNode;
2830
import org.eclipse.aether.graph.Dependency;
2931
import org.eclipse.aether.graph.DependencyNode;
3032
import org.eclipse.aether.repository.RemoteRepository;
33+
import org.eclipse.aether.repository.RepositoryPolicy;
3134
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
3235
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
3336
import org.eclipse.aether.resolution.ArtifactRequest;
@@ -39,39 +42,63 @@
3942
*/
4043
public class MavenDependencyCollector {
4144

42-
private static final Set<String> VALID_EXTENSIONS = Set.of("jar", "pom");
45+
private static final String TYPE_POM = "pom";
4346

44-
private RepositorySystem repoSystem;
45-
private RepositorySystemSession repositorySession;
46-
private List<RemoteRepository> repositories;
47-
private DependencyDepth depth;
48-
private Collection<String> dependencyScopes;
47+
private static final Set<String> VALID_EXTENSIONS = Set.of("jar", TYPE_POM);
48+
49+
private final RepositorySystem repoSystem;
50+
private final RepositorySystemSession repositorySession;
51+
private final List<RemoteRepository> repositories;
52+
private final Collection<String> dependencyScopes;
53+
54+
private DependencyDepth dependencyDepth;
4955

5056
public MavenDependencyCollector(RepositorySystem repoSystem, RepositorySystemSession repositorySession,
51-
List<RemoteRepository> repositories, DependencyDepth depth, Collection<String> dependencyScopes) {
57+
List<RemoteRepository> defaultRepositories, Collection<AdditionalRepository> additionalRepositories,
58+
DependencyDepth depth, Collection<String> dependencyScopes) {
5259
this.repoSystem = repoSystem;
5360
this.repositorySession = repositorySession;
54-
this.repositories = repositories;
55-
this.depth = depth;
61+
if (additionalRepositories == null || additionalRepositories.isEmpty()) {
62+
this.repositories = repoSystem.newResolutionRepositories(repositorySession, defaultRepositories);
63+
} else {
64+
List<RemoteRepository> combined = Stream
65+
.concat(defaultRepositories.stream(), additionalRepositories.stream().map(additional -> {
66+
return new RemoteRepository.Builder(additional.id(), "default", additional.url())
67+
.setReleasePolicy(new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_ALWAYS,
68+
RepositoryPolicy.CHECKSUM_POLICY_WARN))
69+
.setSnapshotPolicy(new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_ALWAYS,
70+
RepositoryPolicy.CHECKSUM_POLICY_WARN))
71+
.build();
72+
})).distinct().toList();
73+
this.repositories = repoSystem.newResolutionRepositories(repositorySession, combined);
74+
}
75+
this.dependencyDepth = depth;
5676
this.dependencyScopes = dependencyScopes;
5777
}
5878

59-
public DependencyResult collect(Dependency root) throws RepositoryException {
60-
if (!isValidDependency(root)) {
79+
public List<RemoteRepository> getEffectiveRepositories() {
80+
return repositories;
81+
}
82+
83+
public DependencyResult collect(MavenRootDependency root) throws RepositoryException {
84+
if (!VALID_EXTENSIONS.contains(root.getType())) {
6185
throw new RepositoryException(
62-
"Invalid root dependency: " + root + " allowed extensions are " + VALID_EXTENSIONS);
86+
"Invalid root dependency: " + root + " allowed types are " + VALID_EXTENSIONS);
6387
}
64-
List<Artifact> artifacts = new ArrayList<>();
88+
DependencyDepth depth = getEffectiveDepth(root, dependencyDepth);
89+
List<RepositoryArtifact> artifacts = new ArrayList<>();
6590
List<DependencyNode> nodes = new ArrayList<>();
66-
ArtifactDescriptor rootDescriptor = readArtifactDescriptor(root, null, artifacts, nodes);
91+
ArtifactDescriptor rootDescriptor = readArtifactDescriptor(new Dependency(
92+
new DefaultArtifact(root.groupId(), root.artifactId(), root.classifier(), root.type(), root.version()),
93+
null), null, artifacts, nodes);
6794
if (depth == DependencyDepth.NONE) {
68-
return new DependencyResult(artifacts, rootDescriptor.node(), nodes);
95+
return new DependencyResult(depth, artifacts, rootDescriptor.node(), nodes);
6996
}
7097
if (depth == DependencyDepth.DIRECT) {
7198
for (Dependency dependency : rootDescriptor.dependencies()) {
7299
readArtifactDescriptor(dependency, rootDescriptor.node(), artifacts, nodes);
73100
}
74-
return new DependencyResult(artifacts, rootDescriptor.node(), nodes);
101+
return new DependencyResult(depth, artifacts, rootDescriptor.node(), nodes);
75102
}
76103
// Add all dependencies with BFS method
77104
Set<String> collected = new HashSet<>();
@@ -90,15 +117,7 @@ public DependencyResult collect(Dependency root) throws RepositoryException {
90117
}
91118
}
92119
}
93-
return new DependencyResult(artifacts, rootDescriptor.node(), nodes);
94-
}
95-
96-
private String getId(Dependency dependency) {
97-
Artifact artifact = dependency.getArtifact();
98-
// This does not include the version so we always ever only collect one version
99-
// of an (transitive) artifact
100-
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
101-
+ artifact.getClassifier();
120+
return new DependencyResult(depth, artifacts, rootDescriptor.node(), nodes);
102121
}
103122

104123
/**
@@ -111,8 +130,7 @@ private String getId(Dependency dependency) {
111130
* @throws RepositoryException
112131
*/
113132
private ArtifactDescriptor readArtifactDescriptor(Dependency dependency, DependencyNode parent,
114-
Collection<Artifact> artifacts,
115-
List<DependencyNode> nodes) throws RepositoryException {
133+
Collection<RepositoryArtifact> artifacts, List<DependencyNode> nodes) throws RepositoryException {
116134
if (isValidScope(dependency) && isValidDependency(dependency)) {
117135
ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
118136
descriptorRequest.setArtifact(dependency.getArtifact());
@@ -123,7 +141,7 @@ private ArtifactDescriptor readArtifactDescriptor(Dependency dependency, Depende
123141
artifactRequest.setRepositories(repositories);
124142
ArtifactResult artifactResult = repoSystem.resolveArtifact(repositorySession, artifactRequest);
125143
Artifact resolved = artifactResult.getArtifact();
126-
artifacts.add(resolved);
144+
artifacts.add(new RepositoryArtifact(resolved, artifactResult.getRepository()));
127145
DefaultDependencyNode dependencyNode = new DefaultDependencyNode(
128146
new Dependency(resolved, dependency.getScope()));
129147
nodes.add(dependencyNode);
@@ -153,4 +171,32 @@ private boolean isValidScope(Dependency dependency) {
153171
return dependencyScopes.contains(scope);
154172
}
155173

174+
public static DependencyDepth getEffectiveDepth(MavenRootDependency root, DependencyDepth dependencyDepth) {
175+
DependencyDepth depth;
176+
if (isClassified(root)) {
177+
// a classified artifact can not have any dependencies and will actually include
178+
// the ones from the main artifact.
179+
// if the user really wants this it is possible to include the pom typed
180+
// artifact or the main artifact in the list
181+
depth = DependencyDepth.NONE;
182+
} else if (dependencyDepth == DependencyDepth.NONE && TYPE_POM.equalsIgnoreCase(root.type())) {
183+
depth = DependencyDepth.DIRECT;
184+
} else {
185+
depth = dependencyDepth;
186+
}
187+
return depth;
188+
}
189+
190+
private static String getId(Dependency dependency) {
191+
Artifact artifact = dependency.getArtifact();
192+
// This does not include the version so we always ever only collect one version
193+
// of an (transitive) artifact
194+
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getClassifier();
195+
}
196+
197+
private static boolean isClassified(MavenRootDependency root) {
198+
String classifier = root.classifier();
199+
return classifier != null && !classifier.isBlank();
200+
}
201+
156202
}

0 commit comments

Comments
 (0)