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.shared.dependency.graph.DependencyGraphBuilder;
25 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
26 import org.apache.maven.shared.dependency.graph.DependencyNode;
27 import org.codehaus.plexus.PlexusConstants;
28 import org.codehaus.plexus.PlexusContainer;
29 import org.codehaus.plexus.component.annotations.Component;
30 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
31 import org.codehaus.plexus.context.Context;
32 import org.codehaus.plexus.context.ContextException;
33 import org.codehaus.plexus.logging.AbstractLogEnabled;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
35
36 import java.util.Collection;
37 import java.util.Collections;
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 public DependencyNode buildDependencyGraph( MavenProject project, ArtifactFilter filter )
64 throws DependencyGraphBuilderException
65 {
66 return buildDependencyGraph( project, filter, null );
67 }
68
69
70
71
72
73
74
75
76
77
78 public DependencyNode buildDependencyGraph( MavenProject project, ArtifactFilter filter,
79 Collection<MavenProject> reactorProjects )
80 throws DependencyGraphBuilderException
81 {
82 try
83 {
84 String hint = isMaven31() ? "maven31" : isMaven2x() ? "maven2" : "maven3";
85
86 DependencyGraphBuilder effectiveGraphBuilder =
87 (DependencyGraphBuilder) container.lookup( DependencyGraphBuilder.class.getCanonicalName(), hint );
88 getLogger().debug( "building " + hint + " dependency graph for " + project.getId() + " with "
89 + effectiveGraphBuilder.getClass().getSimpleName() );
90
91 return effectiveGraphBuilder.buildDependencyGraph( project, filter, reactorProjects );
92 }
93 catch ( ComponentLookupException e )
94 {
95 throw new DependencyGraphBuilderException( e.getMessage(), e );
96 }
97 }
98
99
100
101
102 protected static boolean isMaven2x()
103 {
104 return !canFindCoreClass( "org.apache.maven.project.DependencyResolutionRequest" );
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 public void contextualize( Context context )
136 throws ContextException
137 {
138 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
139 }
140 }