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