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