View Javadoc

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  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          if ( !file.isFile() )
49          {
50              return false;
51          }
52          Properties fileProps = new Properties();
53          InputStream istream = null;
54          try 
55          {
56              istream = new FileInputStream( file );
57              fileProps.load( istream );
58              istream.close();
59              istream = null;
60              return fileProps.equals( props );
61          }
62          catch ( IOException e )
63          {
64              return false;
65          }
66          finally
67          {
68              IOUtil.close( istream );
69          }
70      }
71  
72      private void createPropertyFile( Properties properties, File outputFile,
73                                       boolean forceCreation )
74          throws IOException
75      {
76          File outputDir = outputFile.getParentFile();
77          if ( outputDir != null  &&  !outputDir.isDirectory()  &&  !outputDir.mkdirs() )
78          {
79              throw new IOException( "Failed to create directory: " + outputDir );
80          }
81          if ( !forceCreation  &&  sameContents( properties, outputFile ) )
82          {
83              return;
84          }
85          OutputStream os = new FileOutputStream( outputFile );
86          try
87          {
88              properties.store( os, GENERATED_BY_MAVEN );
89              os.close(); // stream is flushed but not closed by Properties.store()
90              os = null;
91          }
92          finally
93          {
94              IOUtil.close( os );
95          }
96      }
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         final String artifactId = project.getArtifactId();
106         final String groupId = project.getGroupId();
107 
108         Properties p = new Properties();
109 
110         p.setProperty( "groupId", project.getGroupId() );
111 
112         p.setProperty( "artifactId", project.getArtifactId() );
113 
114         p.setProperty( "version", project.getVersion() );
115 
116         createPropertyFile( p, pomPropertiesFile, forceCreation );
117 
118         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
119     }
120 }