1 package org.apache.maven.plugin.assembly.mojos;
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.archiver.MavenArchiveConfiguration;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.execution.MavenSession;
25 import org.apache.maven.plugin.AbstractMojo;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
29 import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
30 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
31 import org.apache.maven.plugin.assembly.archive.AssemblyArchiver;
32 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
33 import org.apache.maven.plugin.assembly.io.AssemblyReadException;
34 import org.apache.maven.plugin.assembly.io.AssemblyReader;
35 import org.apache.maven.plugin.assembly.model.Assembly;
36 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
37 import org.apache.maven.plugin.logging.Log;
38 import org.apache.maven.plugins.annotations.Component;
39 import org.apache.maven.plugins.annotations.Parameter;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.project.MavenProjectHelper;
42 import org.apache.maven.shared.filtering.MavenFileFilter;
43 import org.codehaus.plexus.configuration.PlexusConfiguration;
44
45 import java.io.File;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.List;
49
50
51
52
53
54
55
56 public abstract class AbstractAssemblyMojo
57 extends AbstractMojo
58 implements AssemblerConfigurationSource
59 {
60
61
62
63 @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
64 protected String encoding;
65
66
67
68
69
70
71
72 @Parameter( property = "assembly.escapeString" )
73 protected String escapeString;
74
75
76
77
78
79 @Parameter( property = "assembly.skipAssembly", defaultValue = "false" )
80 private boolean skipAssembly;
81
82
83
84
85 @Parameter( property = "assembly.dryRun", defaultValue = "false" )
86 private boolean dryRun;
87
88
89
90
91
92
93 @Parameter( defaultValue = "true" )
94 private boolean ignoreDirFormatExtensions;
95
96
97
98
99 @Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
100 private ArtifactRepository localRepository;
101
102
103
104 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true )
105 private List<ArtifactRepository> remoteRepositories;
106
107
108
109
110 @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
111 private List<MavenProject> reactorProjects;
112
113
114
115
116 @Parameter( defaultValue = "${project.build.directory}", required = true )
117 private File outputDirectory;
118
119
120
121
122 @Parameter( defaultValue = "${project.build.finalName}", required = true )
123 private String finalName;
124
125
126
127
128 @Parameter( defaultValue = "${project.build.directory}/assembly/work", required = true )
129 private File workDirectory;
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 @Parameter
145 private List<String> formats;
146
147
148
149
150
151
152
153 @Deprecated
154 @SuppressWarnings( "unused" )
155 @Parameter( property = "classifier" )
156 private String classifier;
157
158
159
160
161 @Parameter
162 private String[] descriptors;
163
164
165
166
167
168
169
170
171 @Parameter
172 private String[] descriptorRefs;
173
174
175
176
177 @Parameter
178 private File descriptorSourceDirectory;
179
180
181
182
183
184 @Parameter
185 private File archiveBaseDirectory;
186
187
188
189
190
191
192 @Deprecated
193 @Parameter( property = "descriptorId" )
194 protected String descriptorId;
195
196
197
198
199
200
201 @Deprecated
202 @Parameter( property = "descriptor" )
203 protected String descriptor;
204
205
206
207
208
209 @Parameter( property = "assembly.tarLongFileMode", defaultValue = "warn" )
210 private String tarLongFileMode;
211
212
213
214
215 @Parameter( defaultValue = "${project.basedir}", required = true, readonly = true )
216 private File basedir;
217
218
219
220
221 @Component
222 private MavenProjectHelper projectHelper;
223
224
225
226
227 @Component
228 private MavenFileFilter mavenFileFilter;
229
230
231
232
233 @Component
234 private MavenSession mavenSession;
235
236
237
238
239 @Parameter( defaultValue = "${project.build.directory}/archive-tmp", required = true, readonly = true )
240 private File tempRoot;
241
242
243
244
245 @Parameter( defaultValue = "${project.reporting.outputDirectory}", readonly = true )
246 private File siteDirectory;
247
248
249
250
251
252
253 @Deprecated
254 @Parameter( property = "includeSite", defaultValue = "false" )
255 private boolean includeSite;
256
257
258
259
260 @Parameter( property = "assembly.appendAssemblyId", defaultValue = "true" )
261 protected boolean appendAssemblyId;
262
263
264
265
266 @Parameter( property = "assembly.ignoreMissingDescriptor", defaultValue = "false" )
267 protected boolean ignoreMissingDescriptor;
268
269
270
271
272
273 @Parameter
274 private MavenArchiveConfiguration archive;
275
276
277
278
279
280
281
282
283
284 @Parameter
285 protected List<String> filters;
286
287
288
289
290
291
292 @Parameter( property = "assembly.attach", defaultValue = "true" )
293 private boolean attach;
294
295
296
297
298
299
300
301 @Parameter( defaultValue = "false" )
302 private boolean recompressZippedFiles;
303
304
305
306 @Component
307 private AssemblyArchiver assemblyArchiver;
308
309
310
311 @Component
312 private AssemblyReader assemblyReader;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327 @Parameter
328 private PlexusConfiguration archiverConfig;
329
330
331
332
333
334
335
336 @Parameter( property = "assembly.runOnlyAtExecutionRoot", defaultValue = "false" )
337 private boolean runOnlyAtExecutionRoot;
338
339
340
341
342
343
344
345
346
347
348 @Parameter( property = "assembly.updateOnly", defaultValue = "false" )
349 private boolean updateOnly;
350
351
352
353
354
355
356
357
358 @Parameter( property = "assembly.useJvmChmod", defaultValue = "false" )
359 private boolean useJvmChmod;
360
361
362
363
364
365
366
367
368
369
370
371
372
373 @Parameter( property = "assembly.ignorePermissions", defaultValue = "false" )
374 private boolean ignorePermissions;
375
376
377
378
379
380
381
382 public void execute()
383 throws MojoExecutionException, MojoFailureException
384 {
385 if ( skipAssembly )
386 {
387 getLog().info( "Assemblies have been skipped per configuration of the skipAssembly parameter." );
388 return;
389 }
390
391
392 if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
393 {
394 getLog().info( "Skipping the assembly in this project because it's not the Execution Root" );
395 return;
396 }
397
398 List<Assembly> assemblies;
399 try
400 {
401 assemblies = assemblyReader.readAssemblies( this );
402 }
403 catch ( final AssemblyReadException e )
404 {
405 throw new MojoExecutionException( "Error reading assemblies: " + e.getMessage(), e );
406 }
407 catch ( final InvalidAssemblerConfigurationException e )
408 {
409 throw new MojoFailureException( assemblyReader, e.getMessage(), "Mojo configuration is invalid: "
410 + e.getMessage() );
411 }
412
413
414
415
416 boolean warnedAboutMainProjectArtifact = false;
417 for ( final Iterator<Assembly> assemblyIterator = assemblies.iterator(); assemblyIterator.hasNext(); )
418 {
419 final Assembly assembly = assemblyIterator.next();
420 try
421 {
422 final String fullName = AssemblyFormatUtils.getDistributionName( assembly, this );
423
424 List<String> effectiveFormats = formats;
425 if ( effectiveFormats == null || effectiveFormats.size() == 0 )
426 {
427 effectiveFormats = assembly.getFormats();
428 }
429 if ( effectiveFormats == null || effectiveFormats.size() == 0 )
430 {
431 throw new MojoFailureException( "No formats specified in the execution parameters or the assembly descriptor.");
432 }
433
434 for ( final String format : effectiveFormats )
435 {
436 final File destFile = assemblyArchiver.createArchive( assembly, fullName, format, this, isRecompressZippedFiles());
437
438 final MavenProject project = getProject();
439 final String classifier = getClassifier();
440 final String type = project.getArtifact()
441 .getType();
442
443 if ( attach && destFile.isFile() )
444 {
445 if ( isAssemblyIdAppended() )
446 {
447 projectHelper.attachArtifact( project, format, assembly.getId(), destFile );
448 }
449 else if ( classifier != null )
450 {
451 projectHelper.attachArtifact( project, format, classifier, destFile );
452 }
453 else if ( !"pom".equals( type ) && format.equals( type ) )
454 {
455 if ( !warnedAboutMainProjectArtifact )
456 {
457 final StringBuilder message = new StringBuilder();
458
459 message.append( "Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing." );
460 message.append( "\nInstead of attaching the assembly file: " )
461 .append( destFile )
462 .append( ", it will become the file for main project artifact." );
463 message.append( "\nNOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!" );
464
465 getLog().warn( message );
466 warnedAboutMainProjectArtifact = true;
467 }
468
469 final File existingFile = project.getArtifact()
470 .getFile();
471 if ( ( existingFile != null ) && existingFile.exists() )
472 {
473 getLog().warn( "Replacing pre-existing project main-artifact file: " + existingFile
474 + "\nwith assembly file: " + destFile );
475 }
476
477 project.getArtifact()
478 .setFile( destFile );
479 }
480 else
481 {
482 projectHelper.attachArtifact( project, format, null, destFile );
483 }
484 }
485 else if ( attach )
486 {
487 getLog().warn( "Assembly file: "
488 + destFile
489 + " is not a regular file (it may be a directory). It cannot be attached to the project build for installation or deployment." );
490 }
491 }
492 }
493 catch ( final ArchiveCreationException e )
494 {
495 throw new MojoExecutionException( "Failed to create assembly: " + e.getMessage(), e );
496 }
497 catch ( final AssemblyFormattingException e )
498 {
499 throw new MojoExecutionException( "Failed to create assembly: " + e.getMessage(), e );
500 }
501 catch ( final InvalidAssemblerConfigurationException e )
502 {
503 throw new MojoFailureException( assembly, "Assembly is incorrectly configured: " + assembly.getId(),
504 "Assembly: " + assembly.getId() + " is not configured correctly: "
505 + e.getMessage() );
506 }
507 }
508 }
509
510
511
512
513
514
515 protected boolean isThisTheExecutionRoot()
516 {
517 final Log log = getLog();
518 log.debug( "Root Folder:" + mavenSession.getExecutionRootDirectory() );
519 log.debug( "Current Folder:" + basedir );
520 final boolean result = mavenSession.getExecutionRootDirectory()
521 .equalsIgnoreCase( basedir.toString() );
522 if ( result )
523 {
524 log.debug( "This is the execution root." );
525 }
526 else
527 {
528 log.debug( "This is NOT the execution root." );
529 }
530
531 return result;
532 }
533
534 protected AssemblyArchiver getAssemblyArchiver()
535 {
536 return assemblyArchiver;
537 }
538
539 protected AssemblyReader getAssemblyReader()
540 {
541 return assemblyReader;
542 }
543
544 public File getBasedir()
545 {
546 return basedir;
547 }
548
549
550
551
552
553
554 @Deprecated
555 public String getDescriptor()
556 {
557 return descriptor;
558 }
559
560
561
562
563
564
565 @Deprecated
566 public String getDescriptorId()
567 {
568 return descriptorId;
569 }
570
571 public String[] getDescriptorReferences()
572 {
573 return descriptorRefs;
574 }
575
576 public File getDescriptorSourceDirectory()
577 {
578 return descriptorSourceDirectory;
579 }
580
581 public String[] getDescriptors()
582 {
583 return descriptors;
584 }
585
586 public abstract MavenProject getProject();
587
588 public File getSiteDirectory()
589 {
590 return siteDirectory;
591 }
592
593 public boolean isSiteIncluded()
594 {
595 return includeSite;
596 }
597
598 public String getFinalName()
599 {
600 return finalName;
601 }
602
603 public boolean isAssemblyIdAppended()
604 {
605 return appendAssemblyId;
606 }
607
608 public String getTarLongFileMode()
609 {
610 return tarLongFileMode;
611 }
612
613 public File getOutputDirectory()
614 {
615 return outputDirectory;
616 }
617
618 public MavenArchiveConfiguration getJarArchiveConfiguration()
619 {
620 return archive;
621 }
622
623 public File getWorkingDirectory()
624 {
625 return workDirectory;
626 }
627
628 public ArtifactRepository getLocalRepository()
629 {
630 return localRepository;
631 }
632
633 public File getTemporaryRootDirectory()
634 {
635 return tempRoot;
636 }
637
638 public File getArchiveBaseDirectory()
639 {
640 return archiveBaseDirectory;
641 }
642
643 public List<String> getFilters()
644 {
645 if ( filters == null )
646 {
647 filters = getProject().getBuild()
648 .getFilters();
649 if ( filters == null )
650 {
651 filters = Collections.emptyList();
652 }
653 }
654 return filters;
655 }
656
657 public List<MavenProject> getReactorProjects()
658 {
659 return reactorProjects;
660 }
661
662 public String getClassifier()
663 {
664
665 return null;
666 }
667
668 protected MavenProjectHelper getProjectHelper()
669 {
670 return projectHelper;
671 }
672
673 public void setAppendAssemblyId( final boolean appendAssemblyId )
674 {
675 this.appendAssemblyId = appendAssemblyId;
676 }
677
678 public void setArchive( final MavenArchiveConfiguration archive )
679 {
680 this.archive = archive;
681 }
682
683 public void setArchiveBaseDirectory( final File archiveBaseDirectory )
684 {
685 this.archiveBaseDirectory = archiveBaseDirectory;
686 }
687
688 public void setAssemblyArchiver( final AssemblyArchiver assemblyArchiver )
689 {
690 this.assemblyArchiver = assemblyArchiver;
691 }
692
693 public void setAssemblyReader( final AssemblyReader assemblyReader )
694 {
695 this.assemblyReader = assemblyReader;
696 }
697
698 public void setBasedir( final File basedir )
699 {
700 this.basedir = basedir;
701 }
702
703 public void setClassifier( final String classifier )
704 {
705 this.classifier = classifier;
706 }
707
708
709
710
711
712
713 @Deprecated
714 public void setDescriptor( final String descriptor )
715 {
716 this.descriptor = descriptor;
717 }
718
719
720
721
722
723
724 @Deprecated
725 public void setDescriptorId( final String descriptorId )
726 {
727 this.descriptorId = descriptorId;
728 }
729
730 public void setDescriptorRefs( final String[] descriptorRefs )
731 {
732 this.descriptorRefs = descriptorRefs;
733 }
734
735 public void setDescriptors( final String[] descriptors )
736 {
737 this.descriptors = descriptors;
738 }
739
740 public void setDescriptorSourceDirectory( final File descriptorSourceDirectory )
741 {
742 this.descriptorSourceDirectory = descriptorSourceDirectory;
743 }
744
745 public void setFilters( final List<String> filters )
746 {
747 this.filters = filters;
748 }
749
750 public void setFinalName( final String finalName )
751 {
752 this.finalName = finalName;
753 }
754
755 public void setIncludeSite( final boolean includeSite )
756 {
757 this.includeSite = includeSite;
758 }
759
760 public void setLocalRepository( final ArtifactRepository localRepository )
761 {
762 this.localRepository = localRepository;
763 }
764
765 public void setOutputDirectory( final File outputDirectory )
766 {
767 this.outputDirectory = outputDirectory;
768 }
769
770 public void setProjectHelper( final MavenProjectHelper projectHelper )
771 {
772 this.projectHelper = projectHelper;
773 }
774
775 public void setReactorProjects( final List<MavenProject> reactorProjects )
776 {
777 this.reactorProjects = reactorProjects;
778 }
779
780 public void setSiteDirectory( final File siteDirectory )
781 {
782 this.siteDirectory = siteDirectory;
783 }
784
785 public void setTarLongFileMode( final String tarLongFileMode )
786 {
787 this.tarLongFileMode = tarLongFileMode;
788 }
789
790 public void setTempRoot( final File tempRoot )
791 {
792 this.tempRoot = tempRoot;
793 }
794
795 public void setWorkDirectory( final File workDirectory )
796 {
797 this.workDirectory = workDirectory;
798 }
799
800 public List<ArtifactRepository> getRemoteRepositories()
801 {
802 return remoteRepositories;
803 }
804
805 public boolean isDryRun()
806 {
807 return dryRun;
808 }
809
810 public boolean isIgnoreDirFormatExtensions()
811 {
812 return ignoreDirFormatExtensions;
813 }
814
815 public boolean isIgnoreMissingDescriptor()
816 {
817 return ignoreMissingDescriptor;
818 }
819
820 public void setIgnoreMissingDescriptor( final boolean ignoreMissingDescriptor )
821 {
822 this.ignoreMissingDescriptor = ignoreMissingDescriptor;
823 }
824
825 public MavenSession getMavenSession()
826 {
827 return mavenSession;
828 }
829
830 public String getArchiverConfig()
831 {
832 return archiverConfig == null ? null : archiverConfig.toString();
833 }
834
835 public MavenFileFilter getMavenFileFilter()
836 {
837 return mavenFileFilter;
838 }
839
840 public boolean isUpdateOnly()
841 {
842 return updateOnly;
843 }
844
845 public boolean isUseJvmChmod()
846 {
847 return useJvmChmod;
848 }
849
850 public boolean isIgnorePermissions()
851 {
852 return ignorePermissions;
853 }
854
855 public String getEncoding() {
856 return encoding;
857 }
858
859 protected boolean isRecompressZippedFiles() {
860 return recompressZippedFiles;
861 }
862
863 public String getEscapeString() {
864 return escapeString;
865 }
866 }