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