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