View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archiver;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.PrintWriter;
26  import java.io.StringReader;
27  import java.io.StringWriter;
28  import java.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Properties;
33  
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.archiver.Archiver;
37  
38  /**
39   * This class is responsible for creating the <code>pom.properties</code> file
40   * in <code>META-INF/maven/${groupId}/${artifactId}</code>.
41   *
42   * @author slachiewicz
43   * @version $Id: $Id
44   */
45  public class PomPropertiesUtil {
46      private Properties loadPropertiesFile(File file) throws IOException {
47          Properties fileProps = new Properties();
48          try (InputStream istream = Files.newInputStream(file.toPath())) {
49              fileProps.load(istream);
50              return fileProps;
51          }
52      }
53  
54      private boolean sameContents(Properties props, File file) throws IOException {
55          if (!file.isFile()) {
56              return false;
57          }
58  
59          Properties fileProps = loadPropertiesFile(file);
60          return fileProps.equals(props);
61      }
62  
63      private void createPropertiesFile(Properties properties, File outputFile, boolean forceCreation)
64              throws IOException {
65          File outputDir = outputFile.getParentFile();
66          if (outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs()) {
67              throw new IOException("Failed to create directory: " + outputDir);
68          }
69          if (!forceCreation && sameContents(properties, outputFile)) {
70              return;
71          }
72  
73          try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1");
74                  StringWriter sw = new StringWriter()) {
75  
76              properties.store(sw, null);
77  
78              List<String> lines = new ArrayList<>();
79              try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) {
80                  String line;
81                  while ((line = r.readLine()) != null) {
82                      if (!line.startsWith("#")) {
83                          lines.add(line);
84                      }
85                  }
86              }
87  
88              Collections.sort(lines);
89              for (String l : lines) {
90                  pw.println(l);
91              }
92          }
93      }
94  
95      /**
96       * Creates the pom.properties file.
97       *
98       * @param session {@link org.apache.maven.execution.MavenSession}
99       * @param project {@link org.apache.maven.project.MavenProject}
100      * @param archiver {@link org.codehaus.plexus.archiver.Archiver}
101      * @param customPomPropertiesFile optional custom pom properties file
102      * @param pomPropertiesFile The pom properties file.
103      * @param forceCreation force creation true/false
104      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
105      * @throws java.io.IOException IO exception.
106      */
107     public void createPomProperties(
108             MavenSession session,
109             MavenProject project,
110             Archiver archiver,
111             File customPomPropertiesFile,
112             File pomPropertiesFile,
113             boolean forceCreation)
114             throws IOException {
115         final String groupId = project.getGroupId();
116         final String artifactId = project.getArtifactId();
117         final String version = project.getVersion();
118 
119         Properties p;
120 
121         if (customPomPropertiesFile != null) {
122             p = loadPropertiesFile(customPomPropertiesFile);
123         } else {
124             p = new Properties();
125         }
126 
127         p.setProperty("groupId", groupId);
128 
129         p.setProperty("artifactId", artifactId);
130 
131         p.setProperty("version", version);
132 
133         createPropertiesFile(p, pomPropertiesFile, forceCreation);
134 
135         archiver.addFile(pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties");
136     }
137 }