1 package org.apache.maven.project;
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.HashMap;
24 import java.util.Map;
25
26 import org.apache.maven.RepositoryUtils;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.model.Dependency;
29 import org.apache.maven.model.DependencyManagement;
30 import org.apache.maven.model.Exclusion;
31 import org.apache.maven.model.InputLocation;
32 import org.apache.maven.model.InputSource;
33 import org.codehaus.plexus.component.annotations.Component;
34 import org.codehaus.plexus.component.annotations.Requirement;
35 import org.codehaus.plexus.logging.Logger;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.eclipse.aether.DefaultRepositorySystemSession;
38 import org.eclipse.aether.RepositorySystem;
39 import org.eclipse.aether.RepositorySystemSession;
40 import org.eclipse.aether.RequestTrace;
41 import org.eclipse.aether.artifact.ArtifactProperties;
42 import org.eclipse.aether.artifact.ArtifactType;
43 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
44 import org.eclipse.aether.collection.CollectRequest;
45 import org.eclipse.aether.collection.DependencyCollectionException;
46 import org.eclipse.aether.graph.DependencyFilter;
47 import org.eclipse.aether.graph.DependencyNode;
48 import org.eclipse.aether.graph.DependencyVisitor;
49 import org.eclipse.aether.resolution.ArtifactResult;
50 import org.eclipse.aether.resolution.DependencyRequest;
51 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
52 import org.eclipse.aether.util.artifact.JavaScopes;
53 import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
54
55
56
57
58 @Component( role = ProjectDependenciesResolver.class )
59 public class DefaultProjectDependenciesResolver
60 implements ProjectDependenciesResolver
61 {
62
63 @Requirement
64 private Logger logger;
65
66 @Requirement
67 private RepositorySystem repoSystem;
68
69 public DependencyResolutionResult resolve( DependencyResolutionRequest request )
70 throws DependencyResolutionException
71 {
72 RequestTrace trace = RequestTrace.newChild( null, request );
73
74 DefaultDependencyResolutionResult result = new DefaultDependencyResolutionResult();
75
76 MavenProject project = request.getMavenProject();
77 RepositorySystemSession session = request.getRepositorySession();
78 if ( logger.isDebugEnabled()
79 && session.getConfigProperties().get( DependencyManagerUtils.CONFIG_PROP_VERBOSE ) == null )
80 {
81 DefaultRepositorySystemSession verbose = new DefaultRepositorySystemSession( session );
82 verbose.setConfigProperty( DependencyManagerUtils.CONFIG_PROP_VERBOSE, Boolean.TRUE );
83 session = verbose;
84 }
85 DependencyFilter filter = request.getResolutionFilter();
86
87 ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
88
89 CollectRequest collect = new CollectRequest();
90 collect.setRootArtifact( RepositoryUtils.toArtifact( project.getArtifact() ) );
91 collect.setRequestContext( "project" );
92 collect.setRepositories( project.getRemoteProjectRepositories() );
93
94 if ( project.getDependencyArtifacts() == null )
95 {
96 for ( Dependency dependency : project.getDependencies() )
97 {
98 if ( StringUtils.isEmpty( dependency.getGroupId() ) || StringUtils.isEmpty( dependency.getArtifactId() )
99 || StringUtils.isEmpty( dependency.getVersion() ) )
100 {
101
102 continue;
103 }
104 collect.addDependency( RepositoryUtils.toDependency( dependency, stereotypes ) );
105 }
106 }
107 else
108 {
109 Map<String, Dependency> dependencies = new HashMap<String, Dependency>();
110 for ( Dependency dependency : project.getDependencies() )
111 {
112 String classifier = dependency.getClassifier();
113 if ( classifier == null )
114 {
115 ArtifactType type = stereotypes.get( dependency.getType() );
116 if ( type != null )
117 {
118 classifier = type.getClassifier();
119 }
120 }
121 String key =
122 ArtifactIdUtils.toVersionlessId( dependency.getGroupId(), dependency.getArtifactId(),
123 dependency.getType(), classifier );
124 dependencies.put( key, dependency );
125 }
126 for ( Artifact artifact : project.getDependencyArtifacts() )
127 {
128 String key = artifact.getDependencyConflictId();
129 Dependency dependency = dependencies.get( key );
130 Collection<Exclusion> exclusions = dependency != null ? dependency.getExclusions() : null;
131 org.eclipse.aether.graph.Dependency dep = RepositoryUtils.toDependency( artifact, exclusions );
132 if ( !JavaScopes.SYSTEM.equals( dep.getScope() ) && dep.getArtifact().getFile() != null )
133 {
134
135 org.eclipse.aether.artifact.Artifact art = dep.getArtifact();
136 art = art.setFile( null ).setVersion( art.getBaseVersion() );
137 dep = dep.setArtifact( art );
138 }
139 collect.addDependency( dep );
140 }
141 }
142
143 DependencyManagement depMngt = project.getDependencyManagement();
144 if ( depMngt != null )
145 {
146 for ( Dependency dependency : depMngt.getDependencies() )
147 {
148 collect.addManagedDependency( RepositoryUtils.toDependency( dependency, stereotypes ) );
149 }
150 }
151
152 DependencyRequest depRequest = new DependencyRequest( collect, filter );
153 depRequest.setTrace( trace );
154
155 DependencyNode node;
156 try
157 {
158 collect.setTrace( RequestTrace.newChild( trace, depRequest ) );
159 node = repoSystem.collectDependencies( session, collect ).getRoot();
160 result.setDependencyGraph( node );
161 }
162 catch ( DependencyCollectionException e )
163 {
164 result.setDependencyGraph( e.getResult().getRoot() );
165 result.setCollectionErrors( e.getResult().getExceptions() );
166
167 throw new DependencyResolutionException( result, "Could not resolve dependencies for project "
168 + project.getId() + ": " + e.getMessage(), e );
169 }
170
171 depRequest.setRoot( node );
172
173 if ( logger.isWarnEnabled() )
174 {
175 for ( DependencyNode child : node.getChildren() )
176 {
177 if ( !child.getRelocations().isEmpty() )
178 {
179 logger.warn( "The artifact " + child.getRelocations().get( 0 ) + " has been relocated to "
180 + child.getDependency().getArtifact() );
181 }
182 }
183 }
184
185 if ( logger.isDebugEnabled() )
186 {
187 node.accept( new GraphLogger( project ) );
188 }
189
190 try
191 {
192 process( result, repoSystem.resolveDependencies( session, depRequest ).getArtifactResults() );
193 }
194 catch ( org.eclipse.aether.resolution.DependencyResolutionException e )
195 {
196 process( result, e.getResult().getArtifactResults() );
197
198 throw new DependencyResolutionException( result, "Could not resolve dependencies for project "
199 + project.getId() + ": " + e.getMessage(), e );
200 }
201
202 return result;
203 }
204
205 private void process( DefaultDependencyResolutionResult result, Collection<ArtifactResult> results )
206 {
207 for ( ArtifactResult ar : results )
208 {
209 DependencyNode node = ar.getRequest().getDependencyNode();
210 if ( ar.isResolved() )
211 {
212 result.addResolvedDependency( node.getDependency() );
213 }
214 else
215 {
216 result.setResolutionErrors( node.getDependency(), ar.getExceptions() );
217 }
218 }
219 }
220
221 class GraphLogger
222 implements DependencyVisitor
223 {
224
225 private final MavenProject project;
226
227 private String indent = "";
228
229 private Map<String, Dependency> managed;
230
231 public GraphLogger( MavenProject project )
232 {
233 this.project = project;
234 }
235
236 public boolean visitEnter( DependencyNode node )
237 {
238 StringBuilder buffer = new StringBuilder( 128 );
239 buffer.append( indent );
240 org.eclipse.aether.graph.Dependency dep = node.getDependency();
241 if ( dep != null )
242 {
243 org.eclipse.aether.artifact.Artifact art = dep.getArtifact();
244
245 buffer.append( art );
246 buffer.append( ':' ).append( dep.getScope() );
247
248 String premanagedScope = DependencyManagerUtils.getPremanagedScope( node );
249 if ( premanagedScope != null && !premanagedScope.equals( dep.getScope() ) )
250 {
251 buffer.append( " (scope managed from " ).append( premanagedScope );
252 appendManagementSource( buffer, art, "scope" );
253 buffer.append( ")" );
254 }
255
256 String premanagedVersion = DependencyManagerUtils.getPremanagedVersion( node );
257 if ( premanagedVersion != null && !premanagedVersion.equals( art.getVersion() ) )
258 {
259 buffer.append( " (version managed from " ).append( premanagedVersion );
260 appendManagementSource( buffer, art, "version" );
261 buffer.append( ")" );
262 }
263 }
264 else
265 {
266 buffer.append( project.getGroupId() );
267 buffer.append( ':' ).append( project.getArtifactId() );
268 buffer.append( ':' ).append( project.getPackaging() );
269 buffer.append( ':' ).append( project.getVersion() );
270 }
271
272 logger.debug( buffer.toString() );
273 indent += " ";
274 return true;
275 }
276
277 public boolean visitLeave( DependencyNode node )
278 {
279 indent = indent.substring( 0, indent.length() - 3 );
280 return true;
281 }
282
283 private void appendManagementSource( StringBuilder buffer, org.eclipse.aether.artifact.Artifact artifact,
284 String field )
285 {
286 if ( managed == null )
287 {
288 managed = new HashMap<String, Dependency>();
289 if ( project.getDependencyManagement() != null )
290 {
291 for ( Dependency dep : project.getDependencyManagement().getDependencies() )
292 {
293 managed.put( dep.getManagementKey(), dep );
294 }
295 }
296 }
297
298 String key =
299 ArtifactIdUtils.toVersionlessId( artifact.getGroupId(), artifact.getArtifactId(),
300 artifact.getProperty( ArtifactProperties.TYPE, "jar" ),
301 artifact.getClassifier() );
302
303 Dependency dependency = managed.get( key );
304 if ( dependency != null )
305 {
306 InputLocation location = dependency.getLocation( field );
307 if ( location != null )
308 {
309 InputSource source = location.getSource();
310 if ( source != null )
311 {
312 buffer.append( " by " ).append( source.getModelId() );
313 }
314 }
315 }
316 }
317
318 }
319
320 }