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