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