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