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.metadata.ArtifactMetadata; |
25 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
26 | |
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; |
27 | |
import org.apache.maven.model.Model; |
28 | |
import org.apache.maven.model.Parent; |
29 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
30 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
31 | |
import org.apache.maven.plugin.MojoExecutionException; |
32 | |
import org.apache.maven.plugin.MojoFailureException; |
33 | |
import org.apache.maven.project.MavenProject; |
34 | |
import org.apache.maven.project.MavenProjectHelper; |
35 | |
import org.apache.maven.project.artifact.ProjectArtifactMetadata; |
36 | |
import org.apache.maven.project.validation.ModelValidationResult; |
37 | |
import org.apache.maven.project.validation.ModelValidator; |
38 | |
import org.codehaus.plexus.util.FileUtils; |
39 | |
import org.codehaus.plexus.util.IOUtil; |
40 | |
import org.codehaus.plexus.util.ReaderFactory; |
41 | |
import org.codehaus.plexus.util.StringUtils; |
42 | |
import org.codehaus.plexus.util.WriterFactory; |
43 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
44 | |
|
45 | |
import java.io.File; |
46 | |
import java.io.FileNotFoundException; |
47 | |
import java.io.IOException; |
48 | |
import java.io.Reader; |
49 | |
import java.io.Writer; |
50 | |
import java.util.Iterator; |
51 | |
import java.util.List; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 13 | public class DeployFileMojo |
61 | |
extends AbstractDeployMojo |
62 | |
{ |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private MavenProject project; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private MavenProjectHelper projectHelper; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private String groupId; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
private String artifactId; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private String version; |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private String packaging; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private String description; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private File file; |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
private File javadoc; |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private File sources; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
private String repositoryId; |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
private String repositoryLayout; |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
private String url; |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
private File pomFile; |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
private boolean generatePom; |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
private String classifier; |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
private boolean uniqueVersion; |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
private ModelValidator modelValidator; |
201 | |
|
202 | |
void initProperties() |
203 | |
throws MojoExecutionException |
204 | |
{ |
205 | |
|
206 | 13 | if ( pomFile != null ) |
207 | |
{ |
208 | 8 | generatePom = false; |
209 | |
|
210 | 8 | Model model = readModel( pomFile ); |
211 | |
|
212 | 8 | processModel( model ); |
213 | |
} |
214 | |
|
215 | 13 | if ( packaging == null && file != null ) |
216 | |
{ |
217 | 0 | packaging = FileUtils.getExtension( file.getName() ); |
218 | |
} |
219 | 13 | } |
220 | |
|
221 | |
public void execute() |
222 | |
throws MojoExecutionException, MojoFailureException |
223 | |
{ |
224 | 6 | failIfOffline(); |
225 | |
|
226 | 6 | if ( !file.exists() ) |
227 | |
{ |
228 | 0 | throw new MojoExecutionException( file.getPath() + " not found." ); |
229 | |
} |
230 | |
|
231 | 6 | initProperties(); |
232 | |
|
233 | 6 | validateArtifactInformation(); |
234 | |
|
235 | 6 | ArtifactRepositoryLayout layout = getLayout( repositoryLayout ); |
236 | |
|
237 | 6 | ArtifactRepository deploymentRepository = |
238 | |
repositoryFactory.createDeploymentArtifactRepository( repositoryId, url, layout, uniqueVersion ); |
239 | |
|
240 | 6 | String protocol = deploymentRepository.getProtocol(); |
241 | |
|
242 | 6 | if ( StringUtils.isEmpty( protocol ) ) |
243 | |
{ |
244 | 0 | throw new MojoExecutionException( "No transfer protocol found." ); |
245 | |
} |
246 | |
|
247 | |
|
248 | 6 | Artifact artifact = |
249 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier ); |
250 | |
|
251 | 6 | if ( file.equals( getLocalRepoFile( artifact ) ) ) |
252 | |
{ |
253 | 0 | throw new MojoFailureException( "Cannot deploy artifact from the local repository: " + file ); |
254 | |
} |
255 | |
|
256 | |
|
257 | 6 | if ( !"pom".equals( packaging ) ) |
258 | |
{ |
259 | 6 | if ( pomFile != null ) |
260 | |
{ |
261 | 1 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile ); |
262 | 1 | artifact.addMetadata( metadata ); |
263 | 1 | } |
264 | 5 | else if ( generatePom ) |
265 | |
{ |
266 | 5 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, generatePomFile() ); |
267 | 5 | artifact.addMetadata( metadata ); |
268 | |
} |
269 | |
} |
270 | |
|
271 | 6 | if ( updateReleaseInfo ) |
272 | |
{ |
273 | 0 | artifact.setRelease( true ); |
274 | |
} |
275 | |
|
276 | 6 | project.setArtifact( artifact ); |
277 | |
|
278 | |
try |
279 | |
{ |
280 | 6 | getDeployer().deploy( file, artifact, deploymentRepository, getLocalRepository() ); |
281 | |
} |
282 | 0 | catch ( ArtifactDeploymentException e ) |
283 | |
{ |
284 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
285 | 6 | } |
286 | |
|
287 | 6 | if ( sources != null ) |
288 | |
{ |
289 | 0 | projectHelper.attachArtifact( project, "jar", "sources", sources ); |
290 | |
} |
291 | |
|
292 | 6 | if ( javadoc != null ) |
293 | |
{ |
294 | 0 | projectHelper.attachArtifact( project, "jar", "javadoc", javadoc ); |
295 | |
} |
296 | |
|
297 | 6 | List attachedArtifacts = project.getAttachedArtifacts(); |
298 | |
|
299 | 6 | for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); ) |
300 | |
{ |
301 | 0 | Artifact attached = ( Artifact ) i.next(); |
302 | |
|
303 | |
try |
304 | |
{ |
305 | 0 | getDeployer().deploy( attached.getFile(), attached, deploymentRepository, getLocalRepository() ); |
306 | |
} |
307 | 0 | catch ( ArtifactDeploymentException e ) |
308 | |
{ |
309 | 0 | throw new MojoExecutionException( "Error deploying attached artifact " + attached.getFile() + ": " + e.getMessage(), e ); |
310 | 0 | } |
311 | 0 | } |
312 | |
|
313 | 6 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
private File getLocalRepoFile( Artifact artifact ) |
323 | |
{ |
324 | 6 | String path = getLocalRepository().pathOf( artifact ); |
325 | 6 | return new File( getLocalRepository().getBasedir(), path ); |
326 | |
} |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
private void processModel( Model model ) |
334 | |
{ |
335 | 8 | Parent parent = model.getParent(); |
336 | |
|
337 | 8 | if ( this.groupId == null ) |
338 | |
{ |
339 | 6 | this.groupId = model.getGroupId(); |
340 | 6 | if ( this.groupId == null && parent != null ) |
341 | |
{ |
342 | 4 | this.groupId = parent.getGroupId(); |
343 | |
} |
344 | |
} |
345 | 8 | if ( this.artifactId == null ) |
346 | |
{ |
347 | 6 | this.artifactId = model.getArtifactId(); |
348 | |
} |
349 | 8 | if ( this.version == null ) |
350 | |
{ |
351 | 6 | this.version = model.getVersion(); |
352 | 6 | if ( this.version == null && parent != null ) |
353 | |
{ |
354 | 2 | this.version = parent.getVersion(); |
355 | |
} |
356 | |
} |
357 | 8 | if ( this.packaging == null ) |
358 | |
{ |
359 | 6 | this.packaging = model.getPackaging(); |
360 | |
} |
361 | 8 | } |
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
Model readModel( File pomFile ) |
371 | |
throws MojoExecutionException |
372 | |
{ |
373 | 2 | Reader reader = null; |
374 | |
try |
375 | |
{ |
376 | 2 | reader = ReaderFactory.newXmlReader( pomFile ); |
377 | 2 | return new MavenXpp3Reader().read( reader ); |
378 | |
} |
379 | 0 | catch ( FileNotFoundException e ) |
380 | |
{ |
381 | 0 | throw new MojoExecutionException( "POM not found " + pomFile, e ); |
382 | |
} |
383 | 0 | catch ( IOException e ) |
384 | |
{ |
385 | 0 | throw new MojoExecutionException( "Error reading POM " + pomFile, e ); |
386 | |
} |
387 | 0 | catch ( XmlPullParserException e ) |
388 | |
{ |
389 | 0 | throw new MojoExecutionException( "Error parsing POM " + pomFile, e ); |
390 | |
} |
391 | |
finally |
392 | |
{ |
393 | 2 | IOUtil.close( reader ); |
394 | |
} |
395 | |
} |
396 | |
|
397 | |
private File generatePomFile() |
398 | |
throws MojoExecutionException |
399 | |
{ |
400 | 5 | Model model = generateModel(); |
401 | |
|
402 | 5 | Writer fw = null; |
403 | |
try |
404 | |
{ |
405 | 5 | File tempFile = File.createTempFile( "mvndeploy", ".pom" ); |
406 | 5 | tempFile.deleteOnExit(); |
407 | |
|
408 | 5 | fw = WriterFactory.newXmlWriter( tempFile ); |
409 | 5 | new MavenXpp3Writer().write( fw, model ); |
410 | |
|
411 | 5 | return tempFile; |
412 | |
} |
413 | 0 | catch ( IOException e ) |
414 | |
{ |
415 | 0 | throw new MojoExecutionException( "Error writing temporary pom file: " + e.getMessage(), e ); |
416 | |
} |
417 | |
finally |
418 | |
{ |
419 | 5 | IOUtil.close( fw ); |
420 | |
} |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
private void validateArtifactInformation() |
429 | |
throws MojoExecutionException |
430 | |
{ |
431 | 6 | Model model = generateModel(); |
432 | |
|
433 | 6 | ModelValidationResult result = modelValidator.validate( model ); |
434 | |
|
435 | 6 | if ( result.getMessageCount() > 0 ) |
436 | |
{ |
437 | 0 | throw new MojoExecutionException( "The artifact information is incomplete or not valid:\n" |
438 | |
+ result.render( " " ) ); |
439 | |
} |
440 | 6 | } |
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
private Model generateModel() |
448 | |
{ |
449 | 11 | Model model = new Model(); |
450 | |
|
451 | 11 | model.setModelVersion( "4.0.0" ); |
452 | |
|
453 | 11 | model.setGroupId( groupId ); |
454 | 11 | model.setArtifactId( artifactId ); |
455 | 11 | model.setVersion( version ); |
456 | 11 | model.setPackaging( packaging ); |
457 | |
|
458 | 11 | model.setDescription( description ); |
459 | |
|
460 | 11 | return model; |
461 | |
} |
462 | |
|
463 | |
void setGroupId( String groupId ) |
464 | |
{ |
465 | 1 | this.groupId = groupId; |
466 | 1 | } |
467 | |
|
468 | |
void setArtifactId( String artifactId ) |
469 | |
{ |
470 | 1 | this.artifactId = artifactId; |
471 | 1 | } |
472 | |
|
473 | |
void setVersion( String version ) |
474 | |
{ |
475 | 1 | this.version = version; |
476 | 1 | } |
477 | |
|
478 | |
void setPackaging( String packaging ) |
479 | |
{ |
480 | 1 | this.packaging = packaging; |
481 | 1 | } |
482 | |
|
483 | |
void setPomFile( File pomFile ) |
484 | |
{ |
485 | 7 | this.pomFile = pomFile; |
486 | 7 | } |
487 | |
|
488 | |
String getGroupId() |
489 | |
{ |
490 | 7 | return groupId; |
491 | |
} |
492 | |
|
493 | |
String getArtifactId() |
494 | |
{ |
495 | 7 | return artifactId; |
496 | |
} |
497 | |
|
498 | |
String getVersion() |
499 | |
{ |
500 | 7 | return version; |
501 | |
} |
502 | |
|
503 | |
String getPackaging() |
504 | |
{ |
505 | 7 | return packaging; |
506 | |
} |
507 | |
|
508 | |
File getFile() |
509 | |
{ |
510 | 0 | return file; |
511 | |
} |
512 | |
|
513 | |
String getClassifier() |
514 | |
{ |
515 | 2 | return classifier; |
516 | |
} |
517 | |
|
518 | |
void setClassifier( String classifier ) |
519 | |
{ |
520 | 1 | this.classifier = classifier; |
521 | 1 | } |
522 | |
} |