1 | |
package org.apache.maven.plugin.war.packaging; |
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.model.Resource; |
23 | |
import org.apache.maven.plugin.MojoExecutionException; |
24 | |
import org.apache.maven.plugin.MojoFailureException; |
25 | |
import org.apache.maven.plugin.war.Overlay; |
26 | |
import org.apache.maven.plugin.war.util.PathSet; |
27 | |
import org.apache.maven.shared.filtering.MavenFilteringException; |
28 | |
import org.codehaus.plexus.util.DirectoryScanner; |
29 | |
import org.codehaus.plexus.util.IOUtil; |
30 | |
import org.codehaus.plexus.util.StringUtils; |
31 | |
import org.codehaus.plexus.util.xml.XmlStreamReader; |
32 | |
|
33 | |
import java.io.File; |
34 | |
import java.io.IOException; |
35 | |
import java.util.Iterator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class WarProjectPackagingTask |
51 | |
extends AbstractWarPackagingTask |
52 | |
{ |
53 | |
private final Resource[] webResources; |
54 | |
|
55 | |
private final File webXml; |
56 | |
|
57 | |
private final File containerConfigXML; |
58 | |
|
59 | |
private final String id; |
60 | |
|
61 | |
private Overlay currentProjectOverlay; |
62 | |
|
63 | |
|
64 | |
public WarProjectPackagingTask( Resource[] webResources, File webXml, File containerConfigXml, |
65 | |
Overlay currentProjectOverlay ) |
66 | 61 | { |
67 | 61 | if ( webResources != null ) |
68 | |
{ |
69 | 6 | this.webResources = webResources; |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 55 | this.webResources = new Resource[0]; |
74 | |
} |
75 | 61 | this.webXml = webXml; |
76 | 61 | this.containerConfigXML = containerConfigXml; |
77 | 61 | this.currentProjectOverlay = currentProjectOverlay; |
78 | 61 | this.id = currentProjectOverlay.getId(); |
79 | 61 | } |
80 | |
|
81 | |
public void performPackaging( WarPackagingContext context ) |
82 | |
throws MojoExecutionException, MojoFailureException |
83 | |
{ |
84 | |
|
85 | 61 | context.getLog().info( "Processing war project" ); |
86 | |
|
87 | 61 | File webinfDir = new File( context.getWebappDirectory(), WEB_INF_PATH ); |
88 | 61 | webinfDir.mkdirs(); |
89 | 61 | File metainfDir = new File( context.getWebappDirectory(), META_INF_PATH ); |
90 | 61 | metainfDir.mkdirs(); |
91 | |
|
92 | 61 | handleWebResources( context ); |
93 | |
|
94 | 61 | handeWebAppSourceDirectory( context ); |
95 | |
|
96 | |
|
97 | 61 | PathSet pathSet = context.getWebappStructure().getStructure( "currentBuild" ); |
98 | 61 | context.getLog().debug( "Dump of the current build pathSet content -->" ); |
99 | 61 | for ( Iterator iterator = pathSet.iterator(); iterator.hasNext(); ) |
100 | |
{ |
101 | 126 | context.getLog().debug( "" + iterator.next() ); |
102 | |
} |
103 | 61 | context.getLog().debug( "-- end of dump --" ); |
104 | |
|
105 | 61 | handleDeploymentDescriptors( context, webinfDir, metainfDir ); |
106 | |
|
107 | 61 | handleClassesDirectory( context ); |
108 | |
|
109 | 61 | handleArtifacts( context ); |
110 | 61 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
protected void handleWebResources( WarPackagingContext context ) |
120 | |
throws MojoExecutionException |
121 | |
{ |
122 | 67 | for ( int i = 0; i < webResources.length; i++ ) |
123 | |
{ |
124 | 6 | Resource resource = webResources[i]; |
125 | 6 | if ( !( new File( resource.getDirectory() ) ).isAbsolute() ) |
126 | |
{ |
127 | 0 | resource.setDirectory( context.getProject().getBasedir() + File.separator + resource.getDirectory() ); |
128 | |
} |
129 | |
|
130 | |
|
131 | 6 | if ( !resource.getDirectory().equals( context.getWebappDirectory().getPath() ) ) |
132 | |
{ |
133 | |
|
134 | |
try |
135 | |
{ |
136 | 6 | copyResources( context, resource ); |
137 | |
} |
138 | 0 | catch ( IOException e ) |
139 | |
{ |
140 | 0 | throw new MojoExecutionException( "Could not copy resource [" + resource.getDirectory() + "]", e ); |
141 | 6 | } |
142 | |
} |
143 | |
} |
144 | 61 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
protected void handeWebAppSourceDirectory( WarPackagingContext context ) |
153 | |
throws MojoExecutionException |
154 | |
{ |
155 | 61 | if ( !context.getWebappSourceDirectory().exists() ) |
156 | |
{ |
157 | 0 | context.getLog().debug( "webapp sources directory does not exist - skipping." ); |
158 | |
} |
159 | 61 | else if ( !context.getWebappSourceDirectory().getAbsolutePath().equals( |
160 | |
context.getWebappDirectory().getPath() ) ) |
161 | |
{ |
162 | 60 | context.getLog().info( "Copying webapp resources [" + context.getWebappSourceDirectory() + "]" ); |
163 | 60 | final PathSet sources = |
164 | |
getFilesToIncludes( context.getWebappSourceDirectory(), context.getWebappSourceIncludes(), |
165 | |
context.getWebappSourceExcludes() ); |
166 | |
|
167 | |
try |
168 | |
{ |
169 | 60 | copyFiles( id, context, context.getWebappSourceDirectory(), sources, false ); |
170 | |
} |
171 | 0 | catch ( IOException e ) |
172 | |
{ |
173 | 0 | throw new MojoExecutionException( |
174 | |
"Could not copy webapp sources [" + context.getWebappDirectory().getAbsolutePath() + "]", e ); |
175 | 60 | } |
176 | |
} |
177 | 61 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
protected void handleArtifacts( WarPackagingContext context ) |
186 | |
throws MojoExecutionException |
187 | |
{ |
188 | 61 | ArtifactsPackagingTask task = new ArtifactsPackagingTask( context.getProject().getArtifacts(), |
189 | |
currentProjectOverlay ); |
190 | 61 | task.performPackaging( context ); |
191 | 61 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
protected void handleClassesDirectory( WarPackagingContext context ) |
200 | |
throws MojoExecutionException |
201 | |
{ |
202 | 61 | ClassesPackagingTask task = new ClassesPackagingTask( currentProjectOverlay ); |
203 | 61 | task.performPackaging( context ); |
204 | 61 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
protected void handleDeploymentDescriptors( WarPackagingContext context, File webinfDir, File metainfDir ) |
218 | |
throws MojoFailureException, MojoExecutionException |
219 | |
{ |
220 | |
try |
221 | |
{ |
222 | 61 | if ( webXml != null && StringUtils.isNotEmpty( webXml.getName() ) ) |
223 | |
{ |
224 | 23 | if ( !webXml.exists() ) |
225 | |
{ |
226 | 0 | throw new MojoFailureException( "The specified web.xml file '" + webXml + "' does not exist" ); |
227 | |
} |
228 | |
|
229 | |
|
230 | 23 | context.getWebappStructure().registerFileForced( id, WEB_INF_PATH + "/web.xml" ); |
231 | |
|
232 | 23 | if ( context.isFilteringDeploymentDescriptors() ) |
233 | |
{ |
234 | 0 | context.getMavenFileFilter().copyFile( webXml, new File( webinfDir, "web.xml" ), true, |
235 | |
context.getFilterWrappers(), getEncoding( webXml ) ); |
236 | |
} |
237 | |
else |
238 | |
{ |
239 | 23 | copyFile( context, webXml, new File( webinfDir, "web.xml" ), "WEB-INF/web.xml", true ); |
240 | |
} |
241 | |
} |
242 | |
else |
243 | |
{ |
244 | |
|
245 | 38 | File defaultWebXml = new File( context.getWebappSourceDirectory(), WEB_INF_PATH + "/web.xml" ); |
246 | |
|
247 | 38 | if ( defaultWebXml.exists() && context.isFilteringDeploymentDescriptors() ) |
248 | |
{ |
249 | 0 | context.getWebappStructure().registerFile( id, WEB_INF_PATH + "/web.xml" ); |
250 | 0 | context.getMavenFileFilter().copyFile( defaultWebXml, new File( webinfDir, "web.xml" ), true, |
251 | |
context.getFilterWrappers(), getEncoding( defaultWebXml ) ); |
252 | |
} |
253 | |
} |
254 | |
|
255 | 61 | if ( containerConfigXML != null && StringUtils.isNotEmpty( containerConfigXML.getName() ) ) |
256 | |
{ |
257 | 2 | String xmlFileName = containerConfigXML.getName(); |
258 | |
|
259 | 2 | context.getWebappStructure().registerFileForced( id, META_INF_PATH + "/" + xmlFileName ); |
260 | |
|
261 | 2 | if ( context.isFilteringDeploymentDescriptors() ) |
262 | |
{ |
263 | 0 | context.getMavenFileFilter().copyFile( containerConfigXML, new File( metainfDir, xmlFileName ), |
264 | |
true, context.getFilterWrappers(), |
265 | |
getEncoding( containerConfigXML ) ); |
266 | |
} |
267 | |
else |
268 | |
{ |
269 | 2 | copyFile( context, containerConfigXML, new File( metainfDir, xmlFileName ), |
270 | |
"META-INF/" + xmlFileName, true ); |
271 | |
} |
272 | |
} |
273 | |
} |
274 | 0 | catch ( IOException e ) |
275 | |
{ |
276 | 0 | throw new MojoExecutionException( "Failed to copy deployment descriptor", e ); |
277 | |
} |
278 | 0 | catch ( MavenFilteringException e ) |
279 | |
{ |
280 | 0 | throw new MojoExecutionException( "Failed to copy deployment descriptor", e ); |
281 | 61 | } |
282 | 61 | } |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
private String getEncoding( File webXml ) |
292 | |
throws IOException |
293 | |
{ |
294 | 0 | XmlStreamReader xmlReader = new XmlStreamReader( webXml ); |
295 | |
try |
296 | |
{ |
297 | 0 | return xmlReader.getEncoding(); |
298 | |
} |
299 | |
finally |
300 | |
{ |
301 | 0 | IOUtil.close( xmlReader ); |
302 | |
} |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
public void copyResources( WarPackagingContext context, Resource resource ) |
314 | |
throws IOException, MojoExecutionException |
315 | |
{ |
316 | 6 | if ( !context.getWebappDirectory().exists() ) |
317 | |
{ |
318 | 0 | context.getLog().warn( |
319 | |
"Not copying webapp webResources [" + resource.getDirectory() + "]: webapp directory [" |
320 | |
+ context.getWebappDirectory().getAbsolutePath() + "] does not exist!" ); |
321 | |
} |
322 | |
|
323 | 6 | context.getLog().info( "Copying webapp webResources [" + resource.getDirectory() + "] to [" |
324 | |
+ context.getWebappDirectory().getAbsolutePath() + "]" ); |
325 | 6 | String[] fileNames = getFilesToCopy( resource ); |
326 | 15 | for ( int i = 0; i < fileNames.length; i++ ) |
327 | |
{ |
328 | 9 | String targetFileName = fileNames[i]; |
329 | 9 | if ( resource.getTargetPath() != null ) |
330 | |
{ |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | 1 | if ( !StringUtils.equals( ".", resource.getTargetPath() ) |
336 | |
&& !StringUtils.equals( "./", resource.getTargetPath() ) ) |
337 | |
{ |
338 | 1 | targetFileName = resource.getTargetPath() + File.separator + targetFileName; |
339 | |
} |
340 | |
} |
341 | 9 | if ( resource.isFiltering() && !context.isNonFilteredExtension( fileNames[i] ) ) |
342 | |
{ |
343 | 6 | copyFilteredFile( id, context, new File( resource.getDirectory(), fileNames[i] ), targetFileName ); |
344 | |
} |
345 | |
else |
346 | |
{ |
347 | 3 | copyFile( id, context, new File( resource.getDirectory(), fileNames[i] ), targetFileName ); |
348 | |
} |
349 | |
} |
350 | 6 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
private String[] getFilesToCopy( Resource resource ) |
361 | |
{ |
362 | 6 | DirectoryScanner scanner = new DirectoryScanner(); |
363 | 6 | scanner.setBasedir( resource.getDirectory() ); |
364 | 6 | if ( resource.getIncludes() != null && !resource.getIncludes().isEmpty() ) |
365 | |
{ |
366 | 0 | scanner.setIncludes( |
367 | |
(String[]) resource.getIncludes().toArray( new String[resource.getIncludes().size()] ) ); |
368 | |
} |
369 | |
else |
370 | |
{ |
371 | 6 | scanner.setIncludes( DEFAULT_INCLUDES ); |
372 | |
} |
373 | 6 | if ( resource.getExcludes() != null && !resource.getExcludes().isEmpty() ) |
374 | |
{ |
375 | 0 | scanner.setExcludes( |
376 | |
(String[]) resource.getExcludes().toArray( new String[resource.getExcludes().size()] ) ); |
377 | |
} |
378 | |
|
379 | 6 | scanner.addDefaultExcludes(); |
380 | |
|
381 | 6 | scanner.scan(); |
382 | |
|
383 | 6 | return scanner.getIncludedFiles(); |
384 | |
} |
385 | |
} |