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