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