Coverage Report - org.apache.maven.plugin.ear.util.EarMavenArchiver
 
Classes in this File Line Coverage Branch Coverage Complexity
EarMavenArchiver
27%
8/29
16%
3/18
3
 
 1  
 package org.apache.maven.plugin.ear.util;
 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 java.util.List;
 23  
 import java.util.Set;
 24  
 
 25  
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 26  
 import org.apache.maven.archiver.MavenArchiver;
 27  
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 28  
 import org.apache.maven.execution.MavenSession;
 29  
 import org.apache.maven.plugin.ear.EarModule;
 30  
 import org.apache.maven.project.MavenProject;
 31  
 import org.codehaus.plexus.archiver.jar.Manifest;
 32  
 import org.codehaus.plexus.archiver.jar.ManifestException;
 33  
 
 34  
 /**
 35  
  * A custom {@link MavenArchiver} implementation that takes care
 36  
  * of setting the right classpath value according to the actual
 37  
  * path of bundled files.
 38  
  *
 39  
  * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
 40  
  */
 41  
 public class EarMavenArchiver
 42  
     extends MavenArchiver
 43  
 {
 44  
     public static final String CLASS_PATH_KEY = "Class-Path";
 45  
 
 46  
     private final List<EarModule> earModules;
 47  
 
 48  
 
 49  
     /**
 50  
      * Creates an instance with the ear modules that will be packaged
 51  
      * in the EAR archive.
 52  
      *
 53  
      * @param earModules the intitialized list of ear modules
 54  
      */
 55  
     public EarMavenArchiver( List<EarModule> earModules )
 56  3
     {
 57  3
         this.earModules = earModules;
 58  3
     }
 59  
 
 60  
     /** @deprecated */
 61  
     public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config )
 62  
                     throws ManifestException, DependencyResolutionRequiredException
 63  
     {
 64  0
         return this.getManifest( null, project, config );
 65  
     }
 66  
 
 67  
     public Manifest getManifest( MavenSession session, MavenProject project, MavenArchiveConfiguration config )
 68  
         throws ManifestException, DependencyResolutionRequiredException
 69  
     {
 70  0
         final Manifest manifest = super.getManifest( session, project, config );
 71  0
         if ( config.getManifest().isAddClasspath() )
 72  
         {
 73  0
             String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() );
 74  
             // Class-path can be customized. Let's make sure we don't overwrite this
 75  
             // with our custom change!
 76  0
             final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config );
 77  0
             if ( userSuppliedClassPathEntry != null )
 78  
             {
 79  0
                 earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
 80  
             }
 81  
 
 82  
             // Overwrite the existing one, if any
 83  0
             final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY );
 84  0
             if ( classPathAttr != null )
 85  
             {
 86  0
                 classPathAttr.setValue( earManifestClassPathEntry );
 87  
             }
 88  
             else
 89  
             {
 90  0
                 final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry );
 91  0
                 manifest.addConfiguredAttribute( attr );
 92  
             }
 93  
         }
 94  0
         return manifest;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Generates the <tt>Class-Path</tt> entry of the manifest according to
 99  
      * the list of ear modules.
 100  
      *
 101  
      * @param classPathPrefix the classpath prefix to use
 102  
      * @return the <tt>Class-Path</tt> entry
 103  
      */
 104  
     protected String generateClassPathEntry( String classPathPrefix )
 105  
     {
 106  3
         final StringBuffer classpath = new StringBuffer();
 107  3
         for ( final EarModule earModule : earModules )
 108  
         {
 109  4
             if ( !earModule.isExcluded() )
 110  
             {
 111  4
                 classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " );
 112  
             }
 113  
         }
 114  3
         return classpath.toString().trim();
 115  
     }
 116  
 
 117  
     protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config )
 118  
     {
 119  0
         if ( config.getManifestEntries() != null )
 120  
         {
 121  
             @SuppressWarnings( "unchecked" )
 122  0
             final Set<String> keys = config.getManifestEntries().keySet();
 123  0
             for ( String key : keys )
 124  
             {
 125  0
                 String value = (String) config.getManifestEntries().get( key );
 126  0
                 if ( "Class-Path".equals( key ) && value != null )
 127  
                 {
 128  0
                     return value;
 129  
 
 130  
                 }
 131  
 
 132  0
             }
 133  
         }
 134  0
         return null;
 135  
     }
 136  
 }