Coverage Report - org.apache.maven.plugin.war.packaging.OverlayPackagingTask
 
Classes in this File Line Coverage Branch Coverage Complexity
OverlayPackagingTask
79%
30/38
60%
12/20
4,25
 
 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.plugin.MojoExecutionException;
 23  
 import org.apache.maven.plugin.war.Overlay;
 24  
 import org.apache.maven.plugin.war.util.PathSet;
 25  
 import org.codehaus.plexus.util.FileUtils;
 26  
 
 27  
 import java.io.File;
 28  
 import java.io.IOException;
 29  
 
 30  
 /**
 31  
  * Handles an overlay.
 32  
  *
 33  
  * @author Stephane Nicoll
 34  
  * 
 35  
  * @version $Id: OverlayPackagingTask.java 985593 2010-08-14 22:12:09Z dennisl $
 36  
  */
 37  
 public class OverlayPackagingTask
 38  
     extends AbstractWarPackagingTask
 39  
 {
 40  
     private final Overlay overlay;
 41  
 
 42  
 
 43  
     public OverlayPackagingTask( Overlay overlay, Overlay currentProjectOverlay )
 44  34
     {
 45  34
         if ( overlay == null )
 46  
         {
 47  0
             throw new NullPointerException( "overlay could not be null." );
 48  
         }
 49  34
         if ( overlay.equals( currentProjectOverlay ) )
 50  
         {
 51  0
             throw new IllegalStateException( "Could not handle the current project with this task." );
 52  
         }
 53  34
         this.overlay = overlay;
 54  34
     }
 55  
 
 56  
 
 57  
     public void performPackaging( WarPackagingContext context )
 58  
         throws MojoExecutionException
 59  
     {
 60  34
         context.getLog().debug(
 61  
             "OverlayPackagingTask performPackaging overlay.getTargetPath() " + overlay.getTargetPath() );
 62  34
         if ( overlay.shouldSkip() )
 63  
         {
 64  1
             context.getLog().info( "Skipping overlay [" + overlay + "]" );
 65  
         }
 66  
         else
 67  
         {
 68  
             try
 69  
             {
 70  33
                 context.getLog().info( "Processing overlay [" + overlay + "]" );
 71  
 
 72  
                 // Step1: Extract if necessary
 73  33
                 final File tmpDir = unpackOverlay( context, overlay );
 74  
 
 75  
                 // Step2: setup
 76  33
                 final PathSet includes = getFilesToIncludes( tmpDir, overlay.getIncludes(), overlay.getExcludes() );
 77  
 
 78  
                 // Copy
 79  33
                 if ( null == overlay.getTargetPath() )
 80  
                 {
 81  32
                     copyFiles( overlay.getId(), context, tmpDir, includes, overlay.isFiltered() );
 82  
                 }
 83  
                 else
 84  
                 {
 85  
                     // overlay.getTargetPath() must ended with /
 86  
                     // if not we add it
 87  1
                     String targetPath = overlay.getTargetPath();
 88  1
                     if ( !targetPath.endsWith( "/" ) )
 89  
                     {
 90  1
                         targetPath = targetPath + "/";
 91  
                     }
 92  1
                     copyFiles( overlay.getId(), context, tmpDir, includes, targetPath, overlay.isFiltered() );
 93  
                 }
 94  
             }
 95  0
             catch ( IOException e )
 96  
             {
 97  0
                 throw new MojoExecutionException( "Failed to copy file for overlay [" + overlay + "]", e );
 98  33
             }
 99  
         }
 100  34
     }
 101  
 
 102  
     /**
 103  
      * Unpacks the specified overlay.
 104  
      * <p/>
 105  
      * Makes sure to skip the unpack process if the overlay has
 106  
      * already been unpacked.
 107  
      *
 108  
      * @param context the packaging context
 109  
      * @param overlay the overlay
 110  
      * @return the directory containing the unpacked overlay
 111  
      * @throws MojoExecutionException if an error occurred while unpacking the overlay
 112  
      */
 113  
     protected File unpackOverlay( WarPackagingContext context, Overlay overlay )
 114  
         throws MojoExecutionException
 115  
     {
 116  33
         final File tmpDir = getOverlayTempDirectory( context, overlay );
 117  
 
 118  
         // TODO: not sure it's good, we should reuse the markers of the dependency plugin
 119  33
         if ( FileUtils.sizeOfDirectory( tmpDir ) == 0
 120  
             || overlay.getArtifact().getFile().lastModified() > tmpDir.lastModified() )
 121  
         {
 122  0
             doUnpack( context, overlay.getArtifact().getFile(), tmpDir );
 123  
         }
 124  
         else
 125  
         {
 126  33
             context.getLog().debug( "Overlay [" + overlay + "] was already unpacked" );
 127  
         }
 128  33
         return tmpDir;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Returns the directory to use to unpack the specified overlay.
 133  
      *
 134  
      * @param context the packaging context
 135  
      * @param overlay the overlay
 136  
      * @return the temp directory for the overlay
 137  
      */
 138  
     protected File getOverlayTempDirectory( WarPackagingContext context, Overlay overlay )
 139  
     {
 140  33
         final File groupIdDir = new File( context.getOverlaysWorkDirectory(), overlay.getGroupId() );
 141  33
         if ( !groupIdDir.exists() )
 142  
         {
 143  0
             groupIdDir.mkdir();
 144  
         }
 145  33
         String directoryName = overlay.getArtifactId();
 146  33
         if ( overlay.getClassifier() != null )
 147  
         {
 148  0
             directoryName = directoryName + "-" + overlay.getClassifier();
 149  
         }
 150  33
         final File result = new File( groupIdDir, directoryName );
 151  33
         if ( !result.exists() )
 152  
         {
 153  0
             result.mkdirs();
 154  
         }
 155  33
         return result;
 156  
     }
 157  
 }