1 package org.apache.maven.plugin.war;
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.archiver.MavenArchiver;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.DependencyResolutionRequiredException;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugin.war.util.ClassesPackager;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.plugins.annotations.ResolutionScope;
33 import org.apache.maven.project.MavenProjectHelper;
34 import org.codehaus.plexus.archiver.Archiver;
35 import org.codehaus.plexus.archiver.ArchiverException;
36 import org.codehaus.plexus.archiver.jar.ManifestException;
37 import org.codehaus.plexus.archiver.war.WarArchiver;
38 import org.codehaus.plexus.util.FileUtils;
39 import org.codehaus.plexus.util.StringUtils;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.Arrays;
44
45
46
47
48
49
50
51 @Mojo(
52 name = "war",
53 defaultPhase = LifecyclePhase.PACKAGE,
54 threadSafe = true,
55 requiresDependencyResolution = ResolutionScope.RUNTIME )
56 public class WarMojo
57 extends AbstractWarMojo
58 {
59
60
61
62 @Parameter( defaultValue = "${project.build.directory}", required = true )
63 private String outputDirectory;
64
65
66
67
68 @Parameter( defaultValue = "${project.build.finalName}", property = "war.warName", required = true )
69 private String warName;
70
71
72
73
74
75 @Parameter
76 private String classifier;
77
78
79
80
81
82
83
84
85 @Parameter
86 private String packagingExcludes;
87
88
89
90
91
92
93
94
95 @Parameter
96 private String packagingIncludes;
97
98
99
100
101 @Component( role = Archiver.class, hint = "war" )
102 private WarArchiver warArchiver;
103
104
105
106 @Component
107 private MavenProjectHelper projectHelper;
108
109
110
111
112
113 @Parameter( property = "primaryArtifact", defaultValue = "true" )
114 private boolean primaryArtifact = true;
115
116
117
118
119
120
121
122
123 @Parameter( property = "failOnMissingWebXml", defaultValue = "true" )
124 private boolean failOnMissingWebXml = true;
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 @Parameter( defaultValue = "false" )
150 private boolean attachClasses = false;
151
152
153
154
155
156
157 @Parameter( defaultValue = "classes" )
158 private String classesClassifier = "classes";
159
160
161
162
163
164
165
166
167
168
169 public void execute()
170 throws MojoExecutionException, MojoFailureException
171 {
172 File warFile = getTargetWarFile();
173
174 try
175 {
176 performPackaging( warFile );
177 }
178 catch ( DependencyResolutionRequiredException e )
179 {
180 throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
181 }
182 catch ( ManifestException e )
183 {
184 throw new MojoExecutionException( "Error assembling WAR", e );
185 }
186 catch ( IOException e )
187 {
188 throw new MojoExecutionException( "Error assembling WAR", e );
189 }
190 catch ( ArchiverException e )
191 {
192 throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207 private void performPackaging( File warFile )
208 throws IOException, ManifestException, DependencyResolutionRequiredException,
209 MojoExecutionException, MojoFailureException
210 {
211 getLog().info( "Packaging webapp" );
212
213 buildExplodedWebapp( getWebappDirectory() );
214
215 MavenArchiver archiver = new MavenArchiver();
216
217 archiver.setArchiver( warArchiver );
218
219 archiver.setOutputFile( warFile );
220
221 getLog().debug( "Excluding " + Arrays.asList( getPackagingExcludes() ) + " from the generated webapp archive." );
222 getLog().debug( "Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." );
223
224 warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
225
226 final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
227 if ( webXmlFile.exists() )
228 {
229 warArchiver.setWebxml( webXmlFile );
230 }
231
232 warArchiver.setRecompressAddedZips( isRecompressZippedFiles() );
233
234 warArchiver.setIncludeEmptyDirs( isIncludeEmptyDirectories() );
235
236 if ( !failOnMissingWebXml )
237 {
238 getLog().debug( "Build won't fail if web.xml file is missing." );
239
240 warArchiver.setIgnoreWebxml( false );
241 }
242
243
244 archiver.createArchive( getSession(), getProject(), getArchive() );
245
246
247 if ( isAttachClasses() )
248 {
249 if ( isArchiveClasses() && getJarArchiver().getDestFile() != null )
250 {
251
252 File targetClassesFile = getTargetClassesFile();
253 FileUtils.copyFile( getJarArchiver().getDestFile(), targetClassesFile );
254 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), targetClassesFile );
255 }
256 else
257 {
258 ClassesPackager packager = new ClassesPackager();
259 final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
260 if ( classesDirectory.exists() )
261 {
262 getLog().info( "Packaging classes" );
263 packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getSession(),
264 getProject(), getArchive() );
265 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
266 }
267 }
268 }
269
270 if ( this.classifier != null )
271 {
272 projectHelper.attachArtifact( getProject(), "war", this.classifier, warFile );
273 }
274 else
275 {
276 Artifact artifact = getProject().getArtifact();
277 if ( primaryArtifact )
278 {
279 artifact.setFile( warFile );
280 }
281 else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
282 {
283 artifact.setFile( warFile );
284 }
285 }
286 }
287
288 protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
289 {
290 if ( classifier == null )
291 {
292 classifier = "";
293 }
294 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
295 {
296 classifier = "-" + classifier;
297 }
298
299 return new File( basedir, finalName + classifier + "." + type );
300 }
301
302 protected File getTargetWarFile()
303 {
304 return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
305
306 }
307
308 protected File getTargetClassesFile()
309 {
310 return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
311 }
312
313
314
315 public String getClassifier()
316 {
317 return classifier;
318 }
319
320 public void setClassifier( String classifier )
321 {
322 this.classifier = classifier;
323 }
324
325 public String[] getPackagingExcludes()
326 {
327 if ( StringUtils.isEmpty( packagingExcludes ) )
328 {
329 return new String[0];
330 }
331 else
332 {
333 return StringUtils.split( packagingExcludes, "," );
334 }
335 }
336
337 public void setPackagingExcludes( String packagingExcludes )
338 {
339 this.packagingExcludes = packagingExcludes;
340 }
341
342 public String[] getPackagingIncludes()
343 {
344 if ( StringUtils.isEmpty( packagingIncludes ) )
345 {
346 return new String[] { "**" };
347 }
348 else
349 {
350 return StringUtils.split( packagingIncludes, "," );
351 }
352 }
353
354 public void setPackagingIncludes( String packagingIncludes )
355 {
356 this.packagingIncludes = packagingIncludes;
357 }
358
359 public String getOutputDirectory()
360 {
361 return outputDirectory;
362 }
363
364 public void setOutputDirectory( String outputDirectory )
365 {
366 this.outputDirectory = outputDirectory;
367 }
368
369 public String getWarName()
370 {
371 return warName;
372 }
373
374 public void setWarName( String warName )
375 {
376 this.warName = warName;
377 }
378
379 public WarArchiver getWarArchiver()
380 {
381 return warArchiver;
382 }
383
384 public void setWarArchiver( WarArchiver warArchiver )
385 {
386 this.warArchiver = warArchiver;
387 }
388
389 public MavenProjectHelper getProjectHelper()
390 {
391 return projectHelper;
392 }
393
394 public void setProjectHelper( MavenProjectHelper projectHelper )
395 {
396 this.projectHelper = projectHelper;
397 }
398
399 public boolean isPrimaryArtifact()
400 {
401 return primaryArtifact;
402 }
403
404 public void setPrimaryArtifact( boolean primaryArtifact )
405 {
406 this.primaryArtifact = primaryArtifact;
407 }
408
409 public boolean isAttachClasses()
410 {
411 return attachClasses;
412 }
413
414 public void setAttachClasses( boolean attachClasses )
415 {
416 this.attachClasses = attachClasses;
417 }
418
419 public String getClassesClassifier()
420 {
421 return classesClassifier;
422 }
423
424 public void setClassesClassifier( String classesClassifier )
425 {
426 this.classesClassifier = classesClassifier;
427 }
428
429 public boolean isFailOnMissingWebXml()
430 {
431 return failOnMissingWebXml;
432 }
433
434 public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
435 {
436 this.failOnMissingWebXml = failOnMissingWebXml;
437 }
438 }