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