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