1 | |
package org.apache.maven.plugin.assembly.archive.task; |
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.archive.ArchiveCreationException; |
24 | |
import org.codehaus.plexus.archiver.Archiver; |
25 | |
import org.codehaus.plexus.archiver.ArchiverException; |
26 | |
import org.codehaus.plexus.archiver.util.DefaultFileSet; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class AddDirectoryTask |
37 | |
implements ArchiverTask |
38 | |
{ |
39 | |
|
40 | |
private final File directory; |
41 | |
|
42 | |
private List<String> includes; |
43 | |
|
44 | |
private List<String> excludes; |
45 | |
|
46 | |
private String outputDirectory; |
47 | |
|
48 | 7 | private boolean useDefaultExcludes = true; |
49 | |
|
50 | 7 | private int directoryMode = -1; |
51 | |
|
52 | 7 | private int fileMode = -1; |
53 | |
|
54 | |
public AddDirectoryTask( final File directory ) |
55 | 7 | { |
56 | 7 | this.directory = directory; |
57 | 7 | } |
58 | |
|
59 | |
public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource ) |
60 | |
throws ArchiveCreationException |
61 | |
{ |
62 | 7 | if ( ".".equals( outputDirectory ) ) |
63 | |
{ |
64 | 0 | outputDirectory = ""; |
65 | |
} |
66 | 7 | else if ( "..".equals( outputDirectory ) ) |
67 | |
{ |
68 | 0 | throw new ArchiveCreationException( "Cannot add source directory: " + directory + " to archive-path: " |
69 | |
+ outputDirectory + ". All paths must be within the archive root directory." ); |
70 | |
} |
71 | |
|
72 | 7 | final int oldDirMode = archiver.getOverrideDirectoryMode(); |
73 | 7 | final int oldFileMode = archiver.getOverrideFileMode(); |
74 | |
|
75 | 7 | boolean fileModeSet = false; |
76 | 7 | boolean dirModeSet = false; |
77 | |
|
78 | |
try |
79 | |
{ |
80 | 7 | if ( directoryMode != -1 ) |
81 | |
{ |
82 | 2 | archiver.setDirectoryMode( directoryMode ); |
83 | 2 | dirModeSet = true; |
84 | |
} |
85 | |
|
86 | 7 | if ( fileMode != -1 ) |
87 | |
{ |
88 | 2 | archiver.setFileMode( fileMode ); |
89 | 2 | fileModeSet = true; |
90 | |
} |
91 | |
|
92 | 7 | if ( directory.exists() ) |
93 | |
{ |
94 | |
List<String> directoryExcludes; |
95 | 6 | if ( excludes != null && !excludes.isEmpty() ) |
96 | |
{ |
97 | 3 | directoryExcludes = new ArrayList<String>( excludes ); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 3 | directoryExcludes = new ArrayList<String>(); |
102 | |
} |
103 | |
|
104 | |
try |
105 | |
{ |
106 | 6 | String[] includesArray = null; |
107 | 6 | if ( includes != null && !includes.isEmpty() ) |
108 | |
{ |
109 | 1 | includesArray = new String[includes.size()]; |
110 | |
|
111 | 1 | int i = 0; |
112 | 1 | for ( final Iterator<String> it = includes.iterator(); it.hasNext(); ) |
113 | |
{ |
114 | 1 | String value = it.next(); |
115 | 1 | if ( value.startsWith( "./" ) || value.startsWith( ".\\" ) ) |
116 | |
{ |
117 | 0 | value = value.substring( 2 ); |
118 | |
} |
119 | |
|
120 | 1 | if ( value.startsWith( "/" ) || value.startsWith( "\\" ) ) |
121 | |
{ |
122 | 0 | value = value.substring( 1 ); |
123 | |
} |
124 | |
|
125 | 1 | includesArray[i] = value; |
126 | |
|
127 | 1 | i++; |
128 | 1 | } |
129 | |
} |
130 | |
|
131 | |
|
132 | 6 | final String[] excludesArray = new String[directoryExcludes.size()]; |
133 | |
|
134 | 6 | int i = 0; |
135 | 6 | for ( final Iterator<String> it = directoryExcludes.iterator(); it.hasNext(); ) |
136 | |
{ |
137 | 5 | String value = it.next(); |
138 | 5 | if ( value.startsWith( "./" ) || value.startsWith( ".\\" ) ) |
139 | |
{ |
140 | 0 | value = value.substring( 2 ); |
141 | |
} |
142 | |
|
143 | 5 | if ( value.startsWith( "/" ) || value.startsWith( "\\" ) ) |
144 | |
{ |
145 | 0 | value = value.substring( 1 ); |
146 | |
} |
147 | |
|
148 | 5 | excludesArray[i] = value; |
149 | |
|
150 | 5 | i++; |
151 | 5 | } |
152 | |
|
153 | 6 | final DefaultFileSet fs = new DefaultFileSet(); |
154 | 6 | fs.setUsingDefaultExcludes( useDefaultExcludes ); |
155 | 6 | fs.setPrefix( outputDirectory ); |
156 | 6 | fs.setDirectory( directory ); |
157 | 6 | fs.setIncludes( includesArray ); |
158 | 6 | fs.setExcludes( excludesArray ); |
159 | |
|
160 | 6 | archiver.addFileSet( fs ); |
161 | |
} |
162 | 0 | catch ( final ArchiverException e ) |
163 | |
{ |
164 | 0 | throw new ArchiveCreationException( "Error adding directory to archive: " + e.getMessage(), e ); |
165 | 6 | } |
166 | |
} |
167 | |
} |
168 | |
finally |
169 | |
{ |
170 | 7 | if ( dirModeSet ) |
171 | |
{ |
172 | 2 | archiver.setDirectoryMode( oldDirMode ); |
173 | |
} |
174 | |
|
175 | 7 | if ( fileModeSet ) |
176 | |
{ |
177 | 2 | archiver.setFileMode( oldFileMode ); |
178 | |
} |
179 | |
} |
180 | 7 | } |
181 | |
|
182 | |
public void setExcludes( final List<String> excludes ) |
183 | |
{ |
184 | 3 | this.excludes = excludes; |
185 | 3 | } |
186 | |
|
187 | |
public void setIncludes( final List<String> includes ) |
188 | |
{ |
189 | 3 | this.includes = includes; |
190 | 3 | } |
191 | |
|
192 | |
public void setOutputDirectory( final String outputDirectory ) |
193 | |
{ |
194 | 6 | this.outputDirectory = outputDirectory; |
195 | 6 | } |
196 | |
|
197 | |
public void setDirectoryMode( final int directoryMode ) |
198 | |
{ |
199 | 2 | this.directoryMode = directoryMode; |
200 | 2 | } |
201 | |
|
202 | |
public void setFileMode( final int fileMode ) |
203 | |
{ |
204 | 2 | this.fileMode = fileMode; |
205 | 2 | } |
206 | |
|
207 | |
public void setUseDefaultExcludes( final boolean useDefaultExcludes ) |
208 | |
{ |
209 | 2 | this.useDefaultExcludes = useDefaultExcludes; |
210 | 2 | } |
211 | |
|
212 | |
} |