1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.buildmetadata.maven;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.Properties;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.maven.project.MavenProject;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public final class MavenPropertyHelper
38 {
39
40
41
42
43
44
45
46
47
48
49
50 private static final String PROJECT_PROPERTY_NAME_PREFIX = "project.";
51
52
53
54
55
56
57
58
59 private static final int PROJECT_PROPERTY_NAME_PREFIX_LENGTH =
60 PROJECT_PROPERTY_NAME_PREFIX.length();
61
62
63
64
65
66
67 private final MavenProject project;
68
69
70
71
72
73
74
75
76
77
78 public MavenPropertyHelper(final MavenProject project)
79 {
80 if (project == null)
81 {
82 throw new NullPointerException(
83 "The property helper requires the project reference to be not 'null'.");
84 }
85 this.project = project;
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public String getProperty(final String name)
105 {
106 if (name == null)
107 {
108 throw new NullPointerException(
109 "Name of requested property must not be 'null'");
110 }
111
112 String value = null;
113 if (isProjectProperty(name))
114 {
115 value = getProjectProperty(name);
116 }
117 if (value == null)
118 {
119 value = getPropertiesProperty(name);
120 }
121 return value;
122 }
123
124 private boolean isProjectProperty(final String name)
125 {
126 return name != null && name.length() >= PROJECT_PROPERTY_NAME_PREFIX_LENGTH
127 && name.startsWith(PROJECT_PROPERTY_NAME_PREFIX);
128 }
129
130
131
132
133 private String getProjectProperty(final String name)
134 {
135 final String projectName =
136 name.substring(PROJECT_PROPERTY_NAME_PREFIX_LENGTH);
137 if (PropertyUtils.isReadable(project, projectName))
138 {
139 try
140 {
141 return getProjectProperty(project, projectName);
142 }
143 catch (final Exception e)
144 {
145 throw new IllegalStateException("Cannot access project property '"
146 + name + "'.");
147 }
148 }
149
150 return null;
151 }
152
153 private static String getProjectProperty(final MavenProject project,
154 final String projectName) throws IllegalAccessException,
155 InvocationTargetException, NoSuchMethodException
156 {
157 if (project != null)
158 {
159 final Object value = PropertyUtils.getProperty(project, projectName);
160 if (value != null)
161 {
162 return String.valueOf(value);
163 }
164 else
165 {
166 return getProjectProperty(project.getParent(), projectName);
167 }
168 }
169 return null;
170 }
171
172
173
174
175 private String getPropertiesProperty(final String name)
176 {
177 return getPropertiesProperty(project, name);
178 }
179
180 private static String getPropertiesProperty(final MavenProject project,
181 final String name)
182 {
183 String value = null;
184 final Properties properties = project.getProperties();
185 if (properties != null)
186 {
187 value = properties.getProperty(name);
188 }
189
190 if (value == null)
191 {
192 value = getPropertiesPropertyFromParent(project, name);
193 }
194
195 return value;
196 }
197
198 private static String getPropertiesPropertyFromParent(
199 final MavenProject project, final String name)
200 {
201 final String value;
202 final MavenProject parentProject = project.getParent();
203 if (parentProject != null)
204 {
205 value = getPropertiesProperty(parentProject, name);
206 }
207 else
208 {
209 value = null;
210 }
211 return value;
212 }
213
214
215
216 }