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