1 package org.apache.maven.plugin.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
32 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
33 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34 import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
35 import org.apache.maven.artifact.resolver.ArtifactResolver;
36 import org.apache.maven.plugin.AbstractMojo;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.codehaus.plexus.util.StringUtils;
40
41
42
43
44
45
46
47
48 public class GetMojo
49 extends AbstractMojo
50 {
51
52
53
54
55
56 private ArtifactFactory artifactFactory;
57
58
59
60
61
62 private ArtifactResolver artifactResolver;
63
64
65
66
67
68 private ArtifactRepositoryFactory artifactRepositoryFactory;
69
70
71
72
73 private ArtifactRepositoryLayout repositoryLayout;
74
75
76
77
78
79 private ArtifactMetadataSource source;
80
81
82
83
84
85
86 private ArtifactRepository localRepository;
87
88
89
90
91
92 private String groupId;
93
94
95
96
97
98 private String artifactId;
99
100
101
102
103
104 private String version;
105
106
107
108
109
110
111 private String classifier;
112
113
114
115
116
117 private String packaging = "jar";
118
119
120
121
122
123 private String repositoryId = "temp";
124
125
126
127
128
129
130 private String repositoryUrl;
131
132
133
134
135
136 private String remoteRepositories;
137
138
139
140
141
142 private String artifact;
143
144
145
146
147
148
149
150 private List pomRemoteRepositories;
151
152
153
154
155
156 private boolean transitive = true;
157
158 public void execute()
159 throws MojoExecutionException, MojoFailureException
160 {
161
162 if ( artifactId == null && artifact == null )
163 {
164 throw new MojoFailureException( "You must specify an artifact, "
165 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
166 }
167 if ( artifactId == null )
168 {
169 String[] tokens = StringUtils.split( artifact, ":" );
170 if ( tokens.length < 3 && tokens.length > 5 )
171 {
172 throw new MojoFailureException(
173 "Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] "
174 + artifact );
175 }
176 groupId = tokens[0];
177 artifactId = tokens[1];
178 version = tokens[2];
179 if ( tokens.length >= 4 )
180 {
181 packaging = tokens[3];
182 }
183 if ( tokens.length == 5 )
184 {
185 classifier = tokens[4];
186 }
187 else
188 {
189 classifier = null;
190 }
191 }
192
193 Artifact toDownload = classifier == null
194 ? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging )
195 : artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
196 Artifact dummyOriginatingArtifact =
197 artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );
198
199 ArtifactRepositoryPolicy always =
200 new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
201 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
202 ArtifactRepository remoteRepo =
203 artifactRepositoryFactory.createArtifactRepository( repositoryId, repositoryUrl, repositoryLayout, always,
204 always );
205
206 if ( pomRemoteRepositories == null )
207 {
208 pomRemoteRepositories = new ArrayList();
209 }
210
211 List repoList = new ArrayList( pomRemoteRepositories );
212 if ( remoteRepositories != null )
213 {
214
215 repoList.addAll( Arrays.asList( StringUtils.split( remoteRepositories, "," ) ) );
216 }
217
218 repoList.add( remoteRepo );
219
220 try
221 {
222 if ( transitive )
223 {
224 artifactResolver.resolveTransitively( Collections.singleton( toDownload ), dummyOriginatingArtifact,
225 repoList, localRepository, source );
226 }
227 else
228 {
229 artifactResolver.resolve( toDownload, repoList, localRepository );
230 }
231
232 }
233 catch ( AbstractArtifactResolutionException e )
234 {
235 throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
236 }
237 }
238 }