1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.properties;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Enumeration;
23 import java.util.List;
24 import java.util.Properties;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29
30
31
32
33
34
35
36
37 public class WriteProjectProperties extends AbstractWritePropertiesMojo {
38
39
40
41
42
43
44
45 private boolean writeSorted;
46
47 @Override
48 public void execute() throws MojoExecutionException, MojoFailureException {
49 validateOutputFile();
50 Properties properties = new Properties();
51 properties.putAll(project.getProperties());
52
53 Properties systemProperties = System.getProperties();
54
55
56 Enumeration<?> enumeration = systemProperties.keys();
57 while (enumeration.hasMoreElements()) {
58 String key = (String) enumeration.nextElement();
59 String value = systemProperties.getProperty(key);
60 if (properties.get(key) != null) {
61 properties.put(key, value);
62 }
63
64 }
65
66 getLog().info("Creating " + outputFile);
67 writeProperties(properties, outputFile);
68 if (writeSorted) {
69 createSorted(outputFile, properties);
70 }
71 }
72
73 protected void createSorted(File file, Properties properties) throws MojoExecutionException {
74 List<String> names = new ArrayList<String>(properties.stringPropertyNames());
75 Collections.sort(names);
76 StringBuilder sb = new StringBuilder();
77 for (String name : names) {
78 String value = properties.getProperty(name);
79 value = value.replace("\n", "\\n");
80 value = value.replace("\t", "\\t");
81 value = value.replace(":", "\\:");
82 value = value.replace("#", "\\#");
83 value = value.replace("=", "\\=");
84 sb.append(name + "=" + value + "\n");
85 }
86 try {
87 String filename = outputFile.getAbsolutePath() + ".sorted";
88 getLog().info("Creating " + filename);
89 FileUtils.writeByteArrayToFile(new File(filename), sb.toString().getBytes());
90 } catch (IOException e) {
91 throw new MojoExecutionException("Error creating sorted file", e);
92 }
93 }
94
95 public boolean isWriteSorted() {
96 return writeSorted;
97 }
98
99 public void setWriteSorted(boolean writeSorted) {
100 this.writeSorted = writeSorted;
101 }
102 }