Coverage Report - org.apache.maven.archiver.PomPropertiesUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
PomPropertiesUtil
91%
31/34
83%
10/12
5
 
 1  
 package org.apache.maven.archiver;
 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.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.io.InputStream;
 27  
 import java.io.OutputStream;
 28  
 import java.util.Properties;
 29  
 
 30  
 import org.apache.maven.project.MavenProject;
 31  
 import org.codehaus.plexus.archiver.Archiver;
 32  
 import org.codehaus.plexus.archiver.ArchiverException;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 
 35  
 
 36  
 /**
 37  
  * This class is responsible for creating the pom.properties
 38  
  * file.
 39  
  * @version $Id: PomPropertiesUtil.java 661727 2008-05-30 14:21:49Z bentmann $
 40  
  */
 41  18
 public class PomPropertiesUtil
 42  
 {
 43  
     private static final String GENERATED_BY_MAVEN = "Generated by Maven";
 44  
 
 45  
     private boolean sameContents( Properties props, File file )
 46  
             throws IOException
 47  
     {
 48  4
         if ( !file.isFile() )
 49  
         {
 50  1
             return false;
 51  
         }
 52  3
         Properties fileProps = new Properties();
 53  3
         InputStream istream = null;
 54  
         try 
 55  
         {
 56  3
             istream = new FileInputStream( file );
 57  3
             fileProps.load( istream );
 58  3
             istream.close();
 59  3
             istream = null;
 60  3
             return fileProps.equals( props );
 61  
         }
 62  0
         catch ( IOException e )
 63  
         {
 64  0
             return false;
 65  
         }
 66  
         finally
 67  
         {
 68  3
             IOUtil.close( istream );
 69  
         }
 70  
     }
 71  
 
 72  
     private void createPropertyFile( Properties properties, File outputFile,
 73  
                                      boolean forceCreation )
 74  
         throws IOException
 75  
     {
 76  18
         File outputDir = outputFile.getParentFile();
 77  18
         if ( outputDir != null  &&  !outputDir.isDirectory()  &&  !outputDir.mkdirs() )
 78  
         {
 79  0
             throw new IOException( "Failed to create directory: " + outputDir );
 80  
         }
 81  18
         if ( !forceCreation  &&  sameContents( properties, outputFile ) )
 82  
         {
 83  3
             return;
 84  
         }
 85  15
         OutputStream os = new FileOutputStream( outputFile );
 86  
         try
 87  
         {
 88  15
             properties.store( os, GENERATED_BY_MAVEN );
 89  15
             os.close(); // stream is flushed but not closed by Properties.store()
 90  15
             os = null;
 91  
         }
 92  
         finally
 93  
         {
 94  15
             IOUtil.close( os );
 95  15
         }
 96  15
     }
 97  
 
 98  
     /**
 99  
      * Creates the pom.properties file.
 100  
      */
 101  
     public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesFile,
 102  
                                      boolean forceCreation )
 103  
         throws ArchiverException, IOException
 104  
     {
 105  18
         final String artifactId = project.getArtifactId();
 106  18
         final String groupId = project.getGroupId();
 107  
 
 108  18
         Properties p = new Properties();
 109  
 
 110  18
         p.setProperty( "groupId", project.getGroupId() );
 111  
 
 112  18
         p.setProperty( "artifactId", project.getArtifactId() );
 113  
 
 114  18
         p.setProperty( "version", project.getVersion() );
 115  
 
 116  18
         createPropertyFile( p, pomPropertiesFile, forceCreation );
 117  
 
 118  18
         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
 119  18
     }
 120  
 }