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 @Override
76 public void execute() throws MojoExecutionException, MojoFailureException {
77 Properties properties = new Properties();
78
79 properties.putAll(project.getProperties());
80 if (includeEnvironmentVariables) {
81
82 properties.putAll(getEnvironmentVariables());
83 }
84 if (includeSystemProperties) {
85
86 properties.putAll(System.getProperties());
87 }
88
89
90 if (resolvePlaceholders) {
91 properties = getResolvedProperties(properties);
92 }
93
94
95 trim(properties, exclude, include);
96
97 getLog().info("Creating " + outputFile);
98 writeProperties(this.outputFile, properties, this.outputStyle, this.prefix);
99 }
100
101 protected Properties getResolvedProperties(Properties props) {
102 PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper("${", "}");
103 List<String> keys = new ArrayList<String>(props.stringPropertyNames());
104 Collections.sort(keys);
105 Properties newProps = new Properties();
106 for (String key : keys) {
107 String originalValue = props.getProperty(key);
108 String resolvedValue = pph.replacePlaceholders(originalValue, props);
109 newProps.setProperty(key, resolvedValue);
110 }
111 return newProps;
112
113 }
114
115 protected static Properties getEnvironmentVariables() {
116 String prefix = "env";
117 Map<String, String> map = System.getenv();
118 Properties props = new Properties();
119 for (String key : map.keySet()) {
120 String newKey = prefix + "." + key;
121 String value = map.get(key);
122 props.setProperty(newKey, value);
123 }
124 return props;
125 }
126
127 protected void trim(Properties properties, String excludeCSV, String includeCSV) {
128 List<String> excludes = ReadPropertiesMojo.getListFromCSV(excludeCSV);
129 List<String> includes = ReadPropertiesMojo.getListFromCSV(includeCSV);
130 List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
131 Collections.sort(keys);
132 for (String key : keys) {
133 boolean include = include(key, includes, excludes);
134 if (!include) {
135 properties.remove(key);
136 }
137 }
138 }
139
140 public boolean include(String value, List<String> includes, List<String> excludes) {
141 if (excludes.contains(value)) {
142 return false;
143 } else {
144 return includes.size() == 0 || includes.contains(value);
145 }
146 }
147
148 public boolean isIncludeSystemProperties() {
149 return includeSystemProperties;
150 }
151
152 public void setIncludeSystemProperties(boolean includeSystemProperties) {
153 this.includeSystemProperties = includeSystemProperties;
154 }
155
156 public boolean isIncludeEnvironmentVariables() {
157 return includeEnvironmentVariables;
158 }
159
160 public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
161 this.includeEnvironmentVariables = includeEnvironmentVariables;
162 }
163
164 public String getExclude() {
165 return exclude;
166 }
167
168 public void setExclude(String exclude) {
169 this.exclude = exclude;
170 }
171
172 public String getInclude() {
173 return include;
174 }
175
176 public void setInclude(String include) {
177 this.include = include;
178 }
179
180 public boolean isResolvePlaceholders() {
181 return resolvePlaceholders;
182 }
183
184 public void setResolvePlaceholders(boolean resolvePlaceholders) {
185 this.resolvePlaceholders = resolvePlaceholders;
186 }
187 }