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