1
2
3
4
5
6
7
8
9
10
11 package com.simpligility.maven.plugins.android.common;
12
13 import org.apache.maven.artifact.Artifact;
14 import org.apache.maven.artifact.repository.ArtifactRepository;
15 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
16 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
17 import org.apache.maven.artifact.resolver.ArtifactResolver;
18 import org.apache.maven.plugin.MojoExecutionException;
19 import org.codehaus.plexus.logging.Logger;
20
21 import java.io.File;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Set;
28
29
30
31
32 public final class ArtifactResolverHelper
33 {
34
35
36
37 public static final List<String> EXCLUDE_NON_PACKAGED_SCOPES = Arrays.asList(
38 Artifact.SCOPE_PROVIDED, Artifact.SCOPE_IMPORT
39 );
40
41 private final ArtifactResolver artifactResolver;
42 private final Logger log;
43 private final List<ArtifactRepository> remoteArtifactRepositories;
44
45
46
47
48 public ArtifactResolverHelper( ArtifactResolver artifactResolver, Logger log )
49 {
50 this( artifactResolver, log, Collections.<ArtifactRepository>emptyList() );
51 }
52
53 public ArtifactResolverHelper( ArtifactResolver artifactResolver, Logger log,
54 final List<ArtifactRepository> remoteArtifactRepositories )
55 {
56 this.artifactResolver = artifactResolver;
57 this.log = log;
58 this.remoteArtifactRepositories = remoteArtifactRepositories;
59 }
60
61
62
63
64
65
66
67
68
69
70
71 public Set<Artifact> getFilteredArtifacts( Iterable<Artifact> allArtifacts, String... types )
72 {
73 return getFilteredArtifacts( EXCLUDE_NON_PACKAGED_SCOPES, allArtifacts, types );
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public Set<Artifact> getFilteredArtifacts( List<String> filteredScopes,
87 Iterable<Artifact> allArtifacts, String... types )
88 {
89 final List<String> acceptTypeList = Arrays.asList( types );
90 boolean acceptAllArtifacts = acceptTypeList.isEmpty();
91 final Set<Artifact> results = new LinkedHashSet<Artifact>();
92 for ( Artifact artifact : allArtifacts )
93 {
94 if ( artifact == null )
95 {
96 continue;
97 }
98
99 if ( filteredScopes.contains( artifact.getScope() ) )
100 {
101 continue;
102 }
103
104 if ( acceptAllArtifacts || acceptTypeList.contains( artifact.getType() ) )
105 {
106 results.add( artifact );
107 }
108 }
109 return results;
110 }
111
112
113
114
115
116
117
118
119 public File resolveArtifactToFile( Artifact artifact ) throws MojoExecutionException
120 {
121 final Artifact resolvedArtifact = resolveArtifact( artifact );
122 final File jar = resolvedArtifact.getFile();
123 if ( jar == null )
124 {
125 throw new MojoExecutionException( "Could not resolve artifact " + artifact.getId()
126 + ". Please install it with \"mvn install:install-file ...\" or deploy it to a repository "
127 + "with \"mvn deploy:deploy-file ...\"" );
128 }
129 return jar;
130 }
131
132 public Set<Artifact> resolveArtifacts( Collection<Artifact> artifacts ) throws MojoExecutionException
133 {
134 final Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
135 for ( final Artifact artifact : artifacts )
136 {
137 resolvedArtifacts.add( resolveArtifact( artifact ) );
138 }
139 return resolvedArtifacts;
140 }
141
142
143
144
145
146
147
148 private Artifact resolveArtifact( Artifact artifact ) throws MojoExecutionException
149 {
150 final ArtifactResolutionRequest artifactResolutionRequest = new ArtifactResolutionRequest();
151 artifactResolutionRequest.setArtifact( artifact );
152 if ( remoteArtifactRepositories != null && !remoteArtifactRepositories.isEmpty() )
153 {
154 artifactResolutionRequest.setRemoteRepositories( remoteArtifactRepositories );
155 }
156 final ArtifactResolutionResult resolutionResult = this.artifactResolver.resolve( artifactResolutionRequest );
157
158 log.debug( "Resolving : " + artifact );
159 if ( resolutionResult.getArtifacts().size() == 0 )
160 {
161 throw new MojoExecutionException( "Could not resolve artifact " + artifact
162 + ". Please install it with \"mvn install:install-file ...\" or deploy it to a repository "
163 + "with \"mvn deploy:deploy-file ...\"" );
164 }
165 if ( resolutionResult.getArtifacts().size() > 1 )
166 {
167 log.debug( "Resolved artifacts : " + resolutionResult.getArtifacts() );
168 throw new MojoExecutionException( "Could not resolve artifact " + artifact
169 + " to single target. Found the following possible options : " + resolutionResult.getArtifacts() );
170 }
171
172 final Artifact resolvedArtifact = resolutionResult.getArtifacts().iterator().next();
173 log.debug( "Resolved : " + resolvedArtifact );
174 return resolvedArtifact;
175 }
176 }