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 925069 2014-10-08 17:03:57Z khmarbaise $
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       * The directory for the generated WAR.
61       */
62      @Parameter( defaultValue = "${project.build.directory}", required = true )
63      private String outputDirectory;
64  
65      /**
66       * The name of the generated WAR.
67       */
68      @Parameter( defaultValue = "${project.build.finalName}", property = "war.warName", required = true )
69      private String warName;
70  
71      /**
72       * Classifier to add to the generated WAR. If given, the artifact will be an attachment instead. The classifier will
73       * not be applied to the JAR file of the project - only to the WAR file.
74       */
75      @Parameter
76      private String classifier;
77  
78      /**
79       * The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement
80       * the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude
81       * specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
82       *
83       * @since 2.1-alpha-2
84       */
85      @Parameter
86      private String packagingExcludes;
87  
88      /**
89       * The comma separated list of tokens to include in the WAR before packaging. By default everything is included.
90       * This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions
91       * engine to include and exclude specific pattern using the expression %regex[].
92       *
93       * @since 2.1-beta-1
94       */
95      @Parameter
96      private String packagingIncludes;
97  
98      /**
99       * The WAR archiver.
100      */
101     @Component( role = Archiver.class, hint = "war" )
102     private WarArchiver warArchiver;
103 
104     /**
105      */
106     @Component
107     private MavenProjectHelper projectHelper;
108 
109     /**
110      * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or deploy
111      * it to the local repository instead of the default one in an execution.
112      */
113     @Parameter( property = "primaryArtifact", defaultValue = "true" )
114     private boolean primaryArtifact = true;
115 
116     /**
117      * Whether or not to fail the build if the <code>web.xml</code> file is missing. Set to <code>false</code> if you
118      * want you WAR built without a <code>web.xml</code> file. This may be useful if you are building an overlay that
119      * has no web.xml file.
120      *
121      * @since 2.1-alpha-2
122      */
123     @Parameter( property = "failOnMissingWebXml", defaultValue = "true" )
124     private boolean failOnMissingWebXml = true;
125 
126     /**
127      * Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the project as an
128      * additional artifact.
129      * <p>
130      * By default the classifier for the additional artifact is 'classes'. You can change it with the
131      * <code><![CDATA[<classesClassifier>someclassifier</classesClassifier>]]></code> parameter.
132      * </p>
133      * <p>
134      * If this parameter true, another project can depend on the classes by writing something like:
135      * 
136      * <pre>
137      * <![CDATA[<dependency>
138      *   <groupId>myGroup</groupId>
139      *   <artifactId>myArtifact</artifactId>
140      *   <version>myVersion</myVersion>
141      *   <classifier>classes</classifier>
142      * </dependency>]]>
143      * </pre>
144      * 
145      * </p>
146      *
147      * @since 2.1-alpha-2
148      */
149     @Parameter( defaultValue = "false" )
150     private boolean attachClasses = false;
151 
152     /**
153      * The classifier to use for the attached classes artifact.
154      *
155      * @since 2.1-alpha-2
156      */
157     @Parameter( defaultValue = "classes" )
158     private String classesClassifier = "classes";
159 
160     // ----------------------------------------------------------------------
161     // Implementation
162     // ----------------------------------------------------------------------
163 
164     /**
165      * Executes the WarMojo on the current project.
166      *
167      * @throws MojoExecutionException if an error occurred while building the webapp
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      * Generates the webapp according to the <tt>mode</tt> attribute.
198      *
199      * @param warFile the target WAR file
200      * @throws IOException if an error occurred while copying files
201      * @throws ArchiverException if the archive could not be created
202      * @throws ManifestException if the manifest could not be created
203      * @throws DependencyResolutionRequiredException if an error occurred while resolving the dependencies
204      * @throws MojoExecutionException if the execution failed
205      * @throws MojoFailureException if a fatal exception occurred
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             // The flag is wrong in plexus-archiver so it will need to be fixed at some point
240             warArchiver.setIgnoreWebxml( 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     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     // Getters and Setters
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 }