1 package org.apache.maven.plugins.war;
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.net.MalformedURLException;
25 import java.net.URL;
26 import java.net.URLClassLoader;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import org.apache.maven.archiver.MavenArchiver;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.DependencyResolutionRequiredException;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.plugins.annotations.Component;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.plugins.annotations.ResolutionScope;
40 import org.apache.maven.plugins.war.util.ClassesPackager;
41 import org.apache.maven.project.MavenProjectHelper;
42 import org.codehaus.plexus.archiver.Archiver;
43 import org.codehaus.plexus.archiver.ArchiverException;
44 import org.codehaus.plexus.archiver.jar.ManifestException;
45 import org.codehaus.plexus.archiver.war.WarArchiver;
46 import org.codehaus.plexus.util.FileUtils;
47 import org.codehaus.plexus.util.StringUtils;
48
49
50
51
52
53
54
55
56 @Mojo( name = "war", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME )
57
58 public class WarMojo
59 extends AbstractWarMojo
60 {
61
62
63
64 @Parameter( defaultValue = "${project.build.directory}", required = true )
65 private String outputDirectory;
66
67
68
69
70 @Parameter( defaultValue = "${project.build.finalName}", required = true, readonly = true )
71 private String warName;
72
73
74
75
76
77 @Parameter
78 private String classifier;
79
80
81
82
83
84
85
86
87 @Parameter
88 private String packagingExcludes;
89
90
91
92
93
94
95
96
97 @Parameter
98 private String packagingIncludes;
99
100
101
102
103 @Component( role = Archiver.class, hint = "war" )
104 private WarArchiver warArchiver;
105
106
107
108 @Component
109 private MavenProjectHelper projectHelper;
110
111
112
113
114
115 @Parameter( defaultValue = "true" )
116 private boolean primaryArtifact;
117
118
119
120
121
122
123
124
125
126
127
128 @Parameter
129 private Boolean failOnMissingWebXml;
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 @Parameter( defaultValue = "false" )
154 private boolean attachClasses;
155
156
157
158
159
160
161 @Parameter( defaultValue = "classes" )
162 private String classesClassifier;
163
164
165
166
167
168
169
170 @Parameter( property = "maven.war.skip", defaultValue = "false" )
171 private boolean skip;
172
173
174
175
176
177
178
179
180
181
182
183 public void execute()
184 throws MojoExecutionException, MojoFailureException
185 {
186
187 if ( isSkip() )
188 {
189 getLog().info( "Skipping the execution." );
190 return;
191 }
192
193 File warFile = getTargetWarFile();
194
195 try
196 {
197 performPackaging( warFile );
198 }
199 catch ( DependencyResolutionRequiredException e )
200 {
201 throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
202 }
203 catch ( ManifestException e )
204 {
205 throw new MojoExecutionException( "Error assembling WAR", e );
206 }
207 catch ( IOException e )
208 {
209 throw new MojoExecutionException( "Error assembling WAR", e );
210 }
211 catch ( ArchiverException e )
212 {
213 throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
214 }
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228 private void performPackaging( File warFile )
229 throws IOException, ManifestException, DependencyResolutionRequiredException, MojoExecutionException,
230 MojoFailureException
231 {
232 getLog().info( "Packaging webapp" );
233
234 buildExplodedWebapp( getWebappDirectory() );
235
236 MavenArchiver archiver = new MavenArchiver();
237
238 archiver.setArchiver( warArchiver );
239
240 archiver.setOutputFile( warFile );
241
242
243 getLog().debug( "Excluding " + Arrays.asList( getPackagingExcludes() )
244 + " from the generated webapp archive." );
245 getLog().debug( "Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." );
246
247
248 warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
249
250 final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
251 if ( webXmlFile.exists() )
252 {
253 warArchiver.setWebxml( webXmlFile );
254 }
255
256 warArchiver.setRecompressAddedZips( isRecompressZippedFiles() );
257
258 warArchiver.setIncludeEmptyDirs( isIncludeEmptyDirectories() );
259
260 if ( Boolean.FALSE.equals( failOnMissingWebXml )
261 || ( failOnMissingWebXml == null && isProjectUsingAtLeastServlet30() ) )
262 {
263 getLog().debug( "Build won't fail if web.xml file is missing." );
264 warArchiver.setExpectWebXml( false );
265 }
266
267
268 archiver.createArchive( getSession(), getProject(), getArchive() );
269
270
271 if ( isAttachClasses() )
272 {
273 if ( isArchiveClasses() && getJarArchiver().getDestFile() != null )
274 {
275
276 File targetClassesFile = getTargetClassesFile();
277 FileUtils.copyFile( getJarArchiver().getDestFile(), targetClassesFile );
278 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), targetClassesFile );
279 }
280 else
281 {
282 ClassesPackager packager = new ClassesPackager();
283 final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
284 if ( classesDirectory.exists() )
285 {
286 getLog().info( "Packaging classes" );
287 packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getSession(),
288 getProject(), getArchive() );
289 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
290 }
291 }
292 }
293
294 if ( this.classifier != null )
295 {
296 projectHelper.attachArtifact( getProject(), "war", this.classifier, warFile );
297 }
298 else
299 {
300 Artifact artifact = getProject().getArtifact();
301 if ( primaryArtifact )
302 {
303 artifact.setFile( warFile );
304 }
305 else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
306 {
307 artifact.setFile( warFile );
308 }
309 }
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323 private boolean isProjectUsingAtLeastServlet30()
324 throws DependencyResolutionRequiredException, MalformedURLException
325 {
326 List<String> classpathElements = getProject().getCompileClasspathElements();
327 URL[] urls = new URL[classpathElements.size()];
328 for ( int i = 0; i < urls.length; i++ )
329 {
330 urls[i] = new File( classpathElements.get( i ) ).toURI().toURL();
331 }
332 ClassLoader loader = new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
333 try
334 {
335 Class.forName( "javax.servlet.annotation.WebServlet", false, loader );
336 return true;
337 }
338 catch ( ClassNotFoundException e )
339 {
340 return false;
341 }
342 }
343
344
345
346
347
348
349
350
351 protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
352 {
353 if ( classifier == null )
354 {
355 classifier = "";
356 }
357 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
358 {
359 classifier = "-" + classifier;
360 }
361
362 return new File( basedir, finalName + classifier + "." + type );
363 }
364
365
366
367
368 protected File getTargetWarFile()
369 {
370 return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
371
372 }
373
374
375
376
377 protected File getTargetClassesFile()
378 {
379 return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
380 }
381
382
383
384
385
386
387 public String getClassifier()
388 {
389 return classifier;
390 }
391
392
393
394
395 public void setClassifier( String classifier )
396 {
397 this.classifier = classifier;
398 }
399
400
401
402
403 public String[] getPackagingExcludes()
404 {
405 if ( StringUtils.isEmpty( packagingExcludes ) )
406 {
407 return new String[0];
408 }
409 else
410 {
411 return StringUtils.split( packagingExcludes, "," );
412 }
413 }
414
415
416
417
418 public void setPackagingExcludes( String packagingExcludes )
419 {
420 this.packagingExcludes = packagingExcludes;
421 }
422
423
424
425
426 public String[] getPackagingIncludes()
427 {
428 if ( StringUtils.isEmpty( packagingIncludes ) )
429 {
430 return new String[] { "**" };
431 }
432 else
433 {
434 return StringUtils.split( packagingIncludes, "," );
435 }
436 }
437
438
439
440
441 public void setPackagingIncludes( String packagingIncludes )
442 {
443 this.packagingIncludes = packagingIncludes;
444 }
445
446
447
448
449 public String getOutputDirectory()
450 {
451 return outputDirectory;
452 }
453
454
455
456
457 public void setOutputDirectory( String outputDirectory )
458 {
459 this.outputDirectory = outputDirectory;
460 }
461
462
463
464
465 public String getWarName()
466 {
467 return warName;
468 }
469
470
471
472
473 public void setWarName( String warName )
474 {
475 this.warName = warName;
476 }
477
478
479
480
481 public WarArchiver getWarArchiver()
482 {
483 return warArchiver;
484 }
485
486
487
488
489 public void setWarArchiver( WarArchiver warArchiver )
490 {
491 this.warArchiver = warArchiver;
492 }
493
494
495
496
497 public MavenProjectHelper getProjectHelper()
498 {
499 return projectHelper;
500 }
501
502
503
504
505 public void setProjectHelper( MavenProjectHelper projectHelper )
506 {
507 this.projectHelper = projectHelper;
508 }
509
510
511
512
513 public boolean isPrimaryArtifact()
514 {
515 return primaryArtifact;
516 }
517
518
519
520
521 public void setPrimaryArtifact( boolean primaryArtifact )
522 {
523 this.primaryArtifact = primaryArtifact;
524 }
525
526
527
528
529 public boolean isAttachClasses()
530 {
531 return attachClasses;
532 }
533
534
535
536
537 public void setAttachClasses( boolean attachClasses )
538 {
539 this.attachClasses = attachClasses;
540 }
541
542
543
544
545 public String getClassesClassifier()
546 {
547 return classesClassifier;
548 }
549
550
551
552
553 public void setClassesClassifier( String classesClassifier )
554 {
555 this.classesClassifier = classesClassifier;
556 }
557
558
559
560
561 public boolean isFailOnMissingWebXml()
562 {
563 return failOnMissingWebXml;
564 }
565
566
567
568
569 public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
570 {
571 this.failOnMissingWebXml = failOnMissingWebXml;
572 }
573
574 public boolean isSkip()
575 {
576 return skip;
577 }
578 }