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.lang.reflect.Type; |
51 | |
import java.util.Iterator; |
52 | |
import java.util.List; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 13 | public class DeployFileMojo |
63 | |
extends AbstractDeployMojo |
64 | |
{ |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private MavenProject project; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private MavenProjectHelper projectHelper; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private String groupId; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private String artifactId; |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private String version; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private String packaging; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
private String description; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
private File file; |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private File javadoc; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
private File sources; |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
private String repositoryId; |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private String repositoryLayout; |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
private String url; |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private File pomFile; |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
private boolean generatePom; |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private String classifier; |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private boolean uniqueVersion; |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
private ModelValidator modelValidator; |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
private String types; |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
private String classifiers; |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
private String files; |
231 | |
|
232 | |
void initProperties() |
233 | |
throws MojoExecutionException |
234 | |
{ |
235 | |
|
236 | 13 | if ( pomFile != null ) |
237 | |
{ |
238 | 8 | generatePom = false; |
239 | |
|
240 | 8 | Model model = readModel( pomFile ); |
241 | |
|
242 | 8 | processModel( model ); |
243 | |
} |
244 | |
|
245 | 13 | if ( packaging == null && file != null ) |
246 | |
{ |
247 | 0 | packaging = FileUtils.getExtension( file.getName() ); |
248 | |
} |
249 | 13 | } |
250 | |
|
251 | |
public void execute() |
252 | |
throws MojoExecutionException, MojoFailureException |
253 | |
{ |
254 | 6 | failIfOffline(); |
255 | |
|
256 | 6 | if ( !file.exists() ) |
257 | |
{ |
258 | 0 | throw new MojoExecutionException( file.getPath() + " not found." ); |
259 | |
} |
260 | |
|
261 | 6 | initProperties(); |
262 | |
|
263 | 6 | validateArtifactInformation(); |
264 | |
|
265 | 6 | ArtifactRepositoryLayout layout = getLayout( repositoryLayout ); |
266 | |
|
267 | 6 | ArtifactRepository deploymentRepository = |
268 | |
repositoryFactory.createDeploymentArtifactRepository( repositoryId, url, layout, uniqueVersion ); |
269 | |
|
270 | 6 | String protocol = deploymentRepository.getProtocol(); |
271 | |
|
272 | 6 | if ( StringUtils.isEmpty( protocol ) ) |
273 | |
{ |
274 | 0 | throw new MojoExecutionException( "No transfer protocol found." ); |
275 | |
} |
276 | |
|
277 | |
|
278 | 6 | Artifact artifact = |
279 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier ); |
280 | |
|
281 | 6 | if ( file.equals( getLocalRepoFile( artifact ) ) ) |
282 | |
{ |
283 | 0 | throw new MojoFailureException( "Cannot deploy artifact from the local repository: " + file ); |
284 | |
} |
285 | |
|
286 | |
|
287 | 6 | if ( !"pom".equals( packaging ) ) |
288 | |
{ |
289 | 6 | if ( pomFile != null ) |
290 | |
{ |
291 | 1 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile ); |
292 | 1 | artifact.addMetadata( metadata ); |
293 | 1 | } |
294 | 5 | else if ( generatePom ) |
295 | |
{ |
296 | 5 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, generatePomFile() ); |
297 | 5 | artifact.addMetadata( metadata ); |
298 | |
} |
299 | |
} |
300 | |
|
301 | 6 | if ( updateReleaseInfo ) |
302 | |
{ |
303 | 0 | artifact.setRelease( true ); |
304 | |
} |
305 | |
|
306 | 6 | project.setArtifact( artifact ); |
307 | |
|
308 | |
try |
309 | |
{ |
310 | 6 | deploy( file, artifact, deploymentRepository, getLocalRepository() ); |
311 | |
} |
312 | 0 | catch ( ArtifactDeploymentException e ) |
313 | |
{ |
314 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
315 | 6 | } |
316 | |
|
317 | 6 | if ( sources != null ) |
318 | |
{ |
319 | 0 | projectHelper.attachArtifact( project, "jar", "sources", sources ); |
320 | |
} |
321 | |
|
322 | 6 | if ( javadoc != null ) |
323 | |
{ |
324 | 0 | projectHelper.attachArtifact( project, "jar", "javadoc", javadoc ); |
325 | |
} |
326 | |
|
327 | 6 | if ( files != null ) |
328 | |
{ |
329 | 0 | if ( types == null ) |
330 | |
{ |
331 | 0 | throw new MojoExecutionException( "You must specify 'types' if you specify 'files'" ); |
332 | |
} |
333 | 0 | if ( classifiers == null ) |
334 | |
{ |
335 | 0 | throw new MojoExecutionException( "You must specify 'classifiers' if you specify 'files'" ); |
336 | |
} |
337 | 0 | int filesLength = StringUtils.countMatches( files, "," ); |
338 | 0 | int typesLength = StringUtils.countMatches( types, "," ); |
339 | 0 | int classifiersLength = StringUtils.countMatches( classifiers, "," ); |
340 | 0 | if ( typesLength != filesLength ) |
341 | |
{ |
342 | 0 | throw new MojoExecutionException( "You must specify the same number of entries in 'files' and " + |
343 | |
"'types' (respectively " + filesLength + " and " + typesLength + " entries )" ); |
344 | |
} |
345 | 0 | if ( classifiersLength != filesLength ) |
346 | |
{ |
347 | 0 | throw new MojoExecutionException( "You must specify the same number of entries in 'files' and " + |
348 | |
"'classifiers' (respectively " + filesLength + " and " + classifiersLength + " entries )" ); |
349 | |
} |
350 | 0 | int fi = 0; |
351 | 0 | int ti = 0; |
352 | 0 | int ci = 0; |
353 | 0 | for ( int i = 0; i <= filesLength; i++ ) |
354 | |
{ |
355 | 0 | int nfi = files.indexOf( ',', fi ); |
356 | 0 | if ( nfi == -1 ) |
357 | |
{ |
358 | 0 | nfi = files.length(); |
359 | |
} |
360 | 0 | int nti = types.indexOf( ',', ti ); |
361 | 0 | if ( nti == -1 ) |
362 | |
{ |
363 | 0 | nti = types.length(); |
364 | |
} |
365 | 0 | int nci = classifiers.indexOf( ',', ci ); |
366 | 0 | if ( nci == -1 ) |
367 | |
{ |
368 | 0 | nci = classifiers.length(); |
369 | |
} |
370 | 0 | File file = new File( files.substring( fi, nfi ) ); |
371 | 0 | if ( !file.isFile() ) |
372 | |
{ |
373 | |
|
374 | 0 | file = new File( project.getBasedir(), files.substring( fi, nfi ) ); |
375 | |
} |
376 | 0 | if ( file.isFile() ) |
377 | |
{ |
378 | 0 | if ( StringUtils.isWhitespace( classifiers.substring( ci, nci ) ) ) |
379 | |
{ |
380 | 0 | projectHelper.attachArtifact( project, types.substring( ti, nti ).trim(), file ); |
381 | |
} |
382 | |
else |
383 | |
{ |
384 | 0 | projectHelper.attachArtifact( project, types.substring( ti, nti).trim(), |
385 | |
classifiers.substring( ci, nci ).trim(), file); |
386 | |
} |
387 | |
} |
388 | |
else |
389 | |
{ |
390 | 0 | throw new MojoExecutionException( "Specified side artifact " + file + " does not exist" ); |
391 | |
} |
392 | 0 | fi = nfi + 1; |
393 | 0 | ti = nti + 1; |
394 | 0 | ci = nci + 1; |
395 | |
} |
396 | 0 | } |
397 | |
else |
398 | |
{ |
399 | 6 | if ( types != null ) |
400 | |
{ |
401 | 0 | throw new MojoExecutionException( "You must specify 'files' if you specify 'types'" ); |
402 | |
} |
403 | 6 | if ( classifiers != null ) |
404 | |
{ |
405 | 0 | throw new MojoExecutionException( "You must specify 'files' if you specify 'classifiers'" ); |
406 | |
} |
407 | |
} |
408 | |
|
409 | 6 | List attachedArtifacts = project.getAttachedArtifacts(); |
410 | |
|
411 | 6 | for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); ) |
412 | |
{ |
413 | 0 | Artifact attached = ( Artifact ) i.next(); |
414 | |
|
415 | |
try |
416 | |
{ |
417 | 0 | deploy( attached.getFile(), attached, deploymentRepository, getLocalRepository() ); |
418 | |
} |
419 | 0 | catch ( ArtifactDeploymentException e ) |
420 | |
{ |
421 | 0 | throw new MojoExecutionException( "Error deploying attached artifact " + attached.getFile() + ": " + e.getMessage(), e ); |
422 | 0 | } |
423 | 0 | } |
424 | |
|
425 | 6 | } |
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
private File getLocalRepoFile( Artifact artifact ) |
435 | |
{ |
436 | 6 | String path = getLocalRepository().pathOf( artifact ); |
437 | 6 | return new File( getLocalRepository().getBasedir(), path ); |
438 | |
} |
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
private void processModel( Model model ) |
446 | |
{ |
447 | 8 | Parent parent = model.getParent(); |
448 | |
|
449 | 8 | if ( this.groupId == null ) |
450 | |
{ |
451 | 6 | this.groupId = model.getGroupId(); |
452 | 6 | if ( this.groupId == null && parent != null ) |
453 | |
{ |
454 | 4 | this.groupId = parent.getGroupId(); |
455 | |
} |
456 | |
} |
457 | 8 | if ( this.artifactId == null ) |
458 | |
{ |
459 | 6 | this.artifactId = model.getArtifactId(); |
460 | |
} |
461 | 8 | if ( this.version == null ) |
462 | |
{ |
463 | 6 | this.version = model.getVersion(); |
464 | 6 | if ( this.version == null && parent != null ) |
465 | |
{ |
466 | 2 | this.version = parent.getVersion(); |
467 | |
} |
468 | |
} |
469 | 8 | if ( this.packaging == null ) |
470 | |
{ |
471 | 6 | this.packaging = model.getPackaging(); |
472 | |
} |
473 | 8 | } |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
Model readModel( File pomFile ) |
483 | |
throws MojoExecutionException |
484 | |
{ |
485 | 2 | Reader reader = null; |
486 | |
try |
487 | |
{ |
488 | 2 | reader = ReaderFactory.newXmlReader( pomFile ); |
489 | 2 | return new MavenXpp3Reader().read( reader ); |
490 | |
} |
491 | 0 | catch ( FileNotFoundException e ) |
492 | |
{ |
493 | 0 | throw new MojoExecutionException( "POM not found " + pomFile, e ); |
494 | |
} |
495 | 0 | catch ( IOException e ) |
496 | |
{ |
497 | 0 | throw new MojoExecutionException( "Error reading POM " + pomFile, e ); |
498 | |
} |
499 | 0 | catch ( XmlPullParserException e ) |
500 | |
{ |
501 | 0 | throw new MojoExecutionException( "Error parsing POM " + pomFile, e ); |
502 | |
} |
503 | |
finally |
504 | |
{ |
505 | 2 | IOUtil.close( reader ); |
506 | |
} |
507 | |
} |
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
private File generatePomFile() |
516 | |
throws MojoExecutionException |
517 | |
{ |
518 | 5 | Model model = generateModel(); |
519 | |
|
520 | 5 | Writer fw = null; |
521 | |
try |
522 | |
{ |
523 | 5 | File tempFile = File.createTempFile( "mvndeploy", ".pom" ); |
524 | 5 | tempFile.deleteOnExit(); |
525 | |
|
526 | 5 | fw = WriterFactory.newXmlWriter( tempFile ); |
527 | 5 | new MavenXpp3Writer().write( fw, model ); |
528 | |
|
529 | 5 | return tempFile; |
530 | |
} |
531 | 0 | catch ( IOException e ) |
532 | |
{ |
533 | 0 | throw new MojoExecutionException( "Error writing temporary pom file: " + e.getMessage(), e ); |
534 | |
} |
535 | |
finally |
536 | |
{ |
537 | 5 | IOUtil.close( fw ); |
538 | |
} |
539 | |
} |
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
private void validateArtifactInformation() |
547 | |
throws MojoExecutionException |
548 | |
{ |
549 | 6 | Model model = generateModel(); |
550 | |
|
551 | 6 | ModelValidationResult result = modelValidator.validate( model ); |
552 | |
|
553 | 6 | if ( result.getMessageCount() > 0 ) |
554 | |
{ |
555 | 0 | throw new MojoExecutionException( "The artifact information is incomplete or not valid:\n" |
556 | |
+ result.render( " " ) ); |
557 | |
} |
558 | 6 | } |
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
private Model generateModel() |
566 | |
{ |
567 | 11 | Model model = new Model(); |
568 | |
|
569 | 11 | model.setModelVersion( "4.0.0" ); |
570 | |
|
571 | 11 | model.setGroupId( groupId ); |
572 | 11 | model.setArtifactId( artifactId ); |
573 | 11 | model.setVersion( version ); |
574 | 11 | model.setPackaging( packaging ); |
575 | |
|
576 | 11 | model.setDescription( description ); |
577 | |
|
578 | 11 | return model; |
579 | |
} |
580 | |
|
581 | |
void setGroupId( String groupId ) |
582 | |
{ |
583 | 1 | this.groupId = groupId; |
584 | 1 | } |
585 | |
|
586 | |
void setArtifactId( String artifactId ) |
587 | |
{ |
588 | 1 | this.artifactId = artifactId; |
589 | 1 | } |
590 | |
|
591 | |
void setVersion( String version ) |
592 | |
{ |
593 | 1 | this.version = version; |
594 | 1 | } |
595 | |
|
596 | |
void setPackaging( String packaging ) |
597 | |
{ |
598 | 1 | this.packaging = packaging; |
599 | 1 | } |
600 | |
|
601 | |
void setPomFile( File pomFile ) |
602 | |
{ |
603 | 7 | this.pomFile = pomFile; |
604 | 7 | } |
605 | |
|
606 | |
String getGroupId() |
607 | |
{ |
608 | 7 | return groupId; |
609 | |
} |
610 | |
|
611 | |
String getArtifactId() |
612 | |
{ |
613 | 7 | return artifactId; |
614 | |
} |
615 | |
|
616 | |
String getVersion() |
617 | |
{ |
618 | 7 | return version; |
619 | |
} |
620 | |
|
621 | |
String getPackaging() |
622 | |
{ |
623 | 7 | return packaging; |
624 | |
} |
625 | |
|
626 | |
File getFile() |
627 | |
{ |
628 | 0 | return file; |
629 | |
} |
630 | |
|
631 | |
String getClassifier() |
632 | |
{ |
633 | 2 | return classifier; |
634 | |
} |
635 | |
|
636 | |
void setClassifier( String classifier ) |
637 | |
{ |
638 | 1 | this.classifier = classifier; |
639 | 1 | } |
640 | |
} |