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 | 6 | 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 | 6 | final List<FileItem> fileList = assembly.getFiles(); |
59 | 6 | final File basedir = configSource.getBasedir(); |
60 | |
|
61 | 6 | final FileFormatter fileFormatter = new FileFormatter( configSource, getLogger() ); |
62 | 6 | for ( final Iterator<FileItem> i = fileList.iterator(); i.hasNext(); ) |
63 | |
{ |
64 | 11 | final FileItem fileItem = i.next(); |
65 | |
|
66 | 11 | final String sourcePath = fileItem.getSource(); |
67 | |
|
68 | |
|
69 | 11 | File source = new File( sourcePath ); |
70 | |
|
71 | |
|
72 | |
|
73 | 11 | final String sourceName = source.getName(); |
74 | |
|
75 | 11 | if ( !source.isAbsolute() ) |
76 | |
{ |
77 | 10 | source = new File( basedir, sourcePath ); |
78 | |
} |
79 | |
|
80 | 11 | source = |
81 | |
fileFormatter.format( source, fileItem.isFiltered(), fileItem.getLineEnding(), |
82 | |
configSource.getEncoding() ); |
83 | |
|
84 | 11 | String destName = fileItem.getDestName(); |
85 | |
|
86 | 11 | if ( destName == null ) |
87 | |
{ |
88 | 5 | destName = sourceName; |
89 | |
} |
90 | |
|
91 | 11 | final String outputDirectory = |
92 | |
AssemblyFormatUtils.getOutputDirectory( fileItem.getOutputDirectory(), configSource.getProject(), null, |
93 | |
configSource.getFinalName(), configSource ); |
94 | |
|
95 | |
String target; |
96 | |
|
97 | |
|
98 | 11 | if ( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( "\\" ) ) |
99 | |
{ |
100 | 3 | target = outputDirectory + destName; |
101 | |
} |
102 | 8 | else if ( outputDirectory.length() < 1 ) |
103 | |
{ |
104 | 8 | target = destName; |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | target = outputDirectory + "/" + destName; |
109 | |
} |
110 | |
|
111 | |
try |
112 | |
{ |
113 | 11 | archiver.addFile( source, target, TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() ) ); |
114 | |
} |
115 | 0 | catch ( final ArchiverException e ) |
116 | |
{ |
117 | 0 | throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e ); |
118 | 11 | } |
119 | 11 | } |
120 | 6 | } |
121 | |
|
122 | |
} |