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.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Properties;
25 import java.util.Set;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.util.cli.CommandLineUtils;
33 import org.springframework.core.io.DefaultResourceLoader;
34 import org.springframework.core.io.Resource;
35 import org.springframework.core.io.ResourceLoader;
36
37
38
39
40
41
42
43
44
45
46 public class ReadPropertiesMojo extends AbstractMojo {
47
48
49
50
51
52
53 private MavenProject project;
54
55
56
57
58
59
60
61
62 private String[] locations;
63
64
65
66
67
68
69 private boolean quiet;
70
71
72
73
74
75
76 private String ignore;
77
78 @Override
79 public void execute() throws MojoExecutionException {
80 List<String> ignoreList = getListFromCSV(ignore);
81 Properties projectProperties = project.getProperties();
82 if (!StringUtils.isBlank(ignore)) {
83 getLog().info("Ignoring " + ignore);
84 }
85 for (int i = 0; i < locations.length; i++) {
86 String location = locations[i];
87 if (!validate(location)) {
88 continue;
89 }
90 getLog().info("Loading " + location);
91 Properties p = getProperties(location);
92 updateProperties(projectProperties, p, ignoreList);
93 }
94
95 Properties env = getEnvironment();
96 for (String name : projectProperties.stringPropertyNames()) {
97 String value = getPropertyValue(name, projectProperties, env);
98 projectProperties.setProperty(name, value);
99 }
100 }
101
102 protected Properties getEnvironment() throws MojoExecutionException {
103 try {
104 return CommandLineUtils.getSystemEnvVars();
105 } catch (IOException e) {
106 throw new MojoExecutionException("Error get environment variables", e);
107 }
108 }
109
110 protected void updateProperties(Properties p1, Properties p2, List<String> ignore) {
111 Set<String> names = p2.stringPropertyNames();
112 for (String name : names) {
113 if (!ignore.contains(name)) {
114 String value = p2.getProperty(name);
115 p1.setProperty(name, value);
116 }
117 }
118 }
119
120 protected static final List<String> getListFromCSV(String csv) {
121 if (StringUtils.isBlank(csv)) {
122 return new ArrayList<String>();
123 }
124 List<String> list = new ArrayList<String>();
125 String[] tokens = StringUtils.split(csv, ",");
126 for (String token : tokens) {
127 list.add(token.trim());
128 }
129 return list;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 protected String getPropertyValue(String k, Properties p, Properties environment) {
150 String v = p.getProperty(k);
151 String ret = "";
152 int idx, idx2;
153
154 while ((idx = v.indexOf("${")) >= 0) {
155
156 ret += v.substring(0, idx);
157
158
159 v = v.substring(idx + 2);
160
161 idx2 = v.indexOf("}");
162
163
164 if (idx2 < 0) {
165 break;
166 }
167
168
169
170 String nk = v.substring(0, idx2);
171 v = v.substring(idx2 + 1);
172 String nv = p.getProperty(nk);
173
174
175 if (nv == null) {
176 nv = System.getProperty(nk);
177 }
178
179
180 if (nv == null && nk.startsWith("env.") && environment != null) {
181 nv = environment.getProperty(nk.substring(4));
182 }
183
184
185
186
187
188
189 if (nv == null || nv.equals(nk)) {
190 ret += "${" + nk + "}";
191 } else {
192 v = nv + v;
193 }
194 }
195 return ret + v;
196 }
197
198 protected boolean exists(String location) {
199 if (StringUtils.isBlank(location)) {
200 return false;
201 }
202 File file = new File(location);
203 if (file.exists()) {
204 return true;
205 }
206 ResourceLoader loader = new DefaultResourceLoader();
207 Resource resource = loader.getResource(location);
208 return resource.exists();
209 }
210
211 protected boolean validate(String location) throws MojoExecutionException {
212 boolean exists = exists(location);
213 if (exists) {
214 return true;
215 }
216 if (quiet) {
217 getLog().info("Ignoring non-existent properties file '" + location + "'");
218 return false;
219 } else {
220 throw new MojoExecutionException("Non-existent properties file '" + location + "'");
221 }
222 }
223
224 protected InputStream getInputStream(String location) throws IOException {
225 File file = new File(location);
226 if (file.exists()) {
227 return new FileInputStream(location);
228 }
229 ResourceLoader loader = new DefaultResourceLoader();
230 Resource resource = loader.getResource(location);
231 return resource.getInputStream();
232 }
233
234 protected Properties getProperties(String location) throws MojoExecutionException {
235 InputStream in = null;
236 try {
237 Properties properties = new Properties();
238 in = getInputStream(location);
239 if (location.toLowerCase().endsWith(".xml")) {
240 properties.loadFromXML(in);
241 } else {
242 properties.load(in);
243 }
244 return properties;
245 } catch (IOException e) {
246 throw new MojoExecutionException("Error reading properties file " + location, e);
247 } finally {
248 IOUtils.closeQuietly(in);
249 }
250 }
251
252 public boolean isQuiet() {
253 return quiet;
254 }
255
256 public void setQuiet(boolean quiet) {
257 this.quiet = quiet;
258 }
259
260 public String getIgnore() {
261 return ignore;
262 }
263
264 public void setIgnore(String ignoreProperties) {
265 this.ignore = ignoreProperties;
266 }
267
268 public MavenProject getProject() {
269 return project;
270 }
271
272 public String[] getLocations() {
273 return locations;
274 }
275
276 public void setLocations(String[] locations) {
277 this.locations = locations;
278 }
279
280 }