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