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.DependencyCollectorBuilder;
27  import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilderException;
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 project dependency raw dependency collector API, providing an abstraction layer against Maven 3 and Maven
40   * 3.1+ particular Aether implementations.
41   * 
42   * @author Gabriel Belingueres
43   * @since 3.1.0
44   */
45  @Component( role = DependencyCollectorBuilder.class )
46  public class DefaultDependencyCollectorBuilder
47      extends AbstractLogEnabled
48      implements DependencyCollectorBuilder, Contextualizable
49  {
50      protected PlexusContainer container;
51  
52      @Override
53      public DependencyNode collectDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter )
54          throws DependencyCollectorBuilderException
55      {
56          try
57          {
58              String hint = isMaven31() ? "maven31" : "maven3";
59  
60              DependencyCollectorBuilder effectiveGraphBuilder =
61                  (DependencyCollectorBuilder) container.lookup( DependencyCollectorBuilder.class.getCanonicalName(),
62                                                                 hint );
63  
64              if ( getLogger().isDebugEnabled() )
65              {
66                  MavenProject project = buildingRequest.getProject();
67  
68                  getLogger().debug( "building " + hint + " RAW dependency tree for " + project.getId() + " with "
69                      + effectiveGraphBuilder.getClass().getSimpleName() );
70              }
71  
72              return effectiveGraphBuilder.collectDependencyGraph( buildingRequest, filter );
73          }
74          catch ( ComponentLookupException e )
75          {
76              throw new DependencyCollectorBuilderException( e.getMessage(), e );
77          }
78      }
79  
80      /**
81       * @return true if the current Maven version is Maven 3.1.
82       */
83      protected static boolean isMaven31()
84      {
85          try
86          {
87              Class<?> repoSessionClass =  MavenSession.class.getMethod( "getRepositorySession" ).getReturnType();
88              
89              return "org.eclipse.aether.RepositorySystemSession".equals( repoSessionClass.getName() );
90          }
91          catch ( NoSuchMethodException e )
92          {
93              throw new IllegalStateException( "Cannot determine return type of MavenSession.getRepositorySession" );
94          }
95      }
96  
97      /**
98       * Injects the Plexus content.
99       *
100      * @param context Plexus context to inject.
101      * @throws ContextException if the PlexusContainer could not be located.
102      */
103     @Override
104     public void contextualize( Context context )
105         throws ContextException
106     {
107         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
108     }
109 
110 }