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
25
26
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
41
42
43
44
45 public Set<Artifact> getProjectDependenciesFor( MavenProject project, MavenSession session )
46 throws DependencyGraphBuilderException
47 {
48
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
58
59
60
61
62
63
64
65
66
67 public Set<Artifact> getLibraryDependenciesFor( MavenSession session,
68 RepositorySystem repositorySystem,
69 Artifact artifact )
70 throws MojoExecutionException
71 {
72
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 );
88 request.setResolveTransitively( true );
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 }