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.project.MavenProject;
24 import org.apache.maven.project.ProjectBuildingRequest;
25 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
26 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
27 import org.apache.maven.shared.dependency.graph.DependencyNode;
28 import org.codehaus.plexus.PlexusConstants;
29 import org.codehaus.plexus.PlexusContainer;
30 import org.codehaus.plexus.component.annotations.Component;
31 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32 import org.codehaus.plexus.context.Context;
33 import org.codehaus.plexus.context.ContextException;
34 import org.codehaus.plexus.logging.AbstractLogEnabled;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36
37 import java.util.Collection;
38
39
40
41
42
43
44
45
46
47
48 @Component( role = DependencyGraphBuilder.class )
49 public class DefaultDependencyGraphBuilder
50 extends AbstractLogEnabled
51 implements DependencyGraphBuilder, Contextualizable
52 {
53 protected PlexusContainer container;
54
55
56
57
58
59
60
61
62
63 @Override
64 public DependencyNode buildDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter )
65 throws DependencyGraphBuilderException
66 {
67 return buildDependencyGraph( buildingRequest, filter, null );
68 }
69
70
71
72
73
74
75
76
77
78
79 @Override
80 public DependencyNode buildDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter,
81 Collection<MavenProject> reactorProjects )
82 throws DependencyGraphBuilderException
83 {
84 try
85 {
86 String hint = isMaven31() ? "maven31" : "maven3";
87
88 DependencyGraphBuilder effectiveGraphBuilder =
89 (DependencyGraphBuilder) container.lookup( DependencyGraphBuilder.class.getCanonicalName(), hint );
90
91 if ( getLogger().isDebugEnabled() )
92 {
93 MavenProject project = buildingRequest.getProject();
94
95 getLogger().debug( "building " + hint + " dependency graph for " + project.getId() + " with "
96 + effectiveGraphBuilder.getClass().getSimpleName() );
97 }
98
99 return effectiveGraphBuilder.buildDependencyGraph( buildingRequest, filter, reactorProjects );
100 }
101 catch ( ComponentLookupException e )
102 {
103 throw new DependencyGraphBuilderException( e.getMessage(), e );
104 }
105 }
106
107
108
109
110 protected static boolean isMaven31()
111 {
112 return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" );
113 }
114
115 private static boolean canFindCoreClass( String className )
116 {
117 try
118 {
119 Thread.currentThread().getContextClassLoader().loadClass( className );
120
121 return true;
122 }
123 catch ( ClassNotFoundException e )
124 {
125 return false;
126 }
127 }
128
129
130
131
132
133
134
135 @Override
136 public void contextualize( Context context )
137 throws ContextException
138 {
139 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
140 }
141 }