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 
108     @Deprecated
109     public void createPomProperties(
110             MavenSession session,
111             MavenProject project,
112             Archiver archiver,
113             File customPomPropertiesFile,
114             File pomPropertiesFile,
115             boolean forceCreation)
116             throws IOException {
117         createPomProperties(project, archiver, customPomPropertiesFile, pomPropertiesFile, forceCreation);
118     }
119 
120     
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131     public void createPomProperties(
132             MavenProject project,
133             Archiver archiver,
134             File customPomPropertiesFile,
135             File pomPropertiesFile,
136             boolean forceCreation)
137             throws IOException {
138         final String groupId = project.getGroupId();
139         final String artifactId = project.getArtifactId();
140         final String version = project.getVersion();
141 
142         Properties p;
143 
144         if (customPomPropertiesFile != null) {
145             p = loadPropertiesFile(customPomPropertiesFile);
146         } else {
147             p = new Properties();
148         }
149 
150         p.setProperty("groupId", groupId);
151 
152         p.setProperty("artifactId", artifactId);
153 
154         p.setProperty("version", version);
155 
156         createPropertiesFile(p, pomPropertiesFile, forceCreation);
157 
158         archiver.addFile(pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties");
159     }
160 }