View Javadoc
1   package com.simpligility.maven.plugins.android.common;
2   
3   import org.apache.maven.artifact.Artifact;
4   import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
5   import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
6   import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
7   import org.apache.maven.execution.MavenSession;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.project.MavenProject;
10  import org.apache.maven.repository.RepositorySystem;
11  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
12  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
13  import org.apache.maven.shared.dependency.graph.DependencyNode;
14  import org.codehaus.plexus.logging.Logger;
15  
16  import java.util.HashSet;
17  import java.util.Set;
18  
19  import static com.simpligility.maven.plugins.android.common.AndroidExtension.AAR;
20  import static com.simpligility.maven.plugins.android.common.AndroidExtension.APK;
21  import static com.simpligility.maven.plugins.android.common.AndroidExtension.APKLIB;
22  
23  /**
24   * Resolves the aar and apklib dependencies for an Artifact.
25   *
26   * @author William Ferguson - william.ferguson@xandar.com.au
27   */
28  public final class DependencyResolver
29  {
30      private final Logger log;
31      private final DependencyGraphBuilder dependencyGraphBuilder;
32  
33      public DependencyResolver( Logger log, DependencyGraphBuilder dependencyGraphBuilder )
34      {
35          this.log = log;
36          this.dependencyGraphBuilder = dependencyGraphBuilder;
37      }
38  
39      /**
40       * @param project   MavenProject for which to return the dependencies.
41       * @param session   MavenSession in which to look for reactor dependencies.
42       * @return all the dependencies for a project.
43       * @throws DependencyGraphBuilderException if the dependency graph can't be built.
44       */
45      public Set<Artifact> getProjectDependenciesFor( MavenProject project, MavenSession session )
46              throws DependencyGraphBuilderException
47      {
48          // No need to filter our search. We want to resolve all artifacts.
49          final DependencyNode node = dependencyGraphBuilder.buildDependencyGraph( project, null, session.getProjects() );
50  
51          final DependencyCollector collector = new DependencyCollector( log, project.getArtifact() );
52          collector.visit( node, false );
53          return collector.getDependencies();
54      }
55  
56      /**
57       * Returns the Set of APKLIB, AAR, APK (direct or transitive) dependencies of the supplied artifact.
58       *
59       * The project is searched until artifact is found and then the library dependencies are looked for recursively.
60       *
61       * @param session           MavenSession in which to resolve the artifacts.
62       * @param repositorySystem  RepositorySystem with which to resolve the artifacts.
63       * @param artifact          Artifact for whom to get the dependencies.
64       * @return Set of APK, APKLIB and AAR dependencies.
65       * @throws org.apache.maven.plugin.MojoExecutionException if it couldn't resolve any of the dependencies.
66       */
67      public Set<Artifact> getLibraryDependenciesFor( MavenSession session,
68                                                      RepositorySystem repositorySystem,
69                                                      Artifact artifact )
70              throws MojoExecutionException
71      {
72          // Set a filter that should only return interesting artifacts.
73          final ArtifactFilter filter = new ArtifactFilter()
74          {
75              @Override
76              public boolean include( Artifact found )
77              {
78                  final String type = found.getType();
79                  return ( type.equals( APKLIB ) || type.equals( AAR ) || type.equals( APK ) );
80              }
81          };
82  
83          log.debug( "MavenSession = " + session + "  repositorySystem = " + repositorySystem );
84  
85          final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
86          request.setArtifact( artifact );
87          request.setResolveRoot( false );        // Don't include source artifact in result
88          request.setResolveTransitively( true ); // Include direct plus transitive dependencies.
89          request.setServers( session.getRequest().getServers() );
90          request.setMirrors( session.getRequest().getMirrors() );
91          request.setProxies( session.getRequest().getProxies() );
92          request.setLocalRepository( session.getLocalRepository() );
93          request.setRemoteRepositories( session.getCurrentProject().getRemoteArtifactRepositories() );
94  
95          final ArtifactResolutionResult result = repositorySystem.resolve( request );
96  
97          final Set<Artifact> libraryDeps = new HashSet<Artifact>();
98          for ( final Artifact depArtifact : result.getArtifacts() )
99          {
100             if ( filter.include( depArtifact ) )
101             {
102                 libraryDeps.add( depArtifact );
103             }
104         }
105 
106         return libraryDeps;
107     }
108 }