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.execution.MavenSession;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.archiver.Archiver;
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 1741255 2016-04-27 13:14:30Z andham $
39   */
40  public class PomPropertiesUtil
41  {
42      private static final String CREATED_BY_MAVEN = "Created by Apache Maven";
43  
44      private Properties loadPropertiesFile( File file )
45          throws IOException
46      {
47          Properties fileProps = new Properties();
48          InputStream istream = null;
49          try
50          {
51              istream = new FileInputStream( file );
52              fileProps.load( istream );
53              istream.close();
54              istream = null;
55              return fileProps;
56          }
57          finally
58          {
59              IOUtil.close( istream );
60          }
61      }
62  
63      private boolean sameContents( Properties props, File file )
64          throws IOException
65      {
66          if ( !file.isFile() )
67          {
68              return false;
69          }
70  
71          Properties fileProps = loadPropertiesFile( file );
72          return fileProps.equals( props );
73      }
74  
75      private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
76                                         boolean forceCreation )
77          throws IOException
78      {
79          File outputDir = outputFile.getParentFile();
80          if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
81          {
82              throw new IOException( "Failed to create directory: " + outputDir );
83          }
84          if ( !forceCreation && sameContents( properties, outputFile ) )
85          {
86              return;
87          }
88          OutputStream os = new FileOutputStream( outputFile );
89          try
90          {
91              String createdBy = CREATED_BY_MAVEN;
92              if ( session != null ) // can be null due to API backwards compatibility
93              {
94                  String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );
95                  if ( mavenVersion != null )
96                  {
97                      createdBy += " " + mavenVersion;
98                  }
99              }
100 
101             properties.store( os, createdBy );
102             os.close(); // stream is flushed but not closed by Properties.store()
103             os = null;
104         }
105         finally
106         {
107             IOUtil.close( os );
108         }
109     }
110 
111     /**
112      * Creates the pom.properties file.
113      * @param session {@link MavenSession}
114      * @param project {@link MavenProject}
115      * @param archiver {@link Archiver}
116      * @param customPomPropertiesFile optional custom pom properties file
117      * @param pomPropertiesFile The pom properties file.
118      * @param forceCreation force creation true/false
119      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
120      * @throws IOException IO exception.
121      */
122     public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
123                                      File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
124         throws IOException
125     {
126         final String groupId = project.getGroupId();
127         final String artifactId = project.getArtifactId();
128         final String version = project.getVersion();
129 
130         Properties p;
131 
132         if ( customPomPropertiesFile != null )
133         {
134             p = loadPropertiesFile( customPomPropertiesFile );
135         }
136         else
137         {
138             p = new Properties();
139         }
140 
141         p.setProperty( "groupId", groupId );
142 
143         p.setProperty( "artifactId", artifactId );
144 
145         p.setProperty( "version", version );
146 
147         createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
148 
149         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
150     }
151 }