-
Notifications
You must be signed in to change notification settings - Fork 88
/
WarProjectPackagingTask.java
381 lines (342 loc) · 14.8 KB
/
WarProjectPackagingTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package org.apache.maven.plugins.war.packaging;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.war.Overlay;
import org.apache.maven.plugins.war.util.PathSet;
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;
/**
* Handles the project own resources, that is:
* <ul>
* <li>The list of web resources, if any</li>
* <li>The content of the webapp directory if it exists</li>
* <li>The custom deployment descriptor(s), if any</li>
* <li>The content of the classes directory if it exists</li>
* <li>The dependencies of the project</li>
* </ul>
*
* @author Stephane Nicoll
*/
public class WarProjectPackagingTask
extends AbstractWarPackagingTask
{
private final Resource[] webResources;
private final File webXml;
private final File containerConfigXML;
private final String id;
private Overlay currentProjectOverlay;
/**
* @param webResources {@link #webResources}
* @param webXml {@link #webXml}
* @param containerConfigXml {@link #containerConfigXML}
* @param currentProjectOverlay {@link #currentProjectOverlay}
*/
public WarProjectPackagingTask( Resource[] webResources, File webXml, File containerConfigXml,
Overlay currentProjectOverlay )
{
if ( webResources != null )
{
this.webResources = webResources;
}
else
{
this.webResources = new Resource[0];
}
this.webXml = webXml;
this.containerConfigXML = containerConfigXml;
this.currentProjectOverlay = currentProjectOverlay;
this.id = currentProjectOverlay.getId();
}
@Override
public void performPackaging( WarPackagingContext context )
throws MojoExecutionException, MojoFailureException
{
context.getLog().info( "Processing war project" );
// Prepare the INF directories
File webinfDir = new File( context.getWebappDirectory(), WEB_INF_PATH );
webinfDir.mkdirs();
File metainfDir = new File( context.getWebappDirectory(), META_INF_PATH );
metainfDir.mkdirs();
handleWebResources( context );
handleWebAppSourceDirectory( context );
// Debug mode: dump the path set for the current build
PathSet pathSet = context.getWebappStructure().getStructure( "currentBuild" );
context.getLog().debug( "Dump of the current build pathSet content -->" );
for ( String path : pathSet )
{
context.getLog().debug( path );
}
context.getLog().debug( "-- end of dump --" );
handleDeploymentDescriptors( context, webinfDir, metainfDir, context.isFailOnMissingWebXml() );
handleClassesDirectory( context );
handleArtifacts( context );
if ( !context.getWebappDirectory().mkdirs() )
{
context.deleteOutdatedResources();
}
}
/**
* Handles the web resources.
*
* @param context the packaging context
* @throws MojoExecutionException if a resource could not be copied
*/
protected void handleWebResources( WarPackagingContext context )
throws MojoExecutionException
{
for ( Resource resource : webResources )
{
// MWAR-246
if ( resource.getDirectory() == null )
{
throw new MojoExecutionException( "The <directory> tag is missing from the <resource> tag." );
}
if ( !( new File( resource.getDirectory() ) ).isAbsolute() )
{
resource.setDirectory( context.getProject().getBasedir() + File.separator + resource.getDirectory() );
}
// Make sure that the resource directory is not the same as the webappDirectory
if ( !resource.getDirectory().equals( context.getWebappDirectory().getPath() ) )
{
try
{
copyResources( context, resource );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Could not copy resource [" + resource.getDirectory() + "]", e );
}
}
}
}
/**
* Handles the webapp sources.
*
* @param context the packaging context
* @throws MojoExecutionException if the sources could not be copied
*/
protected void handleWebAppSourceDirectory( WarPackagingContext context )
throws MojoExecutionException
{
// CHECKSTYLE_OFF: LineLength
if ( !context.getWebappSourceDirectory().exists() )
{
context.getLog().debug( "webapp sources directory does not exist - skipping." );
}
else if ( !context.getWebappSourceDirectory().getAbsolutePath().equals( context.getWebappDirectory().getPath() ) )
{
context.getLog().info( "Copying webapp resources [" + context.getWebappSourceDirectory() + "]" );
final PathSet sources =
getFilesToIncludes( context.getWebappSourceDirectory(), context.getWebappSourceIncludes(),
context.getWebappSourceExcludes(), context.isWebappSourceIncludeEmptyDirectories() );
try
{
copyFiles( id, context, context.getWebappSourceDirectory(), sources, false );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Could not copy webapp sources ["
+ context.getWebappDirectory().getAbsolutePath() + "]", e );
}
}
// CHECKSTYLE_ON: LineLength
}
/**
* Handles the webapp artifacts.
*
* @param context the packaging context
* @throws MojoExecutionException if the artifacts could not be packaged
*/
protected void handleArtifacts( WarPackagingContext context )
throws MojoExecutionException
{
ArtifactsPackagingTask task =
new ArtifactsPackagingTask( context.getProject().getArtifacts(), currentProjectOverlay );
task.performPackaging( context );
}
/**
* Handles the webapp classes.
*
* @param context the packaging context
* @throws MojoExecutionException if the classes could not be packaged
*/
protected void handleClassesDirectory( WarPackagingContext context )
throws MojoExecutionException
{
ClassesPackagingTask task = new ClassesPackagingTask( currentProjectOverlay );
task.performPackaging( context );
}
/**
* Handles the deployment descriptors, if specified. Note that the behavior here is slightly different since the
* customized entry always win, even if an overlay has already packaged a web.xml previously.
*
* @param context the packaging context
* @param webinfDir the web-inf directory
* @param metainfDir the meta-inf directory
* @param failOnMissingWebXml if build should fail if web.xml is not found
* @throws MojoFailureException if the web.xml is specified but does not exist and failOnMissingWebXml is true
* @throws MojoExecutionException if an error occurred while copying the descriptors
*/
protected void handleDeploymentDescriptors( WarPackagingContext context, File webinfDir, File metainfDir,
Boolean failOnMissingWebXml )
throws MojoFailureException, MojoExecutionException
{
try
{
if ( webXml != null && StringUtils.isNotEmpty( webXml.getName() ) )
{
if ( !webXml.exists()
&& ( failOnMissingWebXml == null || Boolean.TRUE.equals( failOnMissingWebXml ) ) )
{
throw new MojoFailureException( "The specified web.xml file '" + webXml + "' does not exist" );
}
// Making sure that it won't get overlayed
context.getWebappStructure().registerFileForced( id, WEB_INF_PATH + "/web.xml" );
if ( context.isFilteringDeploymentDescriptors() )
{
context.getMavenFileFilter().copyFile( webXml, new File( webinfDir, "web.xml" ), true,
context.getFilterWrappers(), getEncoding( webXml ) );
}
else
{
copyFile( context, webXml, new File( webinfDir, "web.xml" ), "WEB-INF/web.xml", true );
}
}
else
{
// the webXml can be the default one
File defaultWebXml = new File( context.getWebappSourceDirectory(), WEB_INF_PATH + "/web.xml" );
// if exists we can filter it
if ( defaultWebXml.exists() && context.isFilteringDeploymentDescriptors() )
{
context.getWebappStructure().registerFile( id, WEB_INF_PATH + "/web.xml" );
context.getMavenFileFilter().copyFile( defaultWebXml, new File( webinfDir, "web.xml" ), true,
context.getFilterWrappers(), getEncoding( defaultWebXml ) );
}
}
if ( containerConfigXML != null && StringUtils.isNotEmpty( containerConfigXML.getName() ) )
{
String xmlFileName = containerConfigXML.getName();
context.getWebappStructure().registerFileForced( id, META_INF_PATH + "/" + xmlFileName );
if ( context.isFilteringDeploymentDescriptors() )
{
context.getMavenFileFilter().copyFile( containerConfigXML, new File( metainfDir, xmlFileName ),
true, context.getFilterWrappers(),
getEncoding( containerConfigXML ) );
}
else
{
copyFile( context, containerConfigXML, new File( metainfDir, xmlFileName ), "META-INF/"
+ xmlFileName, true );
}
}
}
catch ( IOException e )
{
if ( failOnMissingWebXml == null || Boolean.TRUE.equals( failOnMissingWebXml ) )
{
throw new MojoExecutionException( "Failed to copy deployment descriptor", e );
}
}
catch ( MavenFilteringException e )
{
throw new MojoExecutionException( "Failed to copy deployment descriptor", e );
}
}
/**
* Copies webapp webResources from the specified directory.
*
* @param context the WAR packaging context to use
* @param resource the resource to copy
* @throws IOException if an error occurred while copying the resources
* @throws MojoExecutionException if an error occurred while retrieving the filter properties
*/
public void copyResources( WarPackagingContext context, Resource resource )
throws IOException, MojoExecutionException
{
if ( !context.getWebappDirectory().exists() )
{
context.getLog().warn( "Not copying webapp webResources [" + resource.getDirectory()
+ "]: webapp directory [" + context.getWebappDirectory().getAbsolutePath()
+ "] does not exist!" );
}
context.getLog().info( "Copying webapp webResources [" + resource.getDirectory() + "] to ["
+ context.getWebappDirectory().getAbsolutePath() + "]" );
String[] fileNames = getFilesToCopy( resource );
for ( String fileName : fileNames )
{
String targetFileName = fileName;
if ( resource.getTargetPath() != null )
{
// TODO make sure this thing is 100% safe
// MWAR-129 if targetPath is only a dot <targetPath>.</targetPath> or ./
// and the Resource is in a part of the warSourceDirectory the file from sources will override this
// that's we don't have to add the targetPath yep not nice but works
if ( !Objects.equals( ".", resource.getTargetPath() )
&& !Objects.equals( "./", resource.getTargetPath() ) )
{
targetFileName = resource.getTargetPath() + File.separator + targetFileName;
}
}
if ( resource.isFiltering() && !context.isNonFilteredExtension( fileName ) )
{
copyFilteredFile( id, context, new File( resource.getDirectory(), fileName ), targetFileName );
}
else
{
copyFile( id, context, new File( resource.getDirectory(), fileName ), targetFileName );
}
}
}
/**
* Returns a list of filenames that should be copied over to the destination directory.
*
* @param resource the resource to be scanned
* @return the array of filenames, relative to the sourceDir
*/
private String[] getFilesToCopy( Resource resource )
{
// CHECKSTYLE_OFF: LineLength
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( resource.getDirectory() );
if ( resource.getIncludes() != null && !resource.getIncludes().isEmpty() )
{
scanner.setIncludes( resource.getIncludes().toArray( new String[resource.getIncludes().size()] ) );
}
else
{
scanner.setIncludes( DEFAULT_INCLUDES );
}
if ( resource.getExcludes() != null && !resource.getExcludes().isEmpty() )
{
scanner.setExcludes( resource.getExcludes().toArray( new String[resource.getExcludes().size()] ) );
}
scanner.addDefaultExcludes();
scanner.scan();
return scanner.getIncludedFiles();
// CHECKSTYLE_ON: LineLength
}
}