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