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.apache.maven.shared.utils.io.IOUtil;
34  
35  /**
36   * This class is responsible for creating the pom.properties file.
37   * 
38   * @version $Id: PomPropertiesUtil.java 1634984 2014-10-28 21:01:38Z khmarbaise $
39   */
40  public class PomPropertiesUtil
41  {
42      private static final String GENERATED_BY_MAVEN = "Generated by Apache Maven";
43  
44      private boolean sameContents( Properties props, File file )
45          throws IOException
46      {
47          if ( !file.isFile() )
48          {
49              return false;
50          }
51          Properties fileProps = new Properties();
52          InputStream istream = null;
53          try
54          {
55              istream = new FileInputStream( file );
56              fileProps.load( istream );
57              istream.close();
58              istream = null;
59              return fileProps.equals( props );
60          }
61          catch ( IOException e )
62          {
63              return false;
64          }
65          finally
66          {
67              IOUtil.close( istream );
68          }
69      }
70  
71      private void createPropertyFile( Properties properties, File outputFile, boolean forceCreation )
72          throws IOException
73      {
74          File outputDir = outputFile.getParentFile();
75          if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
76          {
77              throw new IOException( "Failed to create directory: " + outputDir );
78          }
79          if ( !forceCreation && sameContents( properties, outputFile ) )
80          {
81              return;
82          }
83          OutputStream os = new FileOutputStream( outputFile );
84          try
85          {
86              properties.store( os, GENERATED_BY_MAVEN );
87              os.close(); // stream is flushed but not closed by Properties.store()
88              os = null;
89          }
90          finally
91          {
92              IOUtil.close( os );
93          }
94      }
95  
96      /**
97       * Creates the pom.properties file.
98       * @param project {@link MavenProject}
99       * @param archiver {@link Archiver}
100      * @param pomPropertiesFile The pom properties file.
101      * @param forceCreation force creation true/flas.e
102      * @throws ArchiverException archiver exception.
103      * @throws IOException IO exception.
104      */
105     public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesFile,
106                                      boolean forceCreation )
107         throws ArchiverException, IOException
108     {
109         final String artifactId = project.getArtifactId();
110         final String groupId = project.getGroupId();
111 
112         Properties p = new Properties();
113 
114         p.setProperty( "groupId", project.getGroupId() );
115 
116         p.setProperty( "artifactId", project.getArtifactId() );
117 
118         p.setProperty( "version", project.getVersion() );
119 
120         createPropertyFile( p, pomPropertiesFile, forceCreation );
121 
122         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
123     }
124 }