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.shared.archiver;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.Writer;
25  import java.nio.charset.StandardCharsets;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.util.Properties;
29  import java.util.stream.Collectors;
30  
31  import org.apache.maven.api.Project;
32  import org.codehaus.plexus.archiver.Archiver;
33  import org.codehaus.plexus.util.io.CachingWriter;
34  
35  /**
36   * This class is responsible for creating the <code>pom.properties</code> file
37   * in <code>META-INF/maven/${groupId}/${artifactId}</code>.
38   *
39   * @author slachiewicz
40   * @version $Id: $Id
41   */
42  public class PomPropertiesUtil {
43      private Properties loadPropertiesFile(Path file) throws IOException {
44          Properties fileProps = new Properties();
45          try (InputStream istream = Files.newInputStream(file)) {
46              fileProps.load(istream);
47              return fileProps;
48          }
49      }
50  
51      private void createPropertiesFile(Properties properties, Path outputFile) throws IOException {
52          Path outputDir = outputFile.getParent();
53          if (outputDir != null) {
54              Files.createDirectories(outputDir);
55          }
56          // For reproducible builds, sort the properties and drop comments.
57          // The java.util.Properties class doesn't guarantee order so we have
58          // to write the file using a Writer.
59          ByteArrayOutputStream baos = new ByteArrayOutputStream();
60          properties.store(baos, null);
61          // The encoding can be either UTF-8 or ISO-8859-1, as any non ascii character
62          // is transformed into a \\uxxxx sequence anyway
63          String output = baos.toString(StandardCharsets.ISO_8859_1)
64                  .lines()
65                  .filter(line -> !line.startsWith("#"))
66                  .sorted()
67                  .collect(Collectors.joining("\n", "", "\n")); // system independent new line
68          try (Writer writer = new CachingWriter(outputFile, StandardCharsets.ISO_8859_1)) {
69              writer.write(output);
70          }
71      }
72  
73      /**
74       * Creates the pom.properties file.
75       *
76       * @param project {@link org.apache.maven.api.Project}
77       * @param archiver {@link org.codehaus.plexus.archiver.Archiver}
78       * @param customPomPropertiesFile optional custom pom properties file
79       * @param pomPropertiesFile The pom properties file.
80       * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
81       * @throws java.io.IOException IO exception.
82       */
83      public void createPomProperties(
84              Project project, Archiver archiver, Path customPomPropertiesFile, Path pomPropertiesFile)
85              throws IOException {
86          final String groupId = project.getGroupId();
87          final String artifactId = project.getArtifactId();
88          final String version = project.getVersion();
89          createPomProperties(groupId, artifactId, version, archiver, customPomPropertiesFile, pomPropertiesFile);
90      }
91  
92      public void createPomProperties(
93              String groupId,
94              String artifactId,
95              String version,
96              Archiver archiver,
97              Path customPomPropertiesFile,
98              Path pomPropertiesFile)
99              throws IOException {
100         Properties p;
101 
102         if (customPomPropertiesFile != null) {
103             p = loadPropertiesFile(customPomPropertiesFile);
104         } else {
105             p = new Properties();
106         }
107 
108         p.setProperty("groupId", groupId);
109 
110         p.setProperty("artifactId", artifactId);
111 
112         p.setProperty("version", version);
113 
114         createPropertiesFile(p, pomPropertiesFile);
115 
116         archiver.addFile(
117                 pomPropertiesFile.toFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties");
118     }
119 }