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.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  /**
37   * Default dependency graph builder that detects current Maven version to delegate to either
38   * Maven 2 or Maven 3 specific code.
39   *
40   * @see Maven2DependencyGraphBuilder
41   * @see Maven3DependencyGraphBuilder
42   * @author Hervé Boutemy
43   * @since 2.0
44   */
45  @Component( role = DependencyGraphBuilder.class )
46  public class DefaultDependencyGraphBuilder
47      extends AbstractLogEnabled
48      implements DependencyGraphBuilder, Contextualizable
49  {
50      protected PlexusContainer container;
51  
52      public DependencyNode buildDependencyGraph( MavenProject project, ArtifactFilter filter )
53          throws DependencyGraphBuilderException
54      {
55          try
56          {
57              String hint = isMaven2x() ? "maven2" : "maven3";
58              getLogger().debug( "building " + hint + " dependency graph for " + project.getId() );
59  
60              DependencyGraphBuilder effectiveGraphBuilder =
61                  (DependencyGraphBuilder) container.lookup( DependencyGraphBuilder.class.getCanonicalName(), hint );
62  
63              return effectiveGraphBuilder.buildDependencyGraph( project, filter );
64          }
65          catch ( ComponentLookupException e )
66          {
67              throw new DependencyGraphBuilderException( e.getMessage(), e );
68          }
69      }
70  
71      /**
72       * Check the current Maven version to see if it's Maven 2.x.
73       */
74      protected static boolean isMaven2x()
75      {
76          try
77          {
78              Class.forName( "org.apache.maven.project.DependencyResolutionRequest" ); // Maven 3 specific
79  
80              return false;
81          }
82          catch ( ClassNotFoundException e )
83          {
84              return true;
85          }
86      }
87  
88      public void contextualize( Context context )
89          throws ContextException
90      {
91          container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
92      }
93  }