1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.inject.Singleton;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.ArtifactUtils;
32 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
33 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
34 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
35 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
36 import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
37 import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
38 import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
39 import org.apache.maven.execution.MavenSession;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.project.artifact.ProjectArtifact;
42 import org.apache.maven.repository.RepositorySystem;
43
44
45
46
47
48
49 @Deprecated
50 @Named
51 @Singleton
52 public class DefaultProjectDependenciesResolver implements ProjectDependenciesResolver {
53
54 private final RepositorySystem repositorySystem;
55
56 private final ResolutionErrorHandler resolutionErrorHandler;
57
58 @Inject
59 public DefaultProjectDependenciesResolver(
60 RepositorySystem repositorySystem, ResolutionErrorHandler resolutionErrorHandler) {
61 this.repositorySystem = repositorySystem;
62 this.resolutionErrorHandler = resolutionErrorHandler;
63 }
64
65 public Set<Artifact> resolve(MavenProject project, Collection<String> scopesToResolve, MavenSession session)
66 throws ArtifactResolutionException, ArtifactNotFoundException {
67 return resolve(Collections.singleton(project), scopesToResolve, session);
68 }
69
70 public Set<Artifact> resolve(
71 MavenProject project,
72 Collection<String> scopesToCollect,
73 Collection<String> scopesToResolve,
74 MavenSession session)
75 throws ArtifactResolutionException, ArtifactNotFoundException {
76 Set<MavenProject> mavenProjects = Collections.singleton(project);
77 return resolveImpl(
78 mavenProjects, scopesToCollect, scopesToResolve, session, getIgnorableArtifacts(mavenProjects));
79 }
80
81 public Set<Artifact> resolve(
82 Collection<? extends MavenProject> projects, Collection<String> scopesToResolve, MavenSession session)
83 throws ArtifactResolutionException, ArtifactNotFoundException {
84 return resolveImpl(projects, null, scopesToResolve, session, getIgnorableArtifacts(projects));
85 }
86
87 public Set<Artifact> resolve(
88 MavenProject project,
89 Collection<String> scopesToCollect,
90 Collection<String> scopesToResolve,
91 MavenSession session,
92 Set<Artifact> ignorableArtifacts)
93 throws ArtifactResolutionException, ArtifactNotFoundException {
94 return resolveImpl(
95 Collections.singleton(project),
96 scopesToCollect,
97 scopesToResolve,
98 session,
99 getIgnorableArtifacts(ignorableArtifacts));
100 }
101
102 private Set<Artifact> resolveImpl(
103 Collection<? extends MavenProject> projects,
104 Collection<String> scopesToCollect,
105 Collection<String> scopesToResolve,
106 MavenSession session,
107 Set<String> projectIds)
108 throws ArtifactResolutionException, ArtifactNotFoundException {
109 Set<Artifact> resolved = new LinkedHashSet<>();
110
111 if (projects == null || projects.isEmpty()) {
112 return resolved;
113 }
114
115 if ((scopesToCollect == null || scopesToCollect.isEmpty())
116 && (scopesToResolve == null || scopesToResolve.isEmpty())) {
117 return resolved;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter(scopesToResolve);
153
154 CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter(scopesToCollect);
155 collectionScopeFilter = new CumulativeScopeArtifactFilter(collectionScopeFilter, resolutionScopeFilter);
156
157 ArtifactResolutionRequest request = new ArtifactResolutionRequest()
158 .setResolveRoot(false)
159 .setResolveTransitively(true)
160 .setCollectionFilter(collectionScopeFilter)
161 .setResolutionFilter(resolutionScopeFilter)
162 .setLocalRepository(session.getLocalRepository())
163 .setOffline(session.isOffline())
164 .setForceUpdate(session.getRequest().isUpdateSnapshots());
165 request.setServers(session.getRequest().getServers());
166 request.setMirrors(session.getRequest().getMirrors());
167 request.setProxies(session.getRequest().getProxies());
168
169 for (MavenProject project : projects) {
170 request.setArtifact(new ProjectArtifact(project));
171 request.setArtifactDependencies(project.getDependencyArtifacts());
172 request.setManagedVersionMap(project.getManagedVersionMap());
173 request.setRemoteRepositories(project.getRemoteArtifactRepositories());
174
175 ArtifactResolutionResult result = repositorySystem.resolve(request);
176
177 try {
178 resolutionErrorHandler.throwErrors(request, result);
179 } catch (MultipleArtifactsNotFoundException e) {
180
181 Collection<Artifact> missing = new HashSet<>(e.getMissingArtifacts());
182
183 for (Iterator<Artifact> it = missing.iterator(); it.hasNext(); ) {
184 String key = ArtifactUtils.key(it.next());
185 if (projectIds.contains(key)) {
186 it.remove();
187 }
188 }
189
190 if (!missing.isEmpty()) {
191 throw e;
192 }
193 }
194
195 resolved.addAll(result.getArtifacts());
196 }
197
198 return resolved;
199 }
200
201 private Set<String> getIgnorableArtifacts(Collection<? extends MavenProject> projects) {
202 Set<String> projectIds = new HashSet<>(projects.size() * 2);
203
204 for (MavenProject p : projects) {
205 String key = ArtifactUtils.key(p.getGroupId(), p.getArtifactId(), p.getVersion());
206 projectIds.add(key);
207 }
208 return projectIds;
209 }
210
211 private Set<String> getIgnorableArtifacts(Iterable<Artifact> artifactIterable) {
212 Set<String> projectIds = new HashSet<>();
213
214 for (Artifact artifact : artifactIterable) {
215 String key = ArtifactUtils.key(artifact);
216 projectIds.add(key);
217 }
218 return projectIds;
219 }
220 }