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 static org.eclipse.aether.util.graph.manager.DependencyManagerUtils.NODE_DATA_PREMANAGED_VERSION;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  
31  import org.apache.maven.RepositoryUtils;
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
34  import org.apache.maven.project.DefaultDependencyResolutionRequest;
35  import org.apache.maven.project.DependencyResolutionException;
36  import org.apache.maven.project.DependencyResolutionRequest;
37  import org.apache.maven.project.DependencyResolutionResult;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.project.ProjectBuildingRequest;
40  import org.apache.maven.project.ProjectDependenciesResolver;
41  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
42  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
43  import org.apache.maven.shared.dependency.graph.DependencyNode;
44  import org.eclipse.aether.DefaultRepositorySystemSession;
45  import org.eclipse.aether.RepositorySystemSession;
46  import org.eclipse.aether.graph.Dependency;
47  import org.eclipse.aether.graph.DependencyFilter;
48  import org.eclipse.aether.graph.Exclusion;
49  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
50  import org.eclipse.aether.version.VersionConstraint;
51  
52  /**
53   * Wrapper around Eclipse Aether dependency resolver, used in Maven 3.1.
54   *
55   * @see ProjectDependenciesResolver
56   * @author Hervé Boutemy
57   * @since 2.1
58   */
59  @Named
60  public class DefaultDependencyGraphBuilder
61      implements DependencyGraphBuilder
62  {
63      private final ProjectDependenciesResolver resolver;
64  
65      @Inject
66      public DefaultDependencyGraphBuilder( ProjectDependenciesResolver resolver )
67      {
68          this.resolver = resolver;
69      }
70  
71      /**
72       * Builds the dependency graph for Maven 3.1+.
73       *
74       * @param buildingRequest the buildingRequest
75       * @param filter artifact filter (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          throws DependencyGraphBuilderException
82      {
83          MavenProject project = buildingRequest.getProject();
84  
85          RepositorySystemSession session = buildingRequest.getRepositorySession();
86          
87          if ( Boolean.TRUE != session.getConfigProperties().get( NODE_DATA_PREMANAGED_VERSION ) )
88          {
89              DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session );
90              newSession.setConfigProperty( NODE_DATA_PREMANAGED_VERSION, true );
91              session = newSession;
92          }         
93  
94          final DependencyResolutionRequest request = new DefaultDependencyResolutionRequest();
95          request.setMavenProject( project );
96          request.setRepositorySession( session );
97          // only download the poms, not the artifacts
98          DependencyFilter collectFilter = ( node, parents ) -> false;
99          request.setResolutionFilter( collectFilter );
100 
101         final DependencyResolutionResult result = resolveDependencies( request );
102 
103         org.eclipse.aether.graph.DependencyNode graph = result.getDependencyGraph();
104 
105         return buildDependencyNode( null, graph, project.getArtifact(), filter );
106     }
107 
108     private DependencyResolutionResult resolveDependencies( DependencyResolutionRequest request )
109         throws DependencyGraphBuilderException
110     {
111         try
112         {
113             return resolver.resolve( request );
114         }
115         catch ( DependencyResolutionException e )
116         {
117             throw new DependencyGraphBuilderException( "Could not resolve following dependencies: "
118                 + e.getResult().getUnresolvedDependencies(), e );
119         }
120     }
121 
122     private Artifact getDependencyArtifact( Dependency dep )
123     {
124         org.eclipse.aether.artifact.Artifact artifact = dep.getArtifact();
125 
126         Artifact mavenArtifact = RepositoryUtils.toArtifact( artifact );
127 
128         mavenArtifact.setScope( dep.getScope() );
129         mavenArtifact.setOptional( dep.isOptional() );
130 
131         return mavenArtifact;
132     }
133 
134     private DependencyNode buildDependencyNode( DependencyNode parent, org.eclipse.aether.graph.DependencyNode node,
135                                                 Artifact artifact, ArtifactFilter filter )
136     {
137         String premanagedVersion = DependencyManagerUtils.getPremanagedVersion( node );
138         String premanagedScope = DependencyManagerUtils.getPremanagedScope( node );
139 
140         List<org.apache.maven.model.Exclusion> exclusions = null;
141         Boolean optional = null;
142         if ( node.getDependency() != null )
143         {
144             exclusions = new ArrayList<>( node.getDependency().getExclusions().size() );
145             for ( Exclusion exclusion : node.getDependency().getExclusions() )
146             {
147                 org.apache.maven.model.Exclusion modelExclusion = new org.apache.maven.model.Exclusion();
148                 modelExclusion.setGroupId( exclusion.getGroupId() );
149                 modelExclusion.setArtifactId( exclusion.getArtifactId() );
150                 exclusions.add( modelExclusion );
151             }
152         }
153 
154         DefaultDependencyNode current =
155             new DefaultDependencyNode( parent, artifact, premanagedVersion, premanagedScope,
156                                        getVersionSelectedFromRange( node.getVersionConstraint() ),
157                                        optional, exclusions );
158 
159         List<DependencyNode> nodes = new ArrayList<>( node.getChildren().size() );
160         for ( org.eclipse.aether.graph.DependencyNode child : node.getChildren() )
161         {
162             Artifact childArtifact = getDependencyArtifact( child.getDependency() );
163 
164             if ( ( filter == null ) || filter.include( childArtifact ) )
165             {
166                 nodes.add( buildDependencyNode( current, child, childArtifact, filter ) );
167             }
168         }
169 
170         current.setChildren( Collections.unmodifiableList( nodes ) );
171 
172         return current;
173     }
174 
175     private String getVersionSelectedFromRange( VersionConstraint constraint )
176     {
177         if ( ( constraint == null ) || ( constraint.getVersion() != null ) )
178         {
179             return null;
180         }
181 
182         return constraint.getRange().toString();
183     }
184 }