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.BufferedReader;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.PrintWriter;
28  import java.io.StringReader;
29  import java.io.StringWriter;
30  import java.util.Properties;
31  
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.shared.utils.io.IOUtil;
35  import org.codehaus.plexus.archiver.Archiver;
36  
37  /**
38   * This class is responsible for creating the pom.properties file.
39   *
40   * @version $Id: PomPropertiesUtil.java 1745000 2016-05-21 23:00:43Z michaelo $
41   */
42  public class PomPropertiesUtil
43  {
44      private static final String CREATED_BY_MAVEN = "Created by Apache Maven";
45  
46      private Properties loadPropertiesFile( File file )
47          throws IOException
48      {
49          Properties fileProps = new Properties();
50          InputStream istream = null;
51          try
52          {
53              istream = new FileInputStream( file );
54              fileProps.load( istream );
55              istream.close();
56              istream = null;
57              return fileProps;
58          }
59          finally
60          {
61              IOUtil.close( istream );
62          }
63      }
64  
65      private boolean sameContents( Properties props, File file )
66          throws IOException
67      {
68          if ( !file.isFile() )
69          {
70              return false;
71          }
72  
73          Properties fileProps = loadPropertiesFile( file );
74          return fileProps.equals( props );
75      }
76  
77      private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
78                                         boolean forceCreation )
79          throws IOException
80      {
81          File outputDir = outputFile.getParentFile();
82          if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
83          {
84              throw new IOException( "Failed to create directory: " + outputDir );
85          }
86          if ( !forceCreation && sameContents( properties, outputFile ) )
87          {
88              return;
89          }
90          PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" );
91          try
92          {
93              String createdBy = CREATED_BY_MAVEN;
94              if ( session != null ) // can be null due to API backwards compatibility
95              {
96                  String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );
97                  if ( mavenVersion != null )
98                  {
99                      createdBy += " " + mavenVersion;
100                 }
101             }
102 
103             StringWriter sw = new StringWriter();
104             properties.store( sw, null );
105 
106             BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );
107 
108             pw.println( "#" + createdBy );
109             String line;
110             while ( ( line = r.readLine() ) != null )
111             {
112                 if ( !line.startsWith( "#" ) )
113                 {
114                     pw.println( line );
115                 }
116             }
117 
118             r.close();
119             r = null;
120             sw.close();
121             sw = null;
122             pw.close();
123             pw = null;
124         }
125         finally
126         {
127             IOUtil.close( pw );
128         }
129     }
130 
131     /**
132      * Creates the pom.properties file.
133      * @param session {@link MavenSession}
134      * @param project {@link MavenProject}
135      * @param archiver {@link Archiver}
136      * @param customPomPropertiesFile optional custom pom properties file
137      * @param pomPropertiesFile The pom properties file.
138      * @param forceCreation force creation true/false
139      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
140      * @throws IOException IO exception.
141      */
142     public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
143                                      File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
144         throws IOException
145     {
146         final String groupId = project.getGroupId();
147         final String artifactId = project.getArtifactId();
148         final String version = project.getVersion();
149 
150         Properties p;
151 
152         if ( customPomPropertiesFile != null )
153         {
154             p = loadPropertiesFile( customPomPropertiesFile );
155         }
156         else
157         {
158             p = new Properties();
159         }
160 
161         p.setProperty( "groupId", groupId );
162 
163         p.setProperty( "artifactId", artifactId );
164 
165         p.setProperty( "version", version );
166 
167         createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
168 
169         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
170     }
171 }