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.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.Writer;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.Arrays;
30 import java.util.Properties;
31 import java.util.stream.Collectors;
32
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.project.MavenProject;
35 import org.codehaus.plexus.archiver.Archiver;
36 import org.codehaus.plexus.util.io.CachingWriter;
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 void createPropertiesFile(Properties properties, Path outputFile) throws IOException {
55 Path outputDir = outputFile.getParent();
56 if (outputDir != null) {
57 Files.createDirectories(outputDir);
58 }
59 // For reproducible builds, sort the properties and drop comments.
60 // The java.util.Properties class doesn't guarantee order so we have
61 // to write the file using a Writer.
62 ByteArrayOutputStream baos = new ByteArrayOutputStream();
63 properties.store(baos, null);
64 // The encoding can be either UTF-8 or ISO-8859-1, as any non ascii character
65 // is transformed into a \\uxxxx sequence anyway
66 String output = Arrays.stream(
67 baos.toString(StandardCharsets.ISO_8859_1.name()).split("\\r?\\n"))
68 .filter(line -> !line.startsWith("#"))
69 .sorted()
70 .collect(Collectors.joining("\n", "", "\n"));
71 try (Writer writer = new CachingWriter(outputFile, StandardCharsets.ISO_8859_1)) {
72 writer.write(output);
73 }
74 }
75
76 /**
77 * Creates the pom.properties file.
78 *
79 * @param session {@link org.apache.maven.execution.MavenSession}
80 * @param project {@link org.apache.maven.project.MavenProject}
81 * @param archiver {@link org.codehaus.plexus.archiver.Archiver}
82 * @param customPomPropertiesFile optional custom pom properties file
83 * @param pomPropertiesFile The pom properties file.
84 * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
85 * @throws java.io.IOException IO exception.
86 * @deprecated please use {@link #createPomProperties(MavenProject, Archiver, File, File, boolean)}
87 */
88 @Deprecated
89 public void createPomProperties(
90 MavenSession session,
91 MavenProject project,
92 Archiver archiver,
93 File customPomPropertiesFile,
94 File pomPropertiesFile,
95 boolean forceCreation)
96 throws IOException {
97 createPomProperties(project, archiver, customPomPropertiesFile, pomPropertiesFile, forceCreation);
98 }
99
100 /**
101 * Creates the pom.properties file.
102 *
103 * @param project {@link org.apache.maven.project.MavenProject}
104 * @param archiver {@link org.codehaus.plexus.archiver.Archiver}
105 * @param customPomPropertiesFile optional custom pom properties file
106 * @param pomPropertiesFile The pom properties file.
107 * @param forceCreation force creation true/false
108 * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
109 * @throws java.io.IOException IO exception.
110 */
111 public void createPomProperties(
112 MavenProject project,
113 Archiver archiver,
114 File customPomPropertiesFile,
115 File pomPropertiesFile,
116 boolean forceCreation)
117 throws IOException {
118 final String groupId = project.getGroupId();
119 final String artifactId = project.getArtifactId();
120 final String version = project.getVersion();
121
122 Properties p;
123
124 if (customPomPropertiesFile != null) {
125 p = loadPropertiesFile(customPomPropertiesFile);
126 } else {
127 p = new Properties();
128 }
129
130 p.setProperty("groupId", groupId);
131
132 p.setProperty("artifactId", artifactId);
133
134 p.setProperty("version", version);
135
136 createPropertiesFile(p, pomPropertiesFile.toPath());
137
138 archiver.addFile(pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties");
139 }
140 }