View Javadoc
1   package org.apache.maven.plugin.war;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Build a WAR file.
47   *
48   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
49   * @version $Id: WarMojo.html 935522 2015-01-08 20:15:45Z khmarbaise $
50   */
51  // CHECKSTYLE_OFF: LineLength
52  @Mojo( name = "war", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME )
53  //CHECKSTYLE_ON: LineLength
54  public class WarMojo
55      extends AbstractWarMojo
56  {
57      /**
58       * The directory for the generated WAR.
59       */
60      @Parameter( defaultValue = "${project.build.directory}", required = true )
61      private String outputDirectory;
62  
63      /**
64       * The name of the generated WAR.
65       */
66      @Parameter( defaultValue = "${project.build.finalName}", property = "war.warName", required = true )
67      private String warName;
68  
69      /**
70       * Classifier to add to the generated WAR. If given, the artifact will be an attachment instead. The classifier will
71       * not be applied to the JAR file of the project - only to the WAR file.
72       */
73      @Parameter
74      private String classifier;
75  
76      /**
77       * The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement
78       * the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude
79       * specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
80       *
81       * @since 2.1-alpha-2
82       */
83      @Parameter
84      private String packagingExcludes;
85  
86      /**
87       * The comma separated list of tokens to include in the WAR before packaging. By default everything is included.
88       * This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions
89       * engine to include and exclude specific pattern using the expression %regex[].
90       *
91       * @since 2.1-beta-1
92       */
93      @Parameter
94      private String packagingIncludes;
95  
96      /**
97       * The WAR archiver.
98       */
99      @Component( role = Archiver.class, hint = "war" )
100     private WarArchiver warArchiver;
101 
102     /**
103      */
104     @Component
105     private MavenProjectHelper projectHelper;
106 
107     /**
108      * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or deploy
109      * it to the local repository instead of the default one in an execution.
110      */
111     @Parameter( property = "primaryArtifact", defaultValue = "true" )
112     private boolean primaryArtifact = true;
113 
114     /**
115      * Whether or not to fail the build if the <code>web.xml</code> file is missing. Set to <code>false</code> if you
116      * want you WAR built without a <code>web.xml</code> file. This may be useful if you are building an overlay that
117      * has no web.xml file.
118      *
119      * @since 2.1-alpha-2
120      */
121     @Parameter( property = "failOnMissingWebXml", defaultValue = "true" )
122     private boolean failOnMissingWebXml = true;
123 
124     /**
125      * Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the project as an
126      * additional artifact.
127      * <p>
128      * By default the classifier for the additional artifact is 'classes'. You can change it with the
129      * <code><![CDATA[<classesClassifier>someclassifier</classesClassifier>]]></code> parameter.
130      * </p>
131      * <p>
132      * If this parameter true, another project can depend on the classes by writing something like:
133      * 
134      * <pre>
135      * <![CDATA[<dependency>
136      *   <groupId>myGroup</groupId>
137      *   <artifactId>myArtifact</artifactId>
138      *   <version>myVersion</myVersion>
139      *   <classifier>classes</classifier>
140      * </dependency>]]>
141      * </pre>
142      * 
143      * </p>
144      *
145      * @since 2.1-alpha-2
146      */
147     @Parameter( defaultValue = "false" )
148     private boolean attachClasses = false;
149 
150     /**
151      * The classifier to use for the attached classes artifact.
152      *
153      * @since 2.1-alpha-2
154      */
155     @Parameter( defaultValue = "classes" )
156     private String classesClassifier = "classes";
157 
158     // ----------------------------------------------------------------------
159     // Implementation
160     // ----------------------------------------------------------------------
161 
162     /**
163      * Executes the WarMojo on the current project.
164      *
165      * @throws MojoExecutionException if an error occurred while building the webapp
166      * @throws MojoFailureException if an error.
167      */
168     public void execute()
169         throws MojoExecutionException, MojoFailureException
170     {
171         File warFile = getTargetWarFile();
172 
173         try
174         {
175             performPackaging( warFile );
176         }
177         catch ( DependencyResolutionRequiredException e )
178         {
179             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
180         }
181         catch ( ManifestException e )
182         {
183             throw new MojoExecutionException( "Error assembling WAR", e );
184         }
185         catch ( IOException e )
186         {
187             throw new MojoExecutionException( "Error assembling WAR", e );
188         }
189         catch ( ArchiverException e )
190         {
191             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
192         }
193     }
194 
195     /**
196      * Generates the webapp according to the <tt>mode</tt> attribute.
197      *
198      * @param warFile the target WAR file
199      * @throws IOException if an error occurred while copying files
200      * @throws ArchiverException if the archive could not be created
201      * @throws ManifestException if the manifest could not be created
202      * @throws DependencyResolutionRequiredException if an error occurred while resolving the dependencies
203      * @throws MojoExecutionException if the execution failed
204      * @throws MojoFailureException if a fatal exception occurred
205      */
206     private void performPackaging( File warFile )
207         throws IOException, ManifestException, DependencyResolutionRequiredException, MojoExecutionException,
208         MojoFailureException
209     {
210         getLog().info( "Packaging webapp" );
211 
212         buildExplodedWebapp( getWebappDirectory() );
213 
214         MavenArchiver archiver = new MavenArchiver();
215 
216         archiver.setArchiver( warArchiver );
217 
218         archiver.setOutputFile( warFile );
219 
220         // CHECKSTYLE_OFF: LineLength
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         // CHECKSTYLE_ON: LineLength
224 
225         warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
226 
227         final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
228         if ( webXmlFile.exists() )
229         {
230             warArchiver.setWebxml( webXmlFile );
231         }
232 
233         warArchiver.setRecompressAddedZips( isRecompressZippedFiles() );
234 
235         warArchiver.setIncludeEmptyDirs( isIncludeEmptyDirectories() );
236 
237         if ( !failOnMissingWebXml )
238         {
239             getLog().debug( "Build won't fail if web.xml file is missing." );
240             warArchiver.setExpectWebXml( false );
241         }
242 
243         // create archive
244         archiver.createArchive( getSession(), getProject(), getArchive() );
245 
246         // create the classes to be attached if necessary
247         if ( isAttachClasses() )
248         {
249             if ( isArchiveClasses() && getJarArchiver().getDestFile() != null )
250             {
251                 // special handling in case of archived classes: MWAR-240
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     /**
289      * @param basedir The basedir
290      * @param finalName The finalName
291      * @param classifier The classifier.
292      * @param type The type.
293      * @return {@link File}
294      */
295     protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
296     {
297         if ( classifier == null )
298         {
299             classifier = "";
300         }
301         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
302         {
303             classifier = "-" + classifier;
304         }
305 
306         return new File( basedir, finalName + classifier + "." + type );
307     }
308 
309     /**
310      * @return The war {@link File}
311      */
312     protected File getTargetWarFile()
313     {
314         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
315 
316     }
317 
318     /**
319      * @return The target class {@link File}
320      */
321     protected File getTargetClassesFile()
322     {
323         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
324     }
325 
326     // Getters and Setters
327 
328     /**
329      * @return {@link #classifier}
330      */
331     public String getClassifier()
332     {
333         return classifier;
334     }
335 
336     /**
337      * @param classifier {@link #classifier}
338      */
339     public void setClassifier( String classifier )
340     {
341         this.classifier = classifier;
342     }
343 
344     /**
345      * @return The package excludes.
346      */
347     public String[] getPackagingExcludes()
348     {
349         if ( StringUtils.isEmpty( packagingExcludes ) )
350         {
351             return new String[0];
352         }
353         else
354         {
355             return StringUtils.split( packagingExcludes, "," );
356         }
357     }
358 
359     /**
360      * @param packagingExcludes {@link #packagingExcludes}
361      */
362     public void setPackagingExcludes( String packagingExcludes )
363     {
364         this.packagingExcludes = packagingExcludes;
365     }
366 
367     /**
368      * @return The packaging includes.
369      */
370     public String[] getPackagingIncludes()
371     {
372         if ( StringUtils.isEmpty( packagingIncludes ) )
373         {
374             return new String[] { "**" };
375         }
376         else
377         {
378             return StringUtils.split( packagingIncludes, "," );
379         }
380     }
381 
382     /**
383      * @param packagingIncludes {@link #packagingIncludes}
384      */
385     public void setPackagingIncludes( String packagingIncludes )
386     {
387         this.packagingIncludes = packagingIncludes;
388     }
389 
390     /**
391      * @return {@link #outputDirectory}
392      */
393     public String getOutputDirectory()
394     {
395         return outputDirectory;
396     }
397 
398     /**
399      * @param outputDirectory {@link #outputDirectory}
400      */
401     public void setOutputDirectory( String outputDirectory )
402     {
403         this.outputDirectory = outputDirectory;
404     }
405 
406     /**
407      * @return {@link #warName}
408      */
409     public String getWarName()
410     {
411         return warName;
412     }
413 
414     /**
415      * @param warName {@link #warName}
416      */
417     public void setWarName( String warName )
418     {
419         this.warName = warName;
420     }
421 
422     /**
423      * @return {@link #warArchiver}
424      */
425     public WarArchiver getWarArchiver()
426     {
427         return warArchiver;
428     }
429 
430     /**
431      * @param warArchiver {@link #warArchiver}
432      */
433     public void setWarArchiver( WarArchiver warArchiver )
434     {
435         this.warArchiver = warArchiver;
436     }
437 
438     /**
439      * @return {@link #projectHelper}
440      */
441     public MavenProjectHelper getProjectHelper()
442     {
443         return projectHelper;
444     }
445 
446     /**
447      * @param projectHelper {@link #projectHelper}
448      */
449     public void setProjectHelper( MavenProjectHelper projectHelper )
450     {
451         this.projectHelper = projectHelper;
452     }
453 
454     /**
455      * @return {@link #primaryArtifact}
456      */
457     public boolean isPrimaryArtifact()
458     {
459         return primaryArtifact;
460     }
461 
462     /**
463      * @param primaryArtifact {@link #primaryArtifact}
464      */
465     public void setPrimaryArtifact( boolean primaryArtifact )
466     {
467         this.primaryArtifact = primaryArtifact;
468     }
469 
470     /**
471      * @return {@link #attachClasses}
472      */
473     public boolean isAttachClasses()
474     {
475         return attachClasses;
476     }
477 
478     /**
479      * @param attachClasses {@link #attachClasses}
480      */
481     public void setAttachClasses( boolean attachClasses )
482     {
483         this.attachClasses = attachClasses;
484     }
485 
486     /**
487      * @return {@link #classesClassifier}
488      */
489     public String getClassesClassifier()
490     {
491         return classesClassifier;
492     }
493 
494     /**
495      * @param classesClassifier {@link #classesClassifier}
496      */
497     public void setClassesClassifier( String classesClassifier )
498     {
499         this.classesClassifier = classesClassifier;
500     }
501 
502     /**
503      * @return {@link #failOnMissingWebXml}
504      */
505     public boolean isFailOnMissingWebXml()
506     {
507         return failOnMissingWebXml;
508     }
509 
510     /**
511      * @param failOnMissingWebXml {@link #failOnMissingWebXml}
512      */
513     public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
514     {
515         this.failOnMissingWebXml = failOnMissingWebXml;
516     }
517 }