Coverage Report - org.apache.maven.plugin.assembly.archive.ManifestCreationFinalizer
 
Classes in this File Line Coverage Branch Coverage Complexity
ManifestCreationFinalizer
55%
19/34
58%
7/12
6,667
 
 1  
 package org.apache.maven.plugin.assembly.archive;
 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.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.FileNotFoundException;
 25  
 import java.io.IOException;
 26  
 import java.io.InputStreamReader;
 27  
 import java.io.Reader;
 28  
 import java.util.Collections;
 29  
 import java.util.List;
 30  
 
 31  
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 32  
 import org.apache.maven.archiver.MavenArchiver;
 33  
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 34  
 import org.apache.maven.project.MavenProject;
 35  
 import org.codehaus.plexus.archiver.AbstractArchiveFinalizer;
 36  
 import org.codehaus.plexus.archiver.Archiver;
 37  
 import org.codehaus.plexus.archiver.ArchiverException;
 38  
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 39  
 import org.codehaus.plexus.archiver.jar.Manifest;
 40  
 import org.codehaus.plexus.archiver.jar.ManifestException;
 41  
 import org.codehaus.plexus.util.IOUtil;
 42  
 
 43  
 /**
 44  
  * @version $Id: ManifestCreationFinalizer.java 728546 2008-12-21 22:56:51Z bentmann $
 45  
  */
 46  
 public class ManifestCreationFinalizer
 47  
     extends AbstractArchiveFinalizer
 48  
 {
 49  
 
 50  
     private final MavenProject project;
 51  
     private final MavenArchiveConfiguration archiveConfiguration;
 52  
 
 53  
     // TODO: I'd really prefer to rewrite MavenArchiver as either a
 54  
     // separate manifest creation utility (and to
 55  
     // create an include pom.properties etc into another archiver), or
 56  
     // an implementation of an archiver
 57  
     // (the first is preferable).
 58  10
     private MavenArchiver mavenArchiver = new MavenArchiver();
 59  
 
 60  
     public ManifestCreationFinalizer( MavenProject project, MavenArchiveConfiguration archiveConfiguration )
 61  10
     {
 62  10
         this.project = project;
 63  10
         this.archiveConfiguration = archiveConfiguration;
 64  10
     }
 65  
 
 66  
     public void finalizeArchiveCreation( Archiver archiver )
 67  
         throws ArchiverException
 68  
     {
 69  8
         if ( archiveConfiguration != null )
 70  
         {
 71  
             try
 72  
             {
 73  
                 Manifest manifest;
 74  6
                 File manifestFile = archiveConfiguration.getManifestFile();
 75  
 
 76  6
                 if ( manifestFile != null )
 77  
                 {
 78  2
                     Reader manifestFileReader = null;
 79  
                     try
 80  
                     {
 81  2
                         manifestFileReader = new InputStreamReader( new FileInputStream( manifestFile ), "UTF-8" );
 82  2
                         manifest = new Manifest( manifestFileReader );
 83  
                     }
 84  0
                     catch ( FileNotFoundException e )
 85  
                     {
 86  0
                         throw new ArchiverException( "Manifest not found: " + e.getMessage(), e );
 87  
                     }
 88  0
                     catch ( IOException e )
 89  
                     {
 90  0
                         throw new ArchiverException( "Error processing manifest: " + e.getMessage(), e );
 91  
                     }
 92  
                     finally
 93  
                     {
 94  2
                         IOUtil.close( manifestFileReader );
 95  2
                     }
 96  
                 }
 97  
                 else
 98  
                 {
 99  4
                     manifest = mavenArchiver.getManifest( project, archiveConfiguration );
 100  
                 }
 101  
 
 102  6
                 if ( ( manifest != null ) && ( archiver instanceof JarArchiver ) )
 103  
                 {
 104  4
                     JarArchiver jarArchiver = (JarArchiver) archiver;
 105  4
                     jarArchiver.addConfiguredManifest( manifest );
 106  
                 }
 107  
             }
 108  0
             catch ( ManifestException e )
 109  
             {
 110  0
                 throw new ArchiverException( "Error creating manifest: " + e.getMessage(), e );
 111  
             }
 112  0
             catch ( DependencyResolutionRequiredException e )
 113  
             {
 114  0
                 throw new ArchiverException( "Dependencies were not resolved: " + e.getMessage(), e );
 115  6
             }
 116  
         }
 117  8
     }
 118  
 
 119  
     public List getVirtualFiles()
 120  
     {
 121  0
         if ( archiveConfiguration != null )
 122  
         {
 123  
             try
 124  
             {
 125  0
                 if ( mavenArchiver.getManifest( project, archiveConfiguration.getManifest() ) != null )
 126  
                 {
 127  0
                     return Collections.singletonList( "META-INF/MANIFEST.MF" );
 128  
                 }
 129  
             }
 130  0
             catch ( ManifestException e )
 131  
             {
 132  
             }
 133  0
             catch ( DependencyResolutionRequiredException e )
 134  
             {
 135  0
             }
 136  
         }
 137  
 
 138  0
         return null;
 139  
     }
 140  
 
 141  
 }