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.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  import java.util.Properties;
34  
35  import org.apache.maven.execution.MavenSession;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.shared.utils.io.IOUtil;
38  import org.codehaus.plexus.archiver.Archiver;
39  
40  /**
41   * This class is responsible for creating the <code>pom.properties</code> file
42   * in <code>META-INF/maven/${groupId}/${artifactId}</code>.
43   */
44  public class PomPropertiesUtil
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              StringWriter sw = new StringWriter();
94              properties.store( sw, null );
95  
96              BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );
97  
98              List<String> lines = new ArrayList<String>();
99              String line;
100             while ( ( line = r.readLine() ) != null )
101             {
102                 if ( !line.startsWith( "#" ) )
103                 {
104                     lines.add( line );
105                 }
106             }
107 
108             r.close();
109             r = null;
110             sw.close();
111             sw = null;
112 
113             Collections.sort( lines );
114             for ( String l : lines )
115             {
116                 pw.println( l );
117             }
118 
119             pw.close();
120             pw = null;
121         }
122         finally
123         {
124             IOUtil.close( pw );
125         }
126     }
127 
128     /**
129      * Creates the pom.properties file.
130      * @param session {@link MavenSession}
131      * @param project {@link MavenProject}
132      * @param archiver {@link Archiver}
133      * @param customPomPropertiesFile optional custom pom properties file
134      * @param pomPropertiesFile The pom properties file.
135      * @param forceCreation force creation true/false
136      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
137      * @throws IOException IO exception.
138      */
139     public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
140                                      File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
141         throws IOException
142     {
143         final String groupId = project.getGroupId();
144         final String artifactId = project.getArtifactId();
145         final String version = project.getVersion();
146 
147         Properties p;
148 
149         if ( customPomPropertiesFile != null )
150         {
151             p = loadPropertiesFile( customPomPropertiesFile );
152         }
153         else
154         {
155             p = new Properties();
156         }
157 
158         p.setProperty( "groupId", groupId );
159 
160         p.setProperty( "artifactId", artifactId );
161 
162         p.setProperty( "version", version );
163 
164         createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
165 
166         archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
167     }
168 }