1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.properties;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.util.Enumeration;
35 import java.util.Properties;
36
37 import org.apache.maven.plugin.AbstractMojo;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.project.MavenProject;
40 import org.codehaus.plexus.util.cli.CommandLineUtils;
41
42
43
44
45
46
47
48
49
50
51 public class ReadPropertiesMojo extends AbstractMojo {
52
53
54
55
56
57 private MavenProject project;
58
59
60
61
62
63
64
65 private File[] files;
66
67
68
69
70
71
72 private boolean quiet;
73
74 public void execute() throws MojoExecutionException {
75 Properties projectProperties = new Properties();
76 for (int i = 0; i < files.length; i++) {
77 File file = files[i];
78
79 if (file.exists()) {
80 try {
81 getLog().debug("Loading property file: " + file);
82
83 FileInputStream stream = new FileInputStream(file);
84 projectProperties = project.getProperties();
85
86 try {
87 String filename = file.getName().toLowerCase();
88 if (filename.endsWith(".xml")) {
89 projectProperties.loadFromXML(stream);
90 } else {
91 projectProperties.load(stream);
92 }
93 } finally {
94 if (stream != null) {
95 stream.close();
96 }
97 }
98 } catch (IOException e) {
99 throw new MojoExecutionException("Error reading properties file " + file.getAbsolutePath(), e);
100 }
101 } else {
102 if (quiet) {
103 getLog().warn("Ignoring missing properties file: " + file.getAbsolutePath());
104 } else {
105 throw new MojoExecutionException("Properties file not found: " + file.getAbsolutePath());
106 }
107 }
108 }
109
110 boolean useEnvVariables = false;
111 for (Enumeration n = projectProperties.propertyNames(); n.hasMoreElements();) {
112 String k = (String) n.nextElement();
113 String p = (String) projectProperties.get(k);
114 if (p.indexOf("${env.") != -1) {
115 useEnvVariables = true;
116 break;
117 }
118 }
119 Properties environment = null;
120 if (useEnvVariables) {
121 try {
122 environment = CommandLineUtils.getSystemEnvVars();
123 } catch (IOException e) {
124 throw new MojoExecutionException("Error getting system envorinment variables: ", e);
125 }
126 }
127 for (Enumeration n = projectProperties.propertyNames(); n.hasMoreElements();) {
128 String k = (String) n.nextElement();
129 projectProperties.setProperty(k, getPropertyValue(k, projectProperties, environment));
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 private String getPropertyValue(String k, Properties p, Properties environment) {
151 String v = p.getProperty(k);
152 String ret = "";
153 int idx, idx2;
154
155 while ((idx = v.indexOf("${")) >= 0) {
156
157 ret += v.substring(0, idx);
158
159
160 v = v.substring(idx + 2);
161
162 idx2 = v.indexOf("}");
163
164
165 if (idx2 < 0) {
166 break;
167 }
168
169
170
171 String nk = v.substring(0, idx2);
172 v = v.substring(idx2 + 1);
173 String nv = p.getProperty(nk);
174
175
176 if (nv == null) {
177 nv = System.getProperty(nk);
178 }
179
180
181 if (nv == null && nk.startsWith("env.") && environment != null) {
182 nv = environment.getProperty(nk.substring(4));
183 }
184
185
186
187
188
189
190 if (nv == null || nv.equals(nk)) {
191 ret += "${" + nk + "}";
192 } else {
193 v = nv + v;
194 }
195 }
196 return ret + v;
197 }
198 }