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