Coverage Report - org.apache.maven.plugin.war.packaging.ArtifactsPackagingTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactsPackagingTask
90%
48/53
91%
31/34
8
 
 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.resolver.filter.ScopeArtifactFilter;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.war.Overlay;
 26  
 import org.codehaus.plexus.interpolation.InterpolationException;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 import java.util.Set;
 33  
 
 34  
 /**
 35  
  * Handles the artifacts that needs to be packaged in the web application.
 36  
  *
 37  
  * @author Stephane Nicoll
 38  
  * 
 39  
  * @version $Id: ArtifactsPackagingTask.java 1367293 2012-07-30 20:54:56Z olamy $
 40  
  */
 41  
 public class ArtifactsPackagingTask
 42  
     extends AbstractWarPackagingTask
 43  
 {
 44  
 
 45  
     public static final String TLD_PATH = "WEB-INF/tld/";
 46  
 
 47  
     public static final String SERVICES_PATH = "WEB-INF/services/";
 48  
 
 49  
     public static final String MODULES_PATH = "WEB-INF/modules/";
 50  
 
 51  
     public static final String EXTENSIONS_PATH = "WEB-INF/extensions/";
 52  
 
 53  
     private final Set artifacts;
 54  
 
 55  
     private final String id;
 56  
 
 57  
 
 58  
     public ArtifactsPackagingTask( Set artifacts, Overlay currentProjectOverlay )
 59  124
     {
 60  124
         this.artifacts = artifacts;
 61  124
         this.id = currentProjectOverlay.getId();
 62  124
     }
 63  
 
 64  
 
 65  
     public void performPackaging( WarPackagingContext context )
 66  
         throws MojoExecutionException
 67  
     {
 68  
         try
 69  
         {
 70  124
         final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
 71  124
         final List duplicates = findDuplicates( context, artifacts );
 72  
 
 73  124
         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
 74  
         {
 75  110
             Artifact artifact = (Artifact) iter.next();
 76  110
             String targetFileName = getArtifactFinalName( context, artifact );
 77  
 
 78  110
             context.getLog().debug( "Processing: " + targetFileName );
 79  
 
 80  110
             if ( duplicates.contains( targetFileName ) )
 81  
             {
 82  8
                 context.getLog().debug( "Duplicate found: " + targetFileName );
 83  8
                 targetFileName = artifact.getGroupId() + "-" + targetFileName;
 84  8
                 context.getLog().debug( "Renamed to: " + targetFileName );
 85  
             }
 86  110
             context.getWebappStructure().registerTargetFileName( artifact, targetFileName );
 87  
 
 88  110
             if ( !artifact.isOptional() && filter.include( artifact ) )
 89  
             {
 90  
                 try
 91  
                 {
 92  108
                     String type = artifact.getType();
 93  108
                     if ( "tld".equals( type ) )
 94  
                     {
 95  2
                         copyFile( id, context, artifact.getFile(), TLD_PATH + targetFileName );
 96  
                     }
 97  106
                     else if ( "aar".equals( type ) )
 98  
                     {
 99  2
                         copyFile( id, context, artifact.getFile(), SERVICES_PATH + targetFileName );
 100  
                     }
 101  104
                     else if ( "mar".equals( type ) )
 102  
                     {
 103  2
                         copyFile( id, context, artifact.getFile(), MODULES_PATH + targetFileName );
 104  
                     }
 105  102
                     else if ( "xar".equals( type ) )
 106  
                     {
 107  2
                         copyFile( id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName );
 108  
                     }
 109  100
                     else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
 110  
                         || "test-jar".equals( type ) )
 111  
                     {
 112  32
                         copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
 113  
                     }
 114  68
                     else if ( "par".equals( type ) )
 115  
                     {
 116  2
                         targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
 117  2
                         copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
 118  
                     }
 119  66
                     else if ( "war".equals( type ) )
 120  
                     {
 121  
                         // Nothing to do here, it is an overlay and it's already handled
 122  58
                         context.getLog().debug( "war artifacts are handled as overlays, ignoring [" + artifact + "]" );
 123  
                     }
 124  8
                     else if ( "zip".equals( type ) )
 125  
                     {
 126  
                         // Nothing to do here, it is an overlay and it's already handled
 127  8
                         context.getLog().debug( "zip artifacts are handled as overlays, ignoring [" + artifact + "]" );
 128  
                     }
 129  
                     else
 130  
                     {
 131  0
                         context.getLog().debug(
 132  
                             "Artifact of type [" + type + "] is not supported, ignoring [" + artifact + "]" );
 133  
                     }
 134  
                 }
 135  0
                 catch ( IOException e )
 136  
                 {
 137  0
                     throw new MojoExecutionException( "Failed to copy file for artifact [" + artifact + "]", e );
 138  108
                 }
 139  
             }
 140  110
         }
 141  
         }
 142  0
         catch ( InterpolationException e )
 143  
         {
 144  0
             throw new MojoExecutionException( e.getMessage(), e );
 145  124
         }
 146  124
     }
 147  
 
 148  
     /**
 149  
      * Searches a set of artifacts for duplicate filenames and returns a list
 150  
      * of duplicates.
 151  
      *
 152  
      * @param context   the packaging context
 153  
      * @param artifacts set of artifacts
 154  
      * @return List of duplicated artifacts as bundling file names
 155  
      */
 156  
     private List findDuplicates( WarPackagingContext context, Set artifacts ) 
 157  
         throws InterpolationException
 158  
     {
 159  124
         List duplicates = new ArrayList();
 160  124
         List identifiers = new ArrayList();
 161  124
         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
 162  
         {
 163  110
             Artifact artifact = (Artifact) iter.next();
 164  110
             String candidate = getArtifactFinalName( context, artifact );
 165  110
             if ( identifiers.contains( candidate ) )
 166  
             {
 167  4
                 duplicates.add( candidate );
 168  
             }
 169  
             else
 170  
             {
 171  106
                 identifiers.add( candidate );
 172  
             }
 173  110
         }
 174  124
         return duplicates;
 175  
     }
 176  
 }