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 FileItem fileItem : fileList) {
63 final String sourcePath = fileItem.getSource();
64
65
66 File source = new File(sourcePath);
67
68
69
70 final String sourceName = source.getName();
71
72 if (!source.isAbsolute()) {
73 source = new File(basedir, sourcePath);
74 }
75
76 source =
77 fileFormatter.format(source, fileItem.isFiltered(), fileItem.getLineEnding(),
78 configSource.getEncoding());
79
80 String destName = fileItem.getDestName();
81
82 if (destName == null) {
83 destName = sourceName;
84 }
85
86 final String outputDirectory =
87 AssemblyFormatUtils.getOutputDirectory(fileItem.getOutputDirectory(), configSource.getProject(), null,
88 configSource.getFinalName(), configSource);
89
90 String target;
91
92
93 if (outputDirectory.endsWith("/") || outputDirectory.endsWith("\\")) {
94 target = outputDirectory + destName;
95 } else if (outputDirectory.length() < 1) {
96 target = destName;
97 } else {
98 target = outputDirectory + "/" + destName;
99 }
100
101 try {
102 archiver.addFile(source, target, TypeConversionUtils.modeToInt(fileItem.getFileMode(), getLogger()));
103 } catch (final ArchiverException e) {
104 throw new ArchiveCreationException("Error adding file to archive: " + e.getMessage(), e);
105 }
106 }
107 }
108
109 }