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 project the project
74       * @param filter artifact filter (can be <code>null</code>)
75       * @param reactorProjects Collection of those projects contained in the reactor (can be <code>null</code>)
76       * @return DependencyNode containing the dependency graph.
77       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
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      * @return true if the current Maven version is Maven 3.1.
109      */
110     protected static boolean isMaven31()
111     {
112         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
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      * Injects the Plexus content.
131      *
132      * @param context   Plexus context to inject.
133      * @throws ContextException if the PlexusContainer could not be located.
134      */
135     @Override
136     public void contextualize( Context context )
137         throws ContextException
138     {
139         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
140     }
141 }