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