1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.assembly.archive.task;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
26 import org.apache.maven.plugins.assembly.utils.AssemblyFormatUtils;
27 import org.codehaus.plexus.archiver.Archiver;
28 import org.codehaus.plexus.archiver.ArchiverException;
29 import org.codehaus.plexus.archiver.util.DefaultFileSet;
30 import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
31
32
33
34
35 public class AddDirectoryTask {
36
37 private final File directory;
38
39 private final InputStreamTransformer transformer;
40
41 private List<String> includes;
42
43 private List<String> excludes;
44
45 private String outputDirectory;
46
47 private boolean useDefaultExcludes = true;
48
49 private int directoryMode = -1;
50
51 private int fileMode = -1;
52
53 public AddDirectoryTask(final File directory, InputStreamTransformer transformers) {
54 this.directory = directory;
55
56 this.transformer = transformers;
57 }
58
59 public AddDirectoryTask(final File directory) {
60 this(directory, null);
61 }
62
63 public void execute(final Archiver archiver) throws ArchiveCreationException {
64 if (".".equals(outputDirectory)) {
65 outputDirectory = "";
66 } else if ("..".equals(outputDirectory)) {
67 throw new ArchiveCreationException("Cannot add source directory: " + directory + " to archive-path: "
68 + outputDirectory + ". All paths must be within the archive root directory.");
69 }
70
71 final int oldDirMode = archiver.getOverrideDirectoryMode();
72 final int oldFileMode = archiver.getOverrideFileMode();
73
74 boolean fileModeSet = false;
75 boolean dirModeSet = false;
76
77 try {
78 if (directoryMode != -1) {
79 archiver.setDirectoryMode(directoryMode);
80 dirModeSet = true;
81 }
82
83 if (fileMode != -1) {
84 archiver.setFileMode(fileMode);
85 fileModeSet = true;
86 }
87
88 if (directory.exists()) {
89 List<String> directoryExcludes;
90 if (excludes != null && !excludes.isEmpty()) {
91 directoryExcludes = new ArrayList<>(excludes);
92 } else {
93 directoryExcludes = new ArrayList<>();
94 }
95
96 try {
97 String[] includesArray = null;
98 if (includes != null && !includes.isEmpty()) {
99 includesArray = new String[includes.size()];
100
101 int i = 0;
102 for (String include : includes) {
103 includesArray[i++] = normalize(include);
104 }
105 }
106
107
108 final String[] excludesArray = new String[directoryExcludes.size()];
109
110 int i = 0;
111 for (String directoryExclude : directoryExcludes) {
112 excludesArray[i++] = normalize(directoryExclude);
113 }
114
115 final DefaultFileSet fs = new DefaultFileSet();
116 fs.setUsingDefaultExcludes(useDefaultExcludes);
117 fs.setPrefix(outputDirectory);
118 fs.setDirectory(directory);
119 fs.setIncludes(includesArray);
120 fs.setExcludes(excludesArray);
121 if (transformer != null) {
122 fs.setStreamTransformer(transformer);
123 }
124
125 archiver.addFileSet(fs);
126 } catch (final ArchiverException e) {
127 throw new ArchiveCreationException("Error adding directory to archive: " + e.getMessage(), e);
128 }
129 }
130 } finally {
131 if (dirModeSet) {
132 archiver.setDirectoryMode(oldDirMode);
133 }
134
135 if (fileModeSet) {
136 archiver.setFileMode(oldFileMode);
137 }
138 }
139 }
140
141 private String normalize(String include) {
142 String value = AssemblyFormatUtils.fixRelativeRefs(include);
143
144 if (value.startsWith("/") || value.startsWith("\\")) {
145 value = value.substring(1);
146 }
147 return value;
148 }
149
150 public void setExcludes(final List<String> excludes) {
151 this.excludes = excludes;
152 }
153
154 public void setIncludes(final List<String> includes) {
155 this.includes = includes;
156 }
157
158 public void setOutputDirectory(final String outputDirectory) {
159 this.outputDirectory = outputDirectory;
160 }
161
162 public void setDirectoryMode(final int directoryMode) {
163 this.directoryMode = directoryMode;
164 }
165
166 public void setFileMode(final int fileMode) {
167 this.fileMode = fileMode;
168 }
169
170 public void setUseDefaultExcludes(final boolean useDefaultExcludes) {
171 this.useDefaultExcludes = useDefaultExcludes;
172 }
173 }