1 package org.apache.maven.plugin.assembly.archive.phase;
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.plugin.assembly.AssemblerConfigurationSource;
23 import org.apache.maven.plugin.assembly.AssemblyContext;
24 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
25 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
26 import org.apache.maven.plugin.assembly.format.FileFormatter;
27 import org.apache.maven.plugin.assembly.model.Assembly;
28 import org.apache.maven.plugin.assembly.model.FileItem;
29 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
30 import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
31 import org.codehaus.plexus.archiver.Archiver;
32 import org.codehaus.plexus.archiver.ArchiverException;
33 import org.codehaus.plexus.logging.AbstractLogEnabled;
34
35 import java.io.File;
36 import java.util.Iterator;
37 import java.util.List;
38
39
40
41
42
43
44
45 public class FileItemAssemblyPhase
46 extends AbstractLogEnabled
47 implements AssemblyArchiverPhase
48 {
49
50
51
52
53 public void execute( final Assembly assembly, final Archiver archiver,
54 final AssemblerConfigurationSource configSource, final AssemblyContext context )
55 throws ArchiveCreationException, AssemblyFormattingException
56 {
57 final List<FileItem> fileList = assembly.getFiles();
58 final File basedir = configSource.getBasedir();
59
60 final FileFormatter fileFormatter = new FileFormatter( configSource, getLogger() );
61 for ( final Iterator<FileItem> i = fileList.iterator(); i.hasNext(); )
62 {
63 final FileItem fileItem = i.next();
64
65 final String sourcePath = fileItem.getSource();
66
67
68 File source = new File( sourcePath );
69
70
71
72 final String sourceName = source.getName();
73
74 if ( !source.isAbsolute() )
75 {
76 source = new File( basedir, sourcePath );
77 }
78
79 source = fileFormatter.format( source, fileItem.isFiltered(), fileItem.getLineEnding() );
80
81 String destName = fileItem.getDestName();
82
83 if ( destName == null )
84 {
85 destName = sourceName;
86 }
87
88 final String outputDirectory =
89 AssemblyFormatUtils.getOutputDirectory( fileItem.getOutputDirectory(), configSource.getProject(), null,
90 configSource.getFinalName(), configSource );
91
92 String target;
93
94
95 if ( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( "\\" ) )
96 {
97 target = outputDirectory + destName;
98 }
99 else if ( outputDirectory.length() < 1 )
100 {
101 target = destName;
102 }
103 else
104 {
105 target = outputDirectory + "/" + destName;
106 }
107
108 try
109 {
110 archiver.addFile( source, target, TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() ) );
111 }
112 catch ( final ArchiverException e )
113 {
114 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
115 }
116 }
117 }
118
119 }