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.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   * Default dependency graph builder that detects current Maven version to delegate to either Maven 3.0 or 3.1+ specific
40   * code.
41   *
42   * @see Maven3DependencyGraphBuilder
43   * @see Maven31DependencyGraphBuilder
44   * @author Hervé Boutemy
45   * @since 2.0
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       * Builds a dependency graph.
56       *
57       * @param buildingRequest the buildingRequest
58       * @param filter artifact filter (can be <code>null</code>)
59       * @return DependencyNode containing the dependency graph.
60       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
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       * @return true if the current Maven version is Maven 3.1.
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      * Injects the Plexus content.
108      *
109      * @param context   Plexus context to inject.
110      * @throws ContextException if the PlexusContainer could not be located.
111      */
112     @Override
113     public void contextualize( Context context )
114         throws ContextException
115     {
116         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
117     }
118 }