1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
39
40
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
57
58
59 ByteArrayOutputStream baos = new ByteArrayOutputStream();
60 properties.store(baos, null);
61
62
63 String output = baos.toString(StandardCharsets.ISO_8859_1)
64 .lines()
65 .filter(line -> !line.startsWith("#"))
66 .sorted()
67 .collect(Collectors.joining("\n", "", "\n"));
68 try (Writer writer = new CachingWriter(outputFile, StandardCharsets.ISO_8859_1)) {
69 writer.write(output);
70 }
71 }
72
73
74
75
76
77
78
79
80
81
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 }