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