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 @Deprecated
46 @Component( role = ProjectDependenciesResolver.class )
47 public class DefaultProjectDependenciesResolver
48 implements ProjectDependenciesResolver
49 {
50
51 @Requirement
52 private RepositorySystem repositorySystem;
53
54 @Requirement
55 private ResolutionErrorHandler resolutionErrorHandler;
56
57 public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session )
58 throws ArtifactResolutionException, ArtifactNotFoundException
59 {
60 return resolve( Collections.singleton( project ), scopesToResolve, session );
61 }
62
63 public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
64 Collection<String> scopesToResolve, MavenSession session )
65 throws ArtifactResolutionException, ArtifactNotFoundException
66 {
67 Set<MavenProject> mavenProjects = Collections.singleton( project );
68 return resolveImpl( mavenProjects, scopesToCollect, scopesToResolve, session,
69 getIgnorableArtifacts( mavenProjects ) );
70 }
71
72 public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToResolve,
73 MavenSession session )
74 throws ArtifactResolutionException, ArtifactNotFoundException
75 {
76 return resolveImpl( projects, null, scopesToResolve, session, getIgnorableArtifacts( projects ) );
77 }
78
79 public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
80 Collection<String> scopesToResolve, MavenSession session,
81 Set<Artifact> ignoreableArtifacts )
82 throws ArtifactResolutionException, ArtifactNotFoundException
83 {
84 return resolveImpl( Collections.singleton( project ), scopesToCollect, scopesToResolve, session,
85 getIgnorableArtifacts( ignoreableArtifacts ) );
86 }
87
88
89 private Set<Artifact> resolveImpl( Collection<? extends MavenProject> projects, Collection<String> scopesToCollect,
90 Collection<String> scopesToResolve, MavenSession session,
91 Set<String> projectIds )
92 throws ArtifactResolutionException, ArtifactNotFoundException
93 {
94 Set<Artifact> resolved = new LinkedHashSet<>();
95
96 if ( projects == null || projects.isEmpty() )
97 {
98 return resolved;
99 }
100
101 if ( ( scopesToCollect == null || scopesToCollect.isEmpty() )
102 && ( scopesToResolve == null || scopesToResolve.isEmpty() ) )
103 {
104 return resolved;
105 }
106
107
108
109
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 CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter( scopesToResolve );
140
141 CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter( scopesToCollect );
142 collectionScopeFilter = new CumulativeScopeArtifactFilter( collectionScopeFilter, resolutionScopeFilter );
143
144 ArtifactResolutionRequest request =
145 new ArtifactResolutionRequest().setResolveRoot( false ).setResolveTransitively( true ).setCollectionFilter(
146 collectionScopeFilter ).setResolutionFilter( resolutionScopeFilter ).setLocalRepository(
147 session.getLocalRepository() ).setOffline( session.isOffline() ).setForceUpdate(
148 session.getRequest().isUpdateSnapshots() );
149 request.setServers( session.getRequest().getServers() );
150 request.setMirrors( session.getRequest().getMirrors() );
151 request.setProxies( session.getRequest().getProxies() );
152
153 for ( MavenProject project : projects )
154 {
155 request.setArtifact( new ProjectArtifact( project ) );
156 request.setArtifactDependencies( project.getDependencyArtifacts() );
157 request.setManagedVersionMap( project.getManagedVersionMap() );
158 request.setRemoteRepositories( project.getRemoteArtifactRepositories() );
159
160 ArtifactResolutionResult result = repositorySystem.resolve( request );
161
162 try
163 {
164 resolutionErrorHandler.throwErrors( request, result );
165 }
166 catch ( MultipleArtifactsNotFoundException e )
167 {
168
169 Collection<Artifact> missing = new HashSet<>( e.getMissingArtifacts() );
170
171 for ( Iterator<Artifact> it = missing.iterator(); it.hasNext(); )
172 {
173 String key = ArtifactUtils.key( it.next() );
174 if ( projectIds.contains( key ) )
175 {
176 it.remove();
177 }
178 }
179
180 if ( !missing.isEmpty() )
181 {
182 throw e;
183 }
184 }
185
186 resolved.addAll( result.getArtifacts() );
187 }
188
189 return resolved;
190 }
191
192
193 private Set<String> getIgnorableArtifacts( Collection<? extends MavenProject> projects )
194 {
195 Set<String> projectIds = new HashSet<>( projects.size() * 2 );
196
197 for ( MavenProject p : projects )
198 {
199 String key = ArtifactUtils.key( p.getGroupId(), p.getArtifactId(), p.getVersion() );
200 projectIds.add( key );
201 }
202 return projectIds;
203 }
204
205 private Set<String> getIgnorableArtifacts( Iterable<Artifact> artifactIterable )
206 {
207 Set<String> projectIds = new HashSet<>();
208
209 for ( Artifact artifact : artifactIterable )
210 {
211 String key = ArtifactUtils.key( artifact );
212 projectIds.add( key );
213 }
214 return projectIds;
215 }
216
217 }