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.project.MavenProjectHelper;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.ManifestException;
31  import org.codehaus.plexus.archiver.war.WarArchiver;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.Arrays;
37  
38  /**
39   * Build a WAR file.
40   *
41   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
42   * @version $Id: WarMojo.java 985625 2010-08-15 08:15:16Z dennisl $
43   * @goal war
44   * @phase package
45   * @threadSafe
46   * @requiresDependencyResolution runtime
47   */
48  public class WarMojo
49      extends AbstractWarMojo
50  {
51      /**
52       * The directory for the generated WAR.
53       *
54       * @parameter default-value="${project.build.directory}"
55       * @required
56       */
57      private String outputDirectory;
58  
59      /**
60       * The name of the generated WAR.
61       *
62       * @parameter default-value="${project.build.finalName}"
63       * @required
64       */
65      private String warName;
66  
67      /**
68       * Classifier to add to the generated WAR. If given, the artifact will be an attachment instead.
69       * The classifier will not be applied to the JAR file of the project - only to the WAR file.
70       *
71       * @parameter
72       */
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.
79       *
80       * @parameter
81       * @since 2.1-alpha-2
82       */
83      private String packagingExcludes;
84  
85      /**
86       * The comma separated list of tokens to include in the WAR before
87       * packaging. By default everything is included. This option may be used
88       * to implement the skinny WAR use case.
89       *
90       * @since 2.1-beta-1
91       */
92      private String packagingIncludes;
93  
94      /**
95       * The WAR archiver.
96       *
97       * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="war"
98       */
99      private WarArchiver warArchiver;
100 
101     /**
102      * @component
103      */
104     private MavenProjectHelper projectHelper;
105 
106     /**
107      * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or
108      * deploy it to the local repository instead of the default one in an execution.
109      *
110      * @parameter expression="${primaryArtifact}" default-value="true"
111      */
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>
116      * if you want you WAR built without a <code>web.xml</code> file.
117      * This may be useful if you are building an overlay that has no web.xml file.
118      *
119      * @parameter expression="${failOnMissingWebXml}" default-value="true"
120      * @since 2.1-alpha-2
121      */
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
126      * project.
127      *
128      * @parameter default-value="false"
129      * @since 2.1-alpha-2
130      */
131     private boolean attachClasses = false;
132 
133     /**
134      * The classifier to use for the attached classes artifact.
135      *
136      * @parameter default-value="classes"
137      * @since 2.1-alpha-2
138      */
139     private String classesClassifier = "classes";
140 
141     // ----------------------------------------------------------------------
142     // Implementation
143     // ----------------------------------------------------------------------
144 
145 
146     /**
147      * Executes the WarMojo on the current project.
148      *
149      * @throws MojoExecutionException if an error occurred while building the webapp
150      */
151     public void execute()
152         throws MojoExecutionException, MojoFailureException
153     {
154         File warFile = getTargetWarFile();
155 
156         try
157         {
158             performPackaging( warFile );
159         }
160         catch ( DependencyResolutionRequiredException e )
161         {
162             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
163         }
164         catch ( ManifestException e )
165         {
166             throw new MojoExecutionException( "Error assembling WAR", e );
167         }
168         catch ( IOException e )
169         {
170             throw new MojoExecutionException( "Error assembling WAR", e );
171         }
172         catch ( ArchiverException e )
173         {
174             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
175         }
176     }
177 
178     /**
179      * Generates the webapp according to the <tt>mode</tt> attribute.
180      *
181      * @param warFile the target WAR file
182      * @throws IOException            if an error occurred while copying files
183      * @throws ArchiverException      if the archive could not be created
184      * @throws ManifestException      if the manifest could not be created
185      * @throws DependencyResolutionRequiredException
186      *                                if an error occurred while resolving the dependencies
187      * @throws MojoExecutionException if the execution failed
188      * @throws MojoFailureException   if a fatal exception occurred
189      */
190     private void performPackaging( File warFile )
191         throws IOException, ArchiverException, ManifestException, DependencyResolutionRequiredException,
192         MojoExecutionException, MojoFailureException
193     {
194         getLog().info( "Packaging webapp" );
195 
196         buildExplodedWebapp( getWebappDirectory() );
197 
198         MavenArchiver archiver = new MavenArchiver();
199 
200         archiver.setArchiver( warArchiver );
201 
202         archiver.setOutputFile( warFile );
203 
204         getLog().debug(
205             "Excluding " + Arrays.asList( getPackagingExcludes() ) + " from the generated webapp archive." );
206         getLog().debug(
207             "Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." );
208 
209         warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
210 
211         final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
212         if ( webXmlFile.exists() )
213         {
214             warArchiver.setWebxml( webXmlFile );
215         }
216         if ( !failOnMissingWebXml )
217         {
218             getLog().debug( "Build won't fail if web.xml file is missing." );
219             // The flag is wrong in plexus-archiver so it will need to be fixed at some point
220             warArchiver.setIgnoreWebxml( false );
221         }
222 
223         // create archive
224         archiver.createArchive( getProject(), getArchive() );
225 
226         // create the classes to be attached if necessary
227         if ( isAttachClasses() )
228         {
229             ClassesPackager packager = new ClassesPackager();
230             final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
231             if ( classesDirectory.exists() )
232             {
233                 getLog().info( "Packaging classes" );
234                 packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getProject(),
235                                          getArchive() );
236                 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
237             }
238         }
239 
240         String classifier = this.classifier;
241         if ( classifier != null )
242         {
243             projectHelper.attachArtifact( getProject(), "war", classifier, warFile );
244         }
245         else
246         {
247             Artifact artifact = getProject().getArtifact();
248             if ( primaryArtifact )
249             {
250                 artifact.setFile( warFile );
251             }
252             else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
253             {
254                 artifact.setFile( warFile );
255             }
256         }
257     }
258 
259 
260     protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
261     {
262         if ( classifier == null )
263         {
264             classifier = "";
265         }
266         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
267         {
268             classifier = "-" + classifier;
269         }
270 
271         return new File( basedir, finalName + classifier + "." + type );
272     }
273 
274 
275     protected File getTargetWarFile()
276     {
277         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
278 
279     }
280 
281     protected File getTargetClassesFile()
282     {
283         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
284     }
285 
286     // Getters and Setters
287 
288     public String getClassifier()
289     {
290         return classifier;
291     }
292 
293     public void setClassifier( String classifier )
294     {
295         this.classifier = classifier;
296     }
297 
298     public String[] getPackagingExcludes()
299     {
300         if ( StringUtils.isEmpty( packagingExcludes ) )
301         {
302             return new String[0];
303         }
304         else
305         {
306             return StringUtils.split( packagingExcludes, "," );
307         }
308     }
309 
310     public void setPackagingExcludes( String packagingExcludes )
311     {
312         this.packagingExcludes = packagingExcludes;
313     }
314 
315     public String[] getPackagingIncludes()
316     {
317         if ( StringUtils.isEmpty( packagingIncludes ) )
318         {
319             return new String[]{"**"};
320         }
321         else
322         {
323             return StringUtils.split( packagingIncludes, "," );
324         }
325     }
326 
327     public void setPackagingIncludes( String packagingIncludes )
328     {
329         this.packagingIncludes = packagingIncludes;
330     }
331 
332     public String getOutputDirectory()
333     {
334         return outputDirectory;
335     }
336 
337     public void setOutputDirectory( String outputDirectory )
338     {
339         this.outputDirectory = outputDirectory;
340     }
341 
342     public String getWarName()
343     {
344         return warName;
345     }
346 
347     public void setWarName( String warName )
348     {
349         this.warName = warName;
350     }
351 
352     public WarArchiver getWarArchiver()
353     {
354         return warArchiver;
355     }
356 
357     public void setWarArchiver( WarArchiver warArchiver )
358     {
359         this.warArchiver = warArchiver;
360     }
361 
362     public MavenProjectHelper getProjectHelper()
363     {
364         return projectHelper;
365     }
366 
367     public void setProjectHelper( MavenProjectHelper projectHelper )
368     {
369         this.projectHelper = projectHelper;
370     }
371 
372     public boolean isPrimaryArtifact()
373     {
374         return primaryArtifact;
375     }
376 
377     public void setPrimaryArtifact( boolean primaryArtifact )
378     {
379         this.primaryArtifact = primaryArtifact;
380     }
381 
382     public boolean isAttachClasses()
383     {
384         return attachClasses;
385     }
386 
387     public void setAttachClasses( boolean attachClasses )
388     {
389         this.attachClasses = attachClasses;
390     }
391 
392     public String getClassesClassifier()
393     {
394         return classesClassifier;
395     }
396 
397     public void setClassesClassifier( String classesClassifier )
398     {
399         this.classesClassifier = classesClassifier;
400     }
401 
402     public boolean isFailOnMissingWebXml()
403     {
404         return failOnMissingWebXml;
405     }
406 
407     public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
408     {
409         this.failOnMissingWebXml = failOnMissingWebXml;
410     }
411 }