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