Coverage Report - org.apache.maven.plugin.war.WarMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
WarMojo
71%
65/92
73%
19/26
1,778
 
 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  32
 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  32
     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  32
     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  32
     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  32
     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  28
         File warFile = getTargetWarFile();
 155  
 
 156  
         try
 157  
         {
 158  28
             performPackaging( warFile );
 159  
         }
 160  0
         catch ( DependencyResolutionRequiredException e )
 161  
         {
 162  0
             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
 163  
         }
 164  0
         catch ( ManifestException e )
 165  
         {
 166  0
             throw new MojoExecutionException( "Error assembling WAR", e );
 167  
         }
 168  0
         catch ( IOException e )
 169  
         {
 170  0
             throw new MojoExecutionException( "Error assembling WAR", e );
 171  
         }
 172  2
         catch ( ArchiverException e )
 173  
         {
 174  2
             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
 175  26
         }
 176  26
     }
 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  28
         getLog().info( "Packaging webapp" );
 195  
 
 196  28
         buildExplodedWebapp( getWebappDirectory() );
 197  
 
 198  28
         MavenArchiver archiver = new MavenArchiver();
 199  
 
 200  28
         archiver.setArchiver( warArchiver );
 201  
 
 202  28
         archiver.setOutputFile( warFile );
 203  
 
 204  28
         getLog().debug(
 205  
             "Excluding " + Arrays.asList( getPackagingExcludes() ) + " from the generated webapp archive." );
 206  28
         getLog().debug(
 207  
             "Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." );
 208  
 
 209  28
         warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
 210  
 
 211  28
         final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
 212  28
         if ( webXmlFile.exists() )
 213  
         {
 214  24
             warArchiver.setWebxml( webXmlFile );
 215  
         }
 216  28
         if ( !failOnMissingWebXml )
 217  
         {
 218  2
             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  2
             warArchiver.setIgnoreWebxml( false );
 221  
         }
 222  
 
 223  
         // create archive
 224  28
         archiver.createArchive( getProject(), getArchive() );
 225  
 
 226  
         // create the classes to be attached if necessary
 227  26
         if ( isAttachClasses() )
 228  
         {
 229  4
             ClassesPackager packager = new ClassesPackager();
 230  4
             final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
 231  4
             if ( classesDirectory.exists() )
 232  
             {
 233  4
                 getLog().info( "Packaging classes" );
 234  4
                 packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getProject(),
 235  
                                          getArchive() );
 236  4
                 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
 237  
             }
 238  
         }
 239  
 
 240  26
         String classifier = this.classifier;
 241  26
         if ( classifier != null )
 242  
         {
 243  2
             projectHelper.attachArtifact( getProject(), "war", classifier, warFile );
 244  
         }
 245  
         else
 246  
         {
 247  24
             Artifact artifact = getProject().getArtifact();
 248  24
             if ( primaryArtifact )
 249  
             {
 250  22
                 artifact.setFile( warFile );
 251  
             }
 252  2
             else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
 253  
             {
 254  0
                 artifact.setFile( warFile );
 255  
             }
 256  
         }
 257  26
     }
 258  
 
 259  
 
 260  
     protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
 261  
     {
 262  36
         if ( classifier == null )
 263  
         {
 264  26
             classifier = "";
 265  
         }
 266  10
         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
 267  
         {
 268  10
             classifier = "-" + classifier;
 269  
         }
 270  
 
 271  36
         return new File( basedir, finalName + classifier + "." + type );
 272  
     }
 273  
 
 274  
 
 275  
     protected File getTargetWarFile()
 276  
     {
 277  28
         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
 278  
 
 279  
     }
 280  
 
 281  
     protected File getTargetClassesFile()
 282  
     {
 283  8
         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
 284  
     }
 285  
 
 286  
     // Getters and Setters
 287  
 
 288  
     public String getClassifier()
 289  
     {
 290  28
         return classifier;
 291  
     }
 292  
 
 293  
     public void setClassifier( String classifier )
 294  
     {
 295  0
         this.classifier = classifier;
 296  0
     }
 297  
 
 298  
     public String[] getPackagingExcludes()
 299  
     {
 300  56
         if ( StringUtils.isEmpty( packagingExcludes ) )
 301  
         {
 302  56
             return new String[0];
 303  
         }
 304  
         else
 305  
         {
 306  0
             return StringUtils.split( packagingExcludes, "," );
 307  
         }
 308  
     }
 309  
 
 310  
     public void setPackagingExcludes( String packagingExcludes )
 311  
     {
 312  0
         this.packagingExcludes = packagingExcludes;
 313  0
     }
 314  
 
 315  
     public String[] getPackagingIncludes()
 316  
     {
 317  56
         if ( StringUtils.isEmpty( packagingIncludes ) )
 318  
         {
 319  56
             return new String[]{"**"};
 320  
         }
 321  
         else
 322  
         {
 323  0
             return StringUtils.split( packagingIncludes, "," );
 324  
         }
 325  
     }
 326  
 
 327  
     public void setPackagingIncludes( String packagingIncludes )
 328  
     {
 329  0
         this.packagingIncludes = packagingIncludes;
 330  0
     }
 331  
 
 332  
     public String getOutputDirectory()
 333  
     {
 334  36
         return outputDirectory;
 335  
     }
 336  
 
 337  
     public void setOutputDirectory( String outputDirectory )
 338  
     {
 339  0
         this.outputDirectory = outputDirectory;
 340  0
     }
 341  
 
 342  
     public String getWarName()
 343  
     {
 344  36
         return warName;
 345  
     }
 346  
 
 347  
     public void setWarName( String warName )
 348  
     {
 349  0
         this.warName = warName;
 350  0
     }
 351  
 
 352  
     public WarArchiver getWarArchiver()
 353  
     {
 354  0
         return warArchiver;
 355  
     }
 356  
 
 357  
     public void setWarArchiver( WarArchiver warArchiver )
 358  
     {
 359  0
         this.warArchiver = warArchiver;
 360  0
     }
 361  
 
 362  
     public MavenProjectHelper getProjectHelper()
 363  
     {
 364  0
         return projectHelper;
 365  
     }
 366  
 
 367  
     public void setProjectHelper( MavenProjectHelper projectHelper )
 368  
     {
 369  0
         this.projectHelper = projectHelper;
 370  0
     }
 371  
 
 372  
     public boolean isPrimaryArtifact()
 373  
     {
 374  0
         return primaryArtifact;
 375  
     }
 376  
 
 377  
     public void setPrimaryArtifact( boolean primaryArtifact )
 378  
     {
 379  24
         this.primaryArtifact = primaryArtifact;
 380  24
     }
 381  
 
 382  
     public boolean isAttachClasses()
 383  
     {
 384  26
         return attachClasses;
 385  
     }
 386  
 
 387  
     public void setAttachClasses( boolean attachClasses )
 388  
     {
 389  4
         this.attachClasses = attachClasses;
 390  4
     }
 391  
 
 392  
     public String getClassesClassifier()
 393  
     {
 394  12
         return classesClassifier;
 395  
     }
 396  
 
 397  
     public void setClassesClassifier( String classesClassifier )
 398  
     {
 399  2
         this.classesClassifier = classesClassifier;
 400  2
     }
 401  
 
 402  
     public boolean isFailOnMissingWebXml()
 403  
     {
 404  0
         return failOnMissingWebXml;
 405  
     }
 406  
 
 407  
     public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
 408  
     {
 409  4
         this.failOnMissingWebXml = failOnMissingWebXml;
 410  4
     }
 411  
 }