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.io.OutputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.Set;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.io.IOUtils;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.springframework.util.PropertyPlaceholderHelper;
35
36
37
38
39
40
41
42
43 public class WriteProjectProperties extends AbstractWritePropertiesMojo {
44
45
46
47
48
49
50
51 private boolean includeSystemProperties;
52
53
54
55
56
57
58
59 private boolean includeEnvironmentVariables;
60
61
62
63
64
65
66 private String exclude;
67
68
69
70
71
72
73
74 private String include;
75
76
77
78
79
80
81 private boolean resolvePlaceholders;
82
83 @Override
84 public void execute() throws MojoExecutionException, MojoFailureException {
85 Properties properties = new Properties();
86
87 properties.putAll(project.getProperties());
88 if (includeEnvironmentVariables) {
89
90 properties.putAll(getEnvironmentVariables());
91 }
92 if (includeSystemProperties) {
93
94 properties.putAll(System.getProperties());
95 }
96
97
98 trim(properties, exclude, include);
99
100 if (resolvePlaceholders) {
101 properties = getResolvedProperties(properties);
102 }
103
104 String comment = "# " + new Date() + "\n";
105 getLog().info("Creating " + outputFile);
106 writeProperties(outputFile, comment, properties);
107 }
108
109 protected Properties getResolvedProperties(Properties props) {
110 PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper("${", "}");
111 List<String> keys = new ArrayList<String>(props.stringPropertyNames());
112 Collections.sort(keys);
113 Properties newProps = new Properties();
114 for (String key : keys) {
115 String originalValue = props.getProperty(key);
116 String resolvedValue = pph.replacePlaceholders(originalValue, props);
117 newProps.setProperty(key, resolvedValue);
118 }
119 return newProps;
120
121 }
122
123 protected static Properties getEnvironmentVariables() {
124 String prefix = "env";
125 Map<String, String> map = System.getenv();
126 Properties props = new Properties();
127 for (String key : map.keySet()) {
128 String newKey = prefix + "." + key;
129 String value = map.get(key);
130 props.setProperty(newKey, value);
131 }
132 return props;
133 }
134
135 protected void trim(Properties properties, String excludeCSV, String includeCSV) {
136 List<String> omitKeys = ReadPropertiesMojo.getListFromCSV(excludeCSV);
137 for (String key : omitKeys) {
138 properties.remove(key);
139 }
140 if (StringUtils.isBlank(includeCSV)) {
141 return;
142 }
143 List<String> includeKeys = ReadPropertiesMojo.getListFromCSV(includeCSV);
144 Set<String> keys = properties.stringPropertyNames();
145 for (String key : keys) {
146 if (!includeKeys.contains(key)) {
147 properties.remove(key);
148 }
149 }
150 }
151
152 protected void writeProperties(File file, String comment, Properties properties) throws MojoExecutionException {
153 SortedProperties sp = new SortedProperties();
154 sp.putAll(properties);
155 OutputStream out = null;
156 try {
157 out = FileUtils.openOutputStream(file);
158 sp.store(out, comment);
159 } catch (IOException e) {
160 throw new MojoExecutionException("Error creating properties file", e);
161 } finally {
162 IOUtils.closeQuietly(out);
163 }
164 }
165
166 public boolean isIncludeSystemProperties() {
167 return includeSystemProperties;
168 }
169
170 public void setIncludeSystemProperties(boolean includeSystemProperties) {
171 this.includeSystemProperties = includeSystemProperties;
172 }
173
174 public boolean isIncludeEnvironmentVariables() {
175 return includeEnvironmentVariables;
176 }
177
178 public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
179 this.includeEnvironmentVariables = includeEnvironmentVariables;
180 }
181
182 public String getExclude() {
183 return exclude;
184 }
185
186 public void setExclude(String exclude) {
187 this.exclude = exclude;
188 }
189
190 public String getInclude() {
191 return include;
192 }
193
194 public void setInclude(String include) {
195 this.include = include;
196 }
197
198 public boolean isResolvePlaceholders() {
199 return resolvePlaceholders;
200 }
201
202 public void setResolvePlaceholders(boolean resolvePlaceholders) {
203 this.resolvePlaceholders = resolvePlaceholders;
204 }
205 }