View Javadoc
1   package org.apache.maven.shared.dependency.graph.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Default dependency graph builder that detects current Maven version to delegate to either Maven 3.0 or 3.1+ specific
41   * code.
42   *
43   * @see Maven3DependencyGraphBuilder
44   * @see Maven31DependencyGraphBuilder
45   * @author Hervé Boutemy
46   * @since 2.0
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       * Builds a dependency graph.
57       *
58       * @param buildingRequest the buildingRequest
59       * @param filter artifact filter (can be <code>null</code>)
60       * @return DependencyNode containing the dependency graph.
61       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
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       * Builds a dependency graph.
72       *
73       * @param filter artifact filter (can be <code>null</code>)
74       * @param reactorProjects Collection of those projects contained in the reactor (can be <code>null</code>)
75       * @return DependencyNode containing the dependency graph.
76       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
77       */
78      @Override
79      public DependencyNode buildDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter,
80                                                  Collection<MavenProject> reactorProjects )
81          throws DependencyGraphBuilderException
82      {
83          try
84          {
85              String hint = isMaven31() ? "maven31" : "maven3";
86  
87              DependencyGraphBuilder effectiveGraphBuilder =
88                  (DependencyGraphBuilder) container.lookup( DependencyGraphBuilder.class.getCanonicalName(), hint );
89              
90              if ( getLogger().isDebugEnabled() )
91              {
92                  MavenProject project = buildingRequest.getProject();
93                  
94                  getLogger().debug( "building " + hint + " dependency graph for " + project.getId() + " with "
95                                  + effectiveGraphBuilder.getClass().getSimpleName() );
96              }
97  
98              return effectiveGraphBuilder.buildDependencyGraph( buildingRequest, filter, reactorProjects );
99          }
100         catch ( ComponentLookupException e )
101         {
102             throw new DependencyGraphBuilderException( e.getMessage(), e );
103         }
104     }
105 
106     /**
107      * @return true if the current Maven version is Maven 3.1.
108      */
109     protected static boolean isMaven31()
110     {
111         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
112     }
113 
114     private static boolean canFindCoreClass( String className )
115     {
116         try
117         {
118             Thread.currentThread().getContextClassLoader().loadClass( className );
119 
120             return true;
121         }
122         catch ( ClassNotFoundException e )
123         {
124             return false;
125         }
126     }
127 
128     /**
129      * Injects the Plexus content.
130      *
131      * @param context   Plexus context to inject.
132      * @throws ContextException if the PlexusContainer could not be located.
133      */
134     @Override
135     public void contextualize( Context context )
136         throws ContextException
137     {
138         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
139     }
140 }