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.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Properties;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30
31
32
33
34
35
36
37
38 public class WriteProjectProperties extends AbstractWritePropertiesMojo {
39
40
41
42
43
44
45
46
47 private boolean antEchoPropertiesMode;
48
49
50
51
52
53
54 private boolean includeSystemProperties;
55
56
57
58
59
60
61 private String omit;
62
63 @Override
64 public void execute() throws MojoExecutionException, MojoFailureException {
65 validateOutputFile();
66 Properties properties = new Properties();
67 properties.putAll(project.getProperties());
68
69 Properties systemProperties = System.getProperties();
70
71
72 for (String key : systemProperties.stringPropertyNames()) {
73 String mavenValue = properties.getProperty(key);
74 String systemValue = systemProperties.getProperty(key);
75
76 if (includeSystemProperties) {
77 properties.put(key, systemValue);
78 } else if (mavenValue != null) {
79
80 properties.put(key, systemValue);
81 }
82 }
83
84 remove(properties, omit);
85
86 getLog().info("Creating " + outputFile);
87 if (antEchoPropertiesMode) {
88 echoPropertiesMode(outputFile, properties);
89 } else {
90 writeProperties(properties, outputFile);
91 }
92 }
93
94 protected void remove(Properties properties, String csv) {
95 List<String> keys = ReadPropertiesMojo.getListFromCSV(csv);
96 for (String key : keys) {
97 properties.remove(key);
98 }
99
100 }
101
102 protected void echoPropertiesMode(File file, Properties properties) throws MojoExecutionException {
103
104
105
106 SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd");
107 SimpleDateFormat today = new SimpleDateFormat("MMMM d yyyy");
108 SimpleDateFormat tstamp = new SimpleDateFormat("HHmm");
109 List<String> names = new ArrayList<String>(properties.stringPropertyNames());
110 Collections.sort(names);
111 Date now = new Date();
112 StringBuilder sb = new StringBuilder();
113 sb.append("#Ant properties\n");
114 sb.append("#" + now + "\n");
115 sb.append("DSTAMP=" + dstamp.format(now) + "\n");
116 sb.append("TODAY=" + today.format(now) + "\n");
117 sb.append("TSTAMP=" + tstamp.format(now) + "\n");
118 for (String name : names) {
119 String value = properties.getProperty(name);
120 value = value.replace("\n", "\\n");
121 value = value.replace("\r", "\\r");
122 value = value.replace("\t", "\\t");
123 value = value.replace(":", "\\:");
124 value = value.replace("#", "\\#");
125 value = value.replace("=", "\\=");
126 sb.append(name + "=" + value + "\n");
127 }
128 try {
129 FileUtils.writeByteArrayToFile(file, sb.toString().getBytes());
130 } catch (IOException e) {
131 throw new MojoExecutionException("Error creating properties file", e);
132 }
133 }
134
135 public boolean isAntEchoPropertiesMode() {
136 return antEchoPropertiesMode;
137 }
138
139 public void setAntEchoPropertiesMode(boolean antEchoPropertiesMode) {
140 this.antEchoPropertiesMode = antEchoPropertiesMode;
141 }
142
143 public boolean isIncludeSystemProperties() {
144 return includeSystemProperties;
145 }
146
147 public void setIncludeSystemProperties(boolean includeSystemProperties) {
148 this.includeSystemProperties = includeSystemProperties;
149 }
150 }