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 868453 2013-07-05 11:25:41Z olamy $
50   */
51  @Mojo( name = "war", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true,
52         requiresDependencyResolution = ResolutionScope.RUNTIME )
53  public class WarMojo
54      extends AbstractWarMojo
55  {
56      /**
57       * The directory for the generated WAR.
58       */
59      @Parameter( defaultValue = "${project.build.directory}", required = true )
60      private String outputDirectory;
61  
62      /**
63       * The name of the generated WAR.
64       */
65      @Parameter( defaultValue = "${project.build.finalName}", property = "war.warName", required = true )
66      private String warName;
67  
68      /**
69       * Classifier to add to the generated WAR. If given, the artifact will be an attachment instead.
70       * The classifier will not be applied to the JAR file of the project - only to the WAR file.
71       */
72      @Parameter
73      private String classifier;
74  
75      /**
76       * The comma separated list of tokens to exclude from the WAR before
77       * packaging. This option may be used to implement the skinny WAR use
78       * case. Note that you can use the Java Regular Expressions engine to
79       * include and exclude specific pattern using the expression %regex[].
80       * Hint: read the about (?!Pattern).
81       *
82       * @since 2.1-alpha-2
83       */
84      @Parameter
85      private String packagingExcludes;
86  
87      /**
88       * The comma separated list of tokens to include in the WAR before
89       * packaging. By default everything is included. This option may be used
90       * to implement the skinny WAR use case. Note that you can use the
91       * Java Regular Expressions engine to include and exclude specific pattern
92       * using the expression %regex[].
93       *
94       * @since 2.1-beta-1
95       */
96      @Parameter
97      private String packagingIncludes;
98  
99      /**
100      * The WAR archiver.
101      */
102     @Component( role = Archiver.class, hint = "war" )
103     private WarArchiver warArchiver;
104 
105     /**
106      */
107     @Component
108     private MavenProjectHelper projectHelper;
109 
110     /**
111      * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or
112      * deploy it to the local repository instead of the default one in an execution.
113      */
114     @Parameter( property = "primaryArtifact", defaultValue = "true" )
115     private boolean primaryArtifact = true;
116 
117     /**
118      * Whether or not to fail the build if the <code>web.xml</code> file is missing. Set to <code>false</code>
119      * if you want you WAR built without a <code>web.xml</code> file.
120      * This may be useful if you are building an overlay that has no web.xml file.
121      *
122      * @since 2.1-alpha-2
123      */
124     @Parameter( property = "failOnMissingWebXml", defaultValue = "true" )
125     private boolean failOnMissingWebXml = true;
126 
127     /**
128      * Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the
129      * project as an additional artifact.
130      * <p>By default the
131      * classifier for the additional artifact is 'classes'.
132      * You can change it with the
133      * <code><![CDATA[<classesClassifier>someclassifier</classesClassifier>]]></code>
134      * parameter.
135      * </p><p>
136      * If this parameter true, another project can depend on the classes
137      * by writing something like:
138      * <pre><![CDATA[<dependency>
139      *   <groupId>myGroup</groupId>
140      *   <artifactId>myArtifact</artifactId>
141      *   <version>myVersion</myVersion>
142      *   <classifier>classes</classifier>
143      * </dependency>]]></pre></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     /**
164      * Executes the WarMojo on the current project.
165      *
166      * @throws MojoExecutionException if an error occurred while building the webapp
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
203      *                                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, ArchiverException, 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(
222             "Excluding " + Arrays.asList( getPackagingExcludes() ) + " from the generated webapp archive." );
223         getLog().debug(
224             "Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." );
225 
226         warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
227 
228         final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
229         if ( webXmlFile.exists() )
230         {
231             warArchiver.setWebxml( webXmlFile );
232         }
233 
234         warArchiver.setRecompressAddedZips( isRecompressZippedFiles()  );
235         
236         warArchiver.setIncludeEmptyDirs( isIncludeEmptyDirectories() );
237 
238         if ( !failOnMissingWebXml )
239         {
240             getLog().debug( "Build won't fail if web.xml file is missing." );
241             // The flag is wrong in plexus-archiver so it will need to be fixed at some point
242             warArchiver.setIgnoreWebxml( false );
243         }
244 
245         // create archive
246         archiver.createArchive( getSession(), getProject(), getArchive() );
247 
248         // create the classes to be attached if necessary
249         if ( isAttachClasses() )
250         {
251             if ( isArchiveClasses() && getJarArchiver().getDestFile() != null )
252             {
253                 // special handling in case of archived classes: MWAR-240
254                 File targetClassesFile = getTargetClassesFile();
255                 FileUtils.copyFile(getJarArchiver().getDestFile(), targetClassesFile);
256                 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), targetClassesFile );
257             }
258             else
259             {
260                 ClassesPackager packager = new ClassesPackager();
261                 final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
262                 if ( classesDirectory.exists() )
263                 {
264                     getLog().info( "Packaging classes" );
265                     packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getSession(),
266                                              getProject(), getArchive() );
267                     projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
268                 }
269             }
270         }
271 
272         String classifier = this.classifier;
273         if ( classifier != null )
274         {
275             projectHelper.attachArtifact( getProject(), "war", classifier, warFile );
276         }
277         else
278         {
279             Artifact artifact = getProject().getArtifact();
280             if ( primaryArtifact )
281             {
282                 artifact.setFile( warFile );
283             }
284             else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
285             {
286                 artifact.setFile( warFile );
287             }
288         }
289     }
290 
291 
292     protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
293     {
294         if ( classifier == null )
295         {
296             classifier = "";
297         }
298         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
299         {
300             classifier = "-" + classifier;
301         }
302 
303         return new File( basedir, finalName + classifier + "." + type );
304     }
305 
306 
307     protected File getTargetWarFile()
308     {
309         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
310 
311     }
312 
313     protected File getTargetClassesFile()
314     {
315         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
316     }
317 
318     // Getters and Setters
319 
320     public String getClassifier()
321     {
322         return classifier;
323     }
324 
325     public void setClassifier( String classifier )
326     {
327         this.classifier = classifier;
328     }
329 
330     public String[] getPackagingExcludes()
331     {
332         if ( StringUtils.isEmpty( packagingExcludes ) )
333         {
334             return new String[0];
335         }
336         else
337         {
338             return StringUtils.split( packagingExcludes, "," );
339         }
340     }
341 
342     public void setPackagingExcludes( String packagingExcludes )
343     {
344         this.packagingExcludes = packagingExcludes;
345     }
346 
347     public String[] getPackagingIncludes()
348     {
349         if ( StringUtils.isEmpty( packagingIncludes ) )
350         {
351             return new String[]{"**"};
352         }
353         else
354         {
355             return StringUtils.split( packagingIncludes, "," );
356         }
357     }
358 
359     public void setPackagingIncludes( String packagingIncludes )
360     {
361         this.packagingIncludes = packagingIncludes;
362     }
363 
364     public String getOutputDirectory()
365     {
366         return outputDirectory;
367     }
368 
369     public void setOutputDirectory( String outputDirectory )
370     {
371         this.outputDirectory = outputDirectory;
372     }
373 
374     public String getWarName()
375     {
376         return warName;
377     }
378 
379     public void setWarName( String warName )
380     {
381         this.warName = warName;
382     }
383 
384     public WarArchiver getWarArchiver()
385     {
386         return warArchiver;
387     }
388 
389     public void setWarArchiver( WarArchiver warArchiver )
390     {
391         this.warArchiver = warArchiver;
392     }
393 
394     public MavenProjectHelper getProjectHelper()
395     {
396         return projectHelper;
397     }
398 
399     public void setProjectHelper( MavenProjectHelper projectHelper )
400     {
401         this.projectHelper = projectHelper;
402     }
403 
404     public boolean isPrimaryArtifact()
405     {
406         return primaryArtifact;
407     }
408 
409     public void setPrimaryArtifact( boolean primaryArtifact )
410     {
411         this.primaryArtifact = primaryArtifact;
412     }
413 
414     public boolean isAttachClasses()
415     {
416         return attachClasses;
417     }
418 
419     public void setAttachClasses( boolean attachClasses )
420     {
421         this.attachClasses = attachClasses;
422     }
423 
424     public String getClassesClassifier()
425     {
426         return classesClassifier;
427     }
428 
429     public void setClassesClassifier( String classesClassifier )
430     {
431         this.classesClassifier = classesClassifier;
432     }
433 
434     public boolean isFailOnMissingWebXml()
435     {
436         return failOnMissingWebXml;
437     }
438 
439     public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
440     {
441         this.failOnMissingWebXml = failOnMissingWebXml;
442     }
443 }