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 org.apache.maven.RepositoryUtils;
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
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.codehaus.plexus.logging.AbstractLogEnabled;
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  @Component( role = DependencyGraphBuilder.class, hint = "maven31" )
60  public class Maven31DependencyGraphBuilder
61      extends AbstractLogEnabled
62      implements DependencyGraphBuilder
63  {
64      @Requirement
65      private ProjectDependenciesResolver resolver;
66  
67      private final ExceptionHandler<DependencyGraphBuilderException> exceptionHandler;
68      
69      public Maven31DependencyGraphBuilder()
70      {
71          this.exceptionHandler = new ExceptionHandler<DependencyGraphBuilderException>()
72          {
73              @Override
74              public DependencyGraphBuilderException create( String message, Exception exception )
75              {
76                  return new DependencyGraphBuilderException( message, exception );
77              }
78          };
79      }
80  
81      /**
82       * Builds the dependency graph for Maven 3.1+.
83       *
84       * @param buildingRequest the buildingRequest
85       * @param filter artifact filter (can be <code>null</code>)
86       * @return DependencyNode containing the dependency graph.
87       * @throws DependencyGraphBuilderException if some of the dependencies could not be resolved.
88       */
89      @Override
90      public DependencyNode buildDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter )
91          throws DependencyGraphBuilderException
92      {
93          MavenProject project = buildingRequest.getProject();
94  
95          RepositorySystemSession session =
96              (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession", exceptionHandler );
97  
98          
99          if ( Boolean.TRUE != ( (Boolean) session.getConfigProperties().get( NODE_DATA_PREMANAGED_VERSION ) ) )
100         {
101             DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session );
102             newSession.setConfigProperty( NODE_DATA_PREMANAGED_VERSION, true );
103             session = newSession;
104         }         
105 
106         final DependencyResolutionRequest request = new DefaultDependencyResolutionRequest();
107         request.setMavenProject( project );
108         Invoker.invoke( request, "setRepositorySession", RepositorySystemSession.class, session );
109         // only download the poms, not the artifacts
110         DependencyFilter collectFilter = new DependencyFilter()
111         {
112             @Override
113             public boolean accept( org.eclipse.aether.graph.DependencyNode node,
114                                    List<org.eclipse.aether.graph.DependencyNode> parents )
115             {
116                 return false;
117             }
118         };
119         Invoker.invoke( request, "setResolutionFilter", DependencyFilter.class, collectFilter );
120 
121         final DependencyResolutionResult result = resolveDependencies( request );
122         org.eclipse.aether.graph.DependencyNode graph =
123             (org.eclipse.aether.graph.DependencyNode) Invoker.invoke( DependencyResolutionResult.class, result,
124                                                                       "getDependencyGraph", exceptionHandler );
125 
126         return buildDependencyNode( null, graph, project.getArtifact(), filter );
127     }
128 
129     private DependencyResolutionResult resolveDependencies( DependencyResolutionRequest request )
130         throws DependencyGraphBuilderException
131     {
132         try
133         {
134             return resolver.resolve( request );
135         }
136         catch ( DependencyResolutionException e )
137         {
138             throw new DependencyGraphBuilderException( "Could not resolve following dependencies: "
139                 + e.getResult().getUnresolvedDependencies(), e );
140         }
141     }
142 
143     private Artifact getDependencyArtifact( Dependency dep )
144     {
145         org.eclipse.aether.artifact.Artifact artifact = dep.getArtifact();
146 
147         try
148         {
149             Artifact mavenArtifact = (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
150                                               org.eclipse.aether.artifact.Artifact.class, artifact, exceptionHandler );
151 
152             mavenArtifact.setScope( dep.getScope() );
153             mavenArtifact.setOptional( dep.isOptional() );
154 
155             return mavenArtifact;
156         }
157         catch ( DependencyGraphBuilderException e )
158         {
159             // ReflectionException should not happen
160             throw new RuntimeException( e.getMessage(), e );
161         }
162     }
163 
164     private DependencyNode buildDependencyNode( DependencyNode parent, org.eclipse.aether.graph.DependencyNode node,
165                                                 Artifact artifact, ArtifactFilter filter )
166     {
167         String premanagedVersion = DependencyManagerUtils.getPremanagedVersion( node );
168         String premanagedScope = DependencyManagerUtils.getPremanagedScope( node );
169 
170         List<org.apache.maven.model.Exclusion> exclusions = null;
171         Boolean optional = null;
172         if ( node.getDependency() != null )
173         {
174             exclusions = new ArrayList<>( node.getDependency().getExclusions().size() );
175             for ( Exclusion exclusion : node.getDependency().getExclusions() )
176             {
177                 org.apache.maven.model.Exclusion modelExclusion = new org.apache.maven.model.Exclusion();
178                 modelExclusion.setGroupId( exclusion.getGroupId() );
179                 modelExclusion.setArtifactId( exclusion.getArtifactId() );
180                 exclusions.add( modelExclusion );
181             }
182         }
183 
184         DefaultDependencyNode current =
185             new DefaultDependencyNode( parent, artifact, premanagedVersion, premanagedScope,
186                                        getVersionSelectedFromRange( node.getVersionConstraint() ),
187                                        optional, exclusions );
188 
189         List<DependencyNode> nodes = new ArrayList<DependencyNode>( node.getChildren().size() );
190         for ( org.eclipse.aether.graph.DependencyNode child : node.getChildren() )
191         {
192             Artifact childArtifact = getDependencyArtifact( child.getDependency() );
193 
194             if ( ( filter == null ) || filter.include( childArtifact ) )
195             {
196                 nodes.add( buildDependencyNode( current, child, childArtifact, filter ) );
197             }
198         }
199 
200         current.setChildren( Collections.unmodifiableList( nodes ) );
201 
202         return current;
203     }
204 
205     private String getVersionSelectedFromRange( VersionConstraint constraint )
206     {
207         if ( ( constraint == null ) || ( constraint.getVersion() != null ) )
208         {
209             return null;
210         }
211 
212         return constraint.getRange().toString();
213     }
214 }