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 java.lang.reflect.InvocationTargetException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.factory.ArtifactFactory;
29  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.apache.maven.project.DefaultDependencyResolutionRequest;
32  import org.apache.maven.project.DependencyResolutionException;
33  import org.apache.maven.project.DependencyResolutionRequest;
34  import org.apache.maven.project.DependencyResolutionResult;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.project.ProjectBuildingRequest;
37  import org.apache.maven.project.ProjectDependenciesResolver;
38  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
39  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
40  import org.apache.maven.shared.dependency.graph.DependencyNode;
41  import org.codehaus.plexus.component.annotations.Component;
42  import org.codehaus.plexus.component.annotations.Requirement;
43  import org.eclipse.aether.DefaultRepositorySystemSession;
44  import org.eclipse.aether.RepositorySystemSession;
45  import org.eclipse.aether.graph.Dependency;
46  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
47  import org.eclipse.aether.version.VersionConstraint;
48  
49  /**
50   * Wrapper around Eclipse Aether dependency resolver, used in Maven 3.1.
51   *
52   * @see ProjectDependenciesResolver
53   * @author Hervé Boutemy
54   * @since 2.1
55   */
56  @Component( role = DependencyGraphBuilder.class, hint = "maven31" )
57  public class Maven31DependencyGraphBuilder
58      implements DependencyGraphBuilder
59  {
60      @Requirement
61      private ProjectDependenciesResolver resolver;
62  
63      @Requirement
64      private ArtifactFactory factory;
65  
66      public DependencyNode buildDependencyGraph( MavenProject project, ArtifactFilter filter )
67          throws DependencyGraphBuilderException
68      {
69          try
70          {
71              ProjectBuildingRequest projectBuildingRequest =
72                  (ProjectBuildingRequest) invoke( project.getClass(), project, "getProjectBuildingRequest" );
73  
74              RepositorySystemSession session =
75                  (RepositorySystemSession) invoke( ProjectBuildingRequest.class, projectBuildingRequest,
76                                                    "getRepositorySession" );
77  
78              /*if ( Boolean.TRUE != ( (Boolean) session.getConfigProperties().get( DependencyManagerUtils.NODE_DATA_PREMANAGED_VERSION ) ) )
79              {
80                  DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session );
81                  newSession.setConfigProperty( DependencyManagerUtils.NODE_DATA_PREMANAGED_VERSION, true );
82                  session = newSession;
83              }*/
84  
85              DependencyResolutionRequest request =
86                  new DefaultDependencyResolutionRequest();
87              request.setMavenProject( project );
88              invoke( request, "setRepositorySession", RepositorySystemSession.class, session );
89  
90              DependencyResolutionResult result = resolver.resolve( request );
91  
92              org.eclipse.aether.graph.DependencyNode graph =
93                  (org.eclipse.aether.graph.DependencyNode) invoke( DependencyResolutionResult.class, result,
94                                                                    "getDependencyGraph" );
95  
96              return buildDependencyNode( null, graph, project.getArtifact(), filter );
97          }
98          catch ( DependencyResolutionException e )
99          {
100             throw new DependencyGraphBuilderException( e.getMessage(), e );
101         }
102         catch ( IllegalAccessException e )
103         {
104             throw new DependencyGraphBuilderException( e.getMessage(), e );
105         }
106         catch ( InvocationTargetException e )
107         {
108             throw new DependencyGraphBuilderException( e.getMessage(), e );
109         }
110         catch ( NoSuchMethodException e )
111         {
112             throw new DependencyGraphBuilderException( e.getMessage(), e );
113         }
114     }
115 
116     private Object invoke( Class<?> clazz, Object object, String method )
117         throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
118     {
119         return clazz.getMethod( method ).invoke( object );
120     }
121 
122     private Object invoke( Object object, String method, Class<?> clazz, Object arg )
123         throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
124     {
125         return object.getClass().getMethod( method, clazz ).invoke( object, arg );
126     }
127 
128     private Artifact getDependencyArtifact( Dependency dep )
129     {
130         org.eclipse.aether.artifact.Artifact artifact = dep.getArtifact();
131 
132         return factory.createDependencyArtifact( artifact.getGroupId(), artifact.getArtifactId(),
133                                                  VersionRange.createFromVersion( artifact.getVersion() ),
134                                                  artifact.getProperty( "type", artifact.getExtension() ),
135                                                  artifact.getClassifier(), dep.getScope(), dep.isOptional() );
136     }
137 
138     private DependencyNode buildDependencyNode( DependencyNode parent, org.eclipse.aether.graph.DependencyNode node,
139                                                 Artifact artifact, ArtifactFilter filter )
140     {
141         String premanagedVersion = null; //DependencyManagerUtils.getPremanagedVersion( node );
142         String premanagedScope = null; //DependencyManagerUtils.getPremanagedScope( node );
143 
144         DefaultDependencyNode current =
145             new DefaultDependencyNode( parent, artifact, premanagedVersion, premanagedScope,
146                                        getVersionSelectedFromRange( node.getVersionConstraint() ) );
147 
148         List<DependencyNode> nodes = new ArrayList<DependencyNode>( node.getChildren().size() );
149         for ( org.eclipse.aether.graph.DependencyNode child : node.getChildren() )
150         {
151             Artifact childArtifact = getDependencyArtifact( child.getDependency() );
152 
153             if ( ( filter == null ) || filter.include( childArtifact ) )
154             {
155                 nodes.add( buildDependencyNode( current, child, childArtifact, filter ) );
156             }
157         }
158 
159         current.setChildren( Collections.unmodifiableList( nodes ) );
160 
161         return current;
162     }
163 
164     private String getVersionSelectedFromRange( VersionConstraint constraint )
165     {
166         if ( ( constraint == null ) || ( constraint.getVersion() != null ) )
167         {
168             return null;
169         }
170 
171         return constraint.getRange().toString();
172     }
173 }