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