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  public class PomPropertiesUtil
41  {
42      private Properties loadPropertiesFile( File file )
43          throws IOException
44      {
45          Properties fileProps = new Properties();
46          InputStream istream = null;
47          try
48          {
49              istream = new FileInputStream( file );
50              fileProps.load( istream );
51              istream.close();
52              istream = null;
53              return fileProps;
54          }
55          finally
56          {
57              IOUtil.close( istream );
58          }
59      }
60  
61      private boolean sameContents( Properties props, File file )
62          throws IOException
63      {
64          if ( !file.isFile() )
65          {
66              return false;
67          }
68  
69          Properties fileProps = loadPropertiesFile( file );
70          return fileProps.equals( props );
71      }
72  
73      private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
74                                         boolean forceCreation )
75          throws IOException
76      {
77          File outputDir = outputFile.getParentFile();
78          if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
79          {
80              throw new IOException( "Failed to create directory: " + outputDir );
81          }
82          if ( !forceCreation && sameContents( properties, outputFile ) )
83          {
84              return;
85          }
86          PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" );
87          try
88          {
89              StringWriter sw = new StringWriter();
90              properties.store( sw, null );
91  
92              BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );
93  
94              String line;
95              while ( ( line = r.readLine() ) != null )
96              {
97                  if ( !line.startsWith( "#" ) )
98                  {
99                      pw.println( line );
100                 }
101             }
102 
103             r.close();
104             r = null;
105             sw.close();
106             sw = null;
107             pw.close();
108             pw = null;
109         }
110         finally
111         {
112             IOUtil.close( pw );
113         }
114     }
115 
116     /**
117      * Creates the pom.properties file.
118      * @param session {@link MavenSession}
119      * @param project {@link MavenProject}
120      * @param archiver {@link Archiver}
121      * @param customPomPropertiesFile optional custom pom properties file
122      * @param pomPropertiesFile The pom properties file.
123      * @param forceCreation force creation true/false
124      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
125      * @throws IOException IO exception.
126      */
127     public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
128                                      File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
129         throws IOException
130     {
131         final String groupId = project.getGroupId();
132         final String artifactId = project.getArtifactId();
133         final String version = project.getVersion();
134 
135         Properties p;
136 
137         if ( customPomPropertiesFile != null )
138         {
139             p = loadPropertiesFile( customPomPropertiesFile );
140         }
141         else
142         {
143             p = new Properties();
144         }
145 
146         p.setProperty( "groupId", groupId );
147 
148         p.setProperty( "artifactId", artifactId );
149 
150         p.setProperty( "version", version );
151 
152         createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
153 
154         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
155     }
156 }