Coverage Report - org.apache.maven.plugin.war.packaging.ClassesPackagingTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassesPackagingTask
38%
12/32
50%
5/10
3,667
 
 1  
 package org.apache.maven.plugin.war.packaging;
 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.artifact.Artifact;
 23  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.war.Overlay;
 26  
 import org.apache.maven.plugin.war.util.ClassesPackager;
 27  
 import org.apache.maven.plugin.war.util.PathSet;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 import org.codehaus.plexus.interpolation.InterpolationException;
 30  
 
 31  
 import java.io.File;
 32  
 import java.io.IOException;
 33  
 
 34  
 /**
 35  
  * Handles the classes directory that needs to be packaged in the web application.
 36  
  * <p/>
 37  
  * Based on the {@link WarPackagingContext#archiveClasses()} flag the resources
 38  
  * either copied into to <tt>WEB-INF/classes</tt> directory or archived in a jar
 39  
  * within the <tt>WEB-INF/lib</tt> directory.
 40  
  *
 41  
  * @author Stephane Nicoll
 42  
  *
 43  
  * @version $Id: ClassesPackagingTask.java 985593 2010-08-14 22:12:09Z dennisl $
 44  
  */
 45  
 public class ClassesPackagingTask
 46  
     extends AbstractWarPackagingTask
 47  
 {
 48  
     private final Overlay currentProjectOverlay;
 49  
 
 50  
     public ClassesPackagingTask( Overlay currentProjectOverlay )
 51  60
     {
 52  60
         this.currentProjectOverlay = currentProjectOverlay;
 53  60
     }
 54  
 
 55  
     public void performPackaging( WarPackagingContext context )
 56  
         throws MojoExecutionException
 57  
     {
 58  60
         final File webappClassesDirectory = new File( context.getWebappDirectory(), CLASSES_PATH );
 59  60
         if ( !webappClassesDirectory.exists() )
 60  
         {
 61  12
             webappClassesDirectory.mkdirs();
 62  
         }
 63  
 
 64  60
         if ( context.getClassesDirectory().exists() && !context.getClassesDirectory().equals( webappClassesDirectory ) )
 65  
         {
 66  60
             if ( context.archiveClasses() )
 67  
             {
 68  0
                 generateJarArchive( context );
 69  
             }
 70  
             else
 71  
             {
 72  60
                 final PathSet sources = getFilesToIncludes( context.getClassesDirectory(), null, null );
 73  
                 try
 74  
                 {
 75  60
                     copyFiles( currentProjectOverlay.getId(), context, context.getClassesDirectory(),
 76  
                                sources, CLASSES_PATH, false );
 77  
                 }
 78  0
                 catch ( IOException e )
 79  
                 {
 80  0
                     throw new MojoExecutionException(
 81  
                         "Could not copy webapp classes [" + context.getClassesDirectory().getAbsolutePath() + "]", e );
 82  60
                 }
 83  
             }
 84  
         }
 85  60
     }
 86  
 
 87  
     protected void generateJarArchive( WarPackagingContext context )
 88  
         throws MojoExecutionException
 89  
     {
 90  0
         MavenProject project = context.getProject();
 91  0
         ArtifactFactory factory = context.getArtifactFactory();
 92  0
         Artifact artifact = factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(),
 93  
                                                          project.getVersion(), "jar" );
 94  0
         String archiveName = null;
 95  
         try
 96  
         {
 97  0
             archiveName = getArtifactFinalName( context, artifact );
 98  
         }
 99  0
         catch ( InterpolationException e )
 100  
         {
 101  0
             throw new MojoExecutionException(
 102  
                 "Could not get the final name of the artifact [" + artifact.getGroupId() + ":" + artifact.getArtifactId()
 103  
                     + ":" + artifact.getVersion() + "]", e );
 104  0
         }
 105  0
         final String targetFilename = LIB_PATH + archiveName;
 106  
 
 107  0
         if ( context.getWebappStructure().registerFile( currentProjectOverlay.getId(), targetFilename ) )
 108  
         {
 109  0
             final File libDirectory = new File( context.getWebappDirectory(), LIB_PATH );
 110  0
             final File jarFile = new File( libDirectory, archiveName );
 111  0
             final ClassesPackager packager = new ClassesPackager();
 112  0
             packager.packageClasses( context.getClassesDirectory(), jarFile, context.getJarArchiver(),
 113  
                                      project, context.getArchive() );
 114  0
         }
 115  
         else
 116  
         {
 117  0
             context.getLog().warn(
 118  
                 "Could not generate archive classes file [" + targetFilename + "] has already been copied." );
 119  
         }
 120  0
     }
 121  
 }