1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41
42
43
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
97
98
99
100
101
102
103
104
105
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 }