1 | |
package org.apache.maven.plugin.deploy; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.deployer.ArtifactDeploymentException; |
24 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
25 | |
import org.apache.maven.artifact.metadata.ArtifactMetadata; |
26 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
27 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; |
28 | |
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; |
29 | |
import org.apache.maven.model.Model; |
30 | |
import org.apache.maven.model.Parent; |
31 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
32 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
33 | |
import org.apache.maven.plugin.MojoExecutionException; |
34 | |
import org.apache.maven.project.artifact.ProjectArtifactMetadata; |
35 | |
import org.codehaus.plexus.util.IOUtil; |
36 | |
import org.codehaus.plexus.util.ReaderFactory; |
37 | |
import org.codehaus.plexus.util.WriterFactory; |
38 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
39 | |
|
40 | |
import java.io.File; |
41 | |
import java.io.FileNotFoundException; |
42 | |
import java.io.IOException; |
43 | |
import java.io.Reader; |
44 | |
import java.io.Writer; |
45 | |
import java.util.Map; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 13 | public class DeployFileMojo |
55 | |
extends AbstractDeployMojo |
56 | |
{ |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private String groupId; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private String artifactId; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private String version; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private String packaging; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private String description; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private File file; |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private String repositoryId; |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
private String repositoryLayout; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
private Map repositoryLayouts; |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private String url; |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
private ArtifactFactory artifactFactory; |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
private ArtifactRepositoryFactory repositoryFactory; |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
private File pomFile; |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
private boolean generatePom; |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
private String classifier; |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private boolean uniqueVersion; |
177 | |
|
178 | |
protected void initProperties() |
179 | |
throws MojoExecutionException |
180 | |
{ |
181 | |
|
182 | 13 | if ( pomFile != null ) |
183 | |
{ |
184 | 8 | generatePom = false; |
185 | |
|
186 | 8 | Model model = readModel( pomFile ); |
187 | |
|
188 | 8 | processModel( model ); |
189 | |
} |
190 | |
|
191 | |
|
192 | 13 | if ( groupId == null || artifactId == null || version == null || packaging == null ) |
193 | |
{ |
194 | 3 | throw new MojoExecutionException( "Missing group, artifact, version, or packaging information" ); |
195 | |
} |
196 | 10 | } |
197 | |
|
198 | |
public void execute() |
199 | |
throws MojoExecutionException |
200 | |
{ |
201 | 6 | initProperties(); |
202 | |
|
203 | 6 | if ( !file.exists() ) |
204 | |
{ |
205 | 0 | throw new MojoExecutionException( file.getPath() + " not found." ); |
206 | |
} |
207 | |
|
208 | |
ArtifactRepositoryLayout layout; |
209 | |
|
210 | 6 | layout = ( ArtifactRepositoryLayout ) repositoryLayouts.get( repositoryLayout ); |
211 | |
|
212 | 6 | ArtifactRepository deploymentRepository = |
213 | |
repositoryFactory.createDeploymentArtifactRepository( repositoryId, url, layout, uniqueVersion ); |
214 | |
|
215 | 6 | String protocol = deploymentRepository.getProtocol(); |
216 | |
|
217 | 6 | if ( "".equals( protocol ) || protocol == null ) |
218 | |
{ |
219 | 0 | throw new MojoExecutionException( "No transfer protocol found." ); |
220 | |
} |
221 | |
|
222 | |
|
223 | 6 | Artifact artifact = |
224 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier ); |
225 | |
|
226 | |
|
227 | 6 | if ( generatePom ) |
228 | |
{ |
229 | 5 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, generatePomFile() ); |
230 | 5 | artifact.addMetadata( metadata ); |
231 | 5 | } |
232 | |
else |
233 | |
{ |
234 | 1 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile ); |
235 | 1 | artifact.addMetadata( metadata ); |
236 | |
} |
237 | |
|
238 | |
try |
239 | |
{ |
240 | 6 | getDeployer().deploy( file, artifact, deploymentRepository, getLocalRepository() ); |
241 | |
} |
242 | 0 | catch ( ArtifactDeploymentException e ) |
243 | |
{ |
244 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
245 | 6 | } |
246 | 6 | } |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
private void processModel( Model model ) |
254 | |
{ |
255 | 8 | Parent parent = model.getParent(); |
256 | |
|
257 | 8 | if ( this.groupId == null ) |
258 | |
{ |
259 | 6 | if ( parent != null && parent.getGroupId() != null ) |
260 | |
{ |
261 | 5 | this.groupId = parent.getGroupId(); |
262 | |
} |
263 | 6 | if ( model.getGroupId() != null ) |
264 | |
{ |
265 | 2 | this.groupId = model.getGroupId(); |
266 | |
} |
267 | |
} |
268 | 8 | if ( this.artifactId == null && model.getArtifactId() != null ) |
269 | |
{ |
270 | 5 | this.artifactId = model.getArtifactId(); |
271 | |
} |
272 | 8 | if ( this.version == null ) |
273 | |
{ |
274 | 6 | this.version = model.getVersion(); |
275 | 6 | if ( this.version == null && parent != null ) |
276 | |
{ |
277 | 2 | this.version = parent.getVersion(); |
278 | |
} |
279 | |
} |
280 | 8 | if ( this.packaging == null && model.getPackaging() != null ) |
281 | |
{ |
282 | 3 | this.packaging = model.getPackaging(); |
283 | |
} |
284 | 8 | } |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
protected Model readModel( File pomFile ) |
294 | |
throws MojoExecutionException |
295 | |
{ |
296 | |
|
297 | 2 | if ( !pomFile.exists() ) |
298 | |
{ |
299 | 0 | throw new MojoExecutionException( "Specified pomFile does not exist" ); |
300 | |
} |
301 | |
|
302 | 2 | Reader reader = null; |
303 | |
try |
304 | |
{ |
305 | 2 | reader = ReaderFactory.newXmlReader( pomFile ); |
306 | 2 | MavenXpp3Reader modelReader = new MavenXpp3Reader(); |
307 | 2 | return modelReader.read( reader ); |
308 | |
} |
309 | 0 | catch ( FileNotFoundException e ) |
310 | |
{ |
311 | 0 | throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e ); |
312 | |
} |
313 | 0 | catch ( IOException e ) |
314 | |
{ |
315 | 0 | throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e ); |
316 | |
} |
317 | 0 | catch ( XmlPullParserException e ) |
318 | |
{ |
319 | 0 | throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e ); |
320 | |
} |
321 | |
finally |
322 | |
{ |
323 | 2 | IOUtil.close( reader ); |
324 | |
} |
325 | |
} |
326 | |
|
327 | |
private File generatePomFile() |
328 | |
throws MojoExecutionException |
329 | |
{ |
330 | 5 | Writer fw = null; |
331 | |
try |
332 | |
{ |
333 | 5 | File tempFile = File.createTempFile( "mvninstall", ".pom" ); |
334 | 5 | tempFile.deleteOnExit(); |
335 | |
|
336 | 5 | Model model = new Model(); |
337 | 5 | model.setModelVersion( "4.0.0" ); |
338 | 5 | model.setGroupId( groupId ); |
339 | 5 | model.setArtifactId( artifactId ); |
340 | 5 | model.setVersion( version ); |
341 | 5 | model.setPackaging( packaging ); |
342 | 5 | model.setDescription( description ); |
343 | |
|
344 | 5 | fw = WriterFactory.newXmlWriter( tempFile ); |
345 | 5 | new MavenXpp3Writer().write( fw, model ); |
346 | |
|
347 | 5 | return tempFile; |
348 | |
} |
349 | 0 | catch ( IOException e ) |
350 | |
{ |
351 | 0 | throw new MojoExecutionException( "Error writing temporary pom file: " + e.getMessage(), e ); |
352 | |
} |
353 | |
finally |
354 | |
{ |
355 | 5 | IOUtil.close( fw ); |
356 | |
} |
357 | |
} |
358 | |
|
359 | |
void setGroupId( String groupId ) |
360 | |
{ |
361 | 1 | this.groupId = groupId; |
362 | 1 | } |
363 | |
|
364 | |
void setArtifactId( String artifactId ) |
365 | |
{ |
366 | 1 | this.artifactId = artifactId; |
367 | 1 | } |
368 | |
|
369 | |
void setVersion( String version ) |
370 | |
{ |
371 | 1 | this.version = version; |
372 | 1 | } |
373 | |
|
374 | |
void setPackaging( String packaging ) |
375 | |
{ |
376 | 1 | this.packaging = packaging; |
377 | 1 | } |
378 | |
|
379 | |
void setPomFile( File pomFile ) |
380 | |
{ |
381 | 7 | this.pomFile = pomFile; |
382 | 7 | } |
383 | |
|
384 | |
String getGroupId() |
385 | |
{ |
386 | 7 | return groupId; |
387 | |
} |
388 | |
|
389 | |
String getArtifactId() |
390 | |
{ |
391 | 7 | return artifactId; |
392 | |
} |
393 | |
|
394 | |
String getVersion() |
395 | |
{ |
396 | 7 | return version; |
397 | |
} |
398 | |
|
399 | |
String getPackaging() |
400 | |
{ |
401 | 7 | return packaging; |
402 | |
} |
403 | |
|
404 | |
File getFile() |
405 | |
{ |
406 | 0 | return file; |
407 | |
} |
408 | |
|
409 | |
String getClassifier() |
410 | |
{ |
411 | 2 | return classifier; |
412 | |
} |
413 | |
|
414 | |
void setClassifier( String classifier ) |
415 | |
{ |
416 | 1 | this.classifier = classifier; |
417 | 1 | } |
418 | |
} |