1 package org.apache.maven.shared.dependency.graph.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
23 import org.apache.maven.execution.MavenSession;
24 import org.apache.maven.project.MavenProject;
25 import org.apache.maven.project.ProjectBuildingRequest;
26 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
27 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
28 import org.apache.maven.shared.dependency.graph.DependencyNode;
29 import org.codehaus.plexus.PlexusConstants;
30 import org.codehaus.plexus.PlexusContainer;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33 import org.codehaus.plexus.context.Context;
34 import org.codehaus.plexus.context.ContextException;
35 import org.codehaus.plexus.logging.AbstractLogEnabled;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
37
38
39
40
41
42
43
44
45
46
47 @Component( role = DependencyGraphBuilder.class )
48 public class DefaultDependencyGraphBuilder
49 extends AbstractLogEnabled
50 implements DependencyGraphBuilder, Contextualizable
51 {
52 protected PlexusContainer container;
53
54
55
56
57
58
59
60
61
62 @Override
63 public DependencyNode buildDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter )
64 throws DependencyGraphBuilderException
65 {
66 try
67 {
68 String hint = isMaven31() ? "maven31" : "maven3";
69
70 DependencyGraphBuilder effectiveGraphBuilder =
71 (DependencyGraphBuilder) container.lookup( DependencyGraphBuilder.class.getCanonicalName(), hint );
72
73 if ( getLogger().isDebugEnabled() )
74 {
75 MavenProject project = buildingRequest.getProject();
76
77 getLogger().debug( "building " + hint + " dependency graph for " + project.getId() + " with "
78 + effectiveGraphBuilder.getClass().getSimpleName() );
79 }
80
81 return effectiveGraphBuilder.buildDependencyGraph( buildingRequest, filter );
82 }
83 catch ( ComponentLookupException e )
84 {
85 throw new DependencyGraphBuilderException( e.getMessage(), e );
86 }
87 }
88
89
90
91
92 protected static boolean isMaven31()
93 {
94 try
95 {
96 Class<?> repoSessionClass = MavenSession.class.getMethod( "getRepositorySession" ).getReturnType();
97
98 return "org.eclipse.aether.RepositorySystemSession".equals( repoSessionClass.getName() );
99 }
100 catch ( NoSuchMethodException e )
101 {
102 throw new IllegalStateException( "Cannot determine return type of MavenSession.getRepositorySession" );
103 }
104 }
105
106
107
108
109
110
111
112 @Override
113 public void contextualize( Context context )
114 throws ContextException
115 {
116 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
117 }
118 }