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.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
28  import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.List;
37  
38  /**
39   * Wrapper around Maven 2 dependency tree builder.
40   *
41   * @see DependencyTreeBuilder
42   * @author Hervé Boutemy
43   * @since 2.0
44   */
45  @Component( role = DependencyGraphBuilder.class, hint = "maven2" )
46  public class Maven2DependencyGraphBuilder
47      extends AbstractLogEnabled
48      implements DependencyGraphBuilder
49  {
50      @Requirement
51      private DependencyTreeBuilder treeBuilder;
52  
53      /**
54       * Builds the dependency graph for Maven 2.
55       *
56       * @param project the project
57       * @param filter artifact filter (can be <code>null</code>)
58       * @return DependencyNode containing the dependency graph.
59       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
60       */
61      public DependencyNode buildDependencyGraph( MavenProject project, ArtifactFilter filter )
62          throws DependencyGraphBuilderException
63      {
64          try
65          {
66              return buildDependencyNode( null, treeBuilder.buildDependencyTree( project ), filter );
67          }
68          catch ( DependencyTreeBuilderException e )
69          {
70              throw new DependencyGraphBuilderException( e.getMessage(), e );
71          }
72      }
73  
74      /**
75       * Builds the dependency graph for Maven 2.
76       * <p>
77       * notice: the reactor projects are ignored as no work has been done to try to do the same hack as with Maven 3.
78       * </p>
79       * 
80       * @param project the project
81       * @param filter artifact filter (can be <code>null</code>)
82       * @param reactorProjects Ignored.
83       * @return DependencyNode containing the dependency graph.
84       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
85       */
86      public DependencyNode buildDependencyGraph( MavenProject project, ArtifactFilter filter,
87                                                  Collection<MavenProject> reactorProjects )
88          throws DependencyGraphBuilderException
89      {
90          if ( reactorProjects != null )
91          {
92              getLogger().warn( "Reactor projects ignored - reactor project collection not implemented" );
93          }
94  
95          return buildDependencyGraph( project, filter );
96      }
97  
98      private DependencyNode buildDependencyNode( DependencyNode parent,
99                                                  org.apache.maven.shared.dependency.tree.DependencyNode node,
100                                                 ArtifactFilter filter )
101     {
102         String versionSelectedFromRange = null;
103         if ( node.getVersionSelectedFromRange() != null )
104         {
105             versionSelectedFromRange = node.getVersionSelectedFromRange().toString();
106         }
107 
108         DefaultDependencyNode current =
109             new DefaultDependencyNode( parent, node.getArtifact(), node.getPremanagedVersion(),
110                                        node.getPremanagedScope(), versionSelectedFromRange );
111 
112         List<DependencyNode> nodes = new ArrayList<DependencyNode>( node.getChildren().size() );
113         for ( org.apache.maven.shared.dependency.tree.DependencyNode child : node.getChildren() )
114         {
115             if ( child.getState() != org.apache.maven.shared.dependency.tree.DependencyNode.INCLUDED )
116             {
117                 // only included nodes are supported in the graph API
118                 continue;
119             }
120 
121             if ( ( filter == null ) || filter.include( child.getArtifact() ) )
122             {
123                 nodes.add( buildDependencyNode( current, child, filter ) );
124             }
125         }
126 
127         current.setChildren( Collections.unmodifiableList( nodes ) );
128 
129         return current;
130     }
131 }