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