Revision 48406
Added by Marek Horst almost 7 years ago
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/src/main/java/eu/dnetlib/maven/plugin/properties/GenerateOoziePropertiesMojo.java | ||
---|---|---|
1 |
package eu.dnetlib.maven.plugin.properties; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.apache.commons.lang.ArrayUtils; |
|
8 |
import org.apache.commons.lang.StringUtils; |
|
9 |
import org.apache.maven.plugin.AbstractMojo; |
|
10 |
import org.apache.maven.plugin.MojoExecutionException; |
|
11 |
import org.apache.maven.plugin.MojoFailureException; |
|
12 |
|
|
13 |
/** |
|
14 |
* Generates oozie properties which were not provided from commandline. |
|
15 |
* @author mhorst |
|
16 |
* |
|
17 |
* @goal generate-properties |
|
18 |
*/ |
|
19 |
public class GenerateOoziePropertiesMojo extends AbstractMojo { |
|
20 |
|
|
21 |
public static final String PROPERTY_NAME_WF_SOURCE_DIR = "workflow.source.dir"; |
|
22 |
public static final String PROPERTY_NAME_SANDBOX_NAME = "sandboxName"; |
|
23 |
|
|
24 |
private final String[] limiters = {"iis", "dnetlib", "eu"}; |
|
25 |
|
|
26 |
@Override |
|
27 |
public void execute() throws MojoExecutionException, MojoFailureException { |
|
28 |
if (System.getProperties().containsKey(PROPERTY_NAME_WF_SOURCE_DIR) && |
|
29 |
!System.getProperties().containsKey(PROPERTY_NAME_SANDBOX_NAME)) { |
|
30 |
String generatedSandboxName = generateSandboxName(System.getProperties().getProperty( |
|
31 |
PROPERTY_NAME_WF_SOURCE_DIR)); |
|
32 |
if (generatedSandboxName!=null) { |
|
33 |
System.getProperties().setProperty(PROPERTY_NAME_SANDBOX_NAME, |
|
34 |
generatedSandboxName); |
|
35 |
} else { |
|
36 |
System.out.println("unable to generate sandbox name from path: " + |
|
37 |
System.getProperties().getProperty(PROPERTY_NAME_WF_SOURCE_DIR)); |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* Generates sandbox name from workflow source directory. |
|
44 |
* @param wfSourceDir |
|
45 |
* @return generated sandbox name |
|
46 |
*/ |
|
47 |
private String generateSandboxName(String wfSourceDir) { |
|
48 |
// utilize all dir names until finding one of the limiters |
|
49 |
List<String> sandboxNameParts = new ArrayList<String>(); |
|
50 |
String[] tokens = StringUtils.split(wfSourceDir, File.separatorChar); |
|
51 |
ArrayUtils.reverse(tokens); |
|
52 |
if (tokens.length>0) { |
|
53 |
for (String token : tokens) { |
|
54 |
for (String limiter : limiters) { |
|
55 |
if (limiter.equals(token)) { |
|
56 |
return sandboxNameParts.size()>0? |
|
57 |
StringUtils.join(sandboxNameParts.toArray()):null; |
|
58 |
} |
|
59 |
} |
|
60 |
if (sandboxNameParts.size()>0) { |
|
61 |
sandboxNameParts.add(0, File.separator); |
|
62 |
} |
|
63 |
sandboxNameParts.add(0, token); |
|
64 |
} |
|
65 |
if (sandboxNameParts.size()>0) { |
|
66 |
return StringUtils.join(sandboxNameParts.toArray()); |
|
67 |
} else { |
|
68 |
return null; |
|
69 |
} |
|
70 |
} else { |
|
71 |
return null; |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
} |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/src/main/java/eu/dnetlib/maven/plugin/properties/WritePredefinedProjectProperties.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* Licensed under the Educational Community License, Version 2.0 (the "License"); |
|
4 |
* you may not use this file except in compliance with the License. |
|
5 |
* You may obtain a copy of the License at |
|
6 |
* |
|
7 |
* http://www.opensource.org/licenses/ecl2.php |
|
8 |
* |
|
9 |
* Unless required by applicable law or agreed to in writing, software |
|
10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 |
* See the License for the specific language governing permissions and |
|
13 |
* limitations under the License. |
|
14 |
*/ |
|
15 |
package eu.dnetlib.maven.plugin.properties; |
|
16 |
|
|
17 |
import java.io.File; |
|
18 |
import java.io.FileInputStream; |
|
19 |
import java.io.IOException; |
|
20 |
import java.io.InputStream; |
|
21 |
import java.io.OutputStream; |
|
22 |
import java.text.SimpleDateFormat; |
|
23 |
import java.util.ArrayList; |
|
24 |
import java.util.Arrays; |
|
25 |
import java.util.Collections; |
|
26 |
import java.util.Date; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.Properties; |
|
30 |
import java.util.Set; |
|
31 |
|
|
32 |
import org.apache.commons.io.FileUtils; |
|
33 |
import org.apache.commons.io.IOUtils; |
|
34 |
import org.apache.commons.lang.StringUtils; |
|
35 |
import org.apache.maven.plugin.AbstractMojo; |
|
36 |
import org.apache.maven.plugin.MojoExecutionException; |
|
37 |
import org.apache.maven.plugin.MojoFailureException; |
|
38 |
import org.apache.maven.project.MavenProject; |
|
39 |
import org.springframework.core.io.DefaultResourceLoader; |
|
40 |
import org.springframework.core.io.Resource; |
|
41 |
import org.springframework.core.io.ResourceLoader; |
|
42 |
|
|
43 |
/** |
|
44 |
* Writes project properties for the keys listed in specified properties files. |
|
45 |
* Based on: |
|
46 |
* http://site.kuali.org/maven/plugins/properties-maven-plugin/1.3.2/write-project-properties-mojo.html |
|
47 |
|
|
48 |
* @author mhorst |
|
49 |
* @goal write-project-properties |
|
50 |
*/ |
|
51 |
public class WritePredefinedProjectProperties extends AbstractMojo { |
|
52 |
|
|
53 |
private static final String CR = "\r"; |
|
54 |
private static final String LF = "\n"; |
|
55 |
private static final String TAB = "\t"; |
|
56 |
private static final String[] ANT_ESCAPE_CHARS = { CR, LF, TAB, ":", "#", "=" }; |
|
57 |
|
|
58 |
/** |
|
59 |
* @parameter expression="${properties.includePropertyKeysFromFiles}" |
|
60 |
*/ |
|
61 |
private String[] includePropertyKeysFromFiles; |
|
62 |
|
|
63 |
/** |
|
64 |
* @parameter default-value="${project}" |
|
65 |
* @required |
|
66 |
* @readonly |
|
67 |
*/ |
|
68 |
private MavenProject project; |
|
69 |
|
|
70 |
/** |
|
71 |
* The file that properties will be written to |
|
72 |
* |
|
73 |
* @parameter expression="${properties.outputFile}" |
|
74 |
* default-value="${project.build.directory}/properties/project.properties"; |
|
75 |
* @required |
|
76 |
*/ |
|
77 |
private File outputFile; |
|
78 |
|
|
79 |
/** |
|
80 |
* If true, the plugin will silently ignore any non-existent properties files, and the build will continue |
|
81 |
* |
|
82 |
* @parameter expression="${properties.quiet}" default-value="true" |
|
83 |
*/ |
|
84 |
private boolean quiet; |
|
85 |
|
|
86 |
/** |
|
87 |
* Comma separated list of characters to escape when writing property values. cr=carriage return, lf=linefeed, |
|
88 |
* tab=tab. Any other values are taken literally. |
|
89 |
* |
|
90 |
* @parameter default-value="cr,lf,tab" expression="${properties.escapeChars}" |
|
91 |
*/ |
|
92 |
private String escapeChars; |
|
93 |
|
|
94 |
/** |
|
95 |
* If true, the plugin will create the properties file formatted the same way Ant formats properties files using the |
|
96 |
* <code>echoproperties</code> task. This mode adds 3 custom properties at the top of the file, DSTAMP, TODAY, and |
|
97 |
* TSTAMP. In this mode <code>escapeChars</code> is ignored and the 6 characters Ant escapes are used instead |
|
98 |
* <code>CR</code>,<code>LF</code>,<code>TAB</code>,<code>:</code>,<code>#</code>,<code>=</code> |
|
99 |
* |
|
100 |
* @parameter default-value="false" expression="${properties.antEchoPropertiesMode}" |
|
101 |
*/ |
|
102 |
private boolean antEchoPropertiesMode; |
|
103 |
|
|
104 |
/** |
|
105 |
* If true, the plugin will include system properties when writing the properties file. System properties override |
|
106 |
* both environment variables and project properties. |
|
107 |
* |
|
108 |
* @parameter default-value="false" expression="${properties.includeSystemProperties}" |
|
109 |
*/ |
|
110 |
private boolean includeSystemProperties; |
|
111 |
|
|
112 |
/** |
|
113 |
* If true, the plugin will include environment variables when writing the properties file. Environment variables |
|
114 |
* are prefixed with "env". Environment variables override project properties. |
|
115 |
* |
|
116 |
* @parameter default-value="false" expression="${properties.includeEnvironmentVariables}" |
|
117 |
*/ |
|
118 |
private boolean includeEnvironmentVariables; |
|
119 |
|
|
120 |
/** |
|
121 |
* Comma separated set of properties to exclude when writing the properties file |
|
122 |
* |
|
123 |
* @parameter expression="${properties.exclude}" |
|
124 |
*/ |
|
125 |
private String exclude; |
|
126 |
|
|
127 |
/** |
|
128 |
* Comma separated set of properties to write to the properties file. If provided, only the properties matching |
|
129 |
* those supplied here will be written to the properties file. |
|
130 |
* |
|
131 |
* @parameter expression="${properties.include}" |
|
132 |
*/ |
|
133 |
private String include; |
|
134 |
|
|
135 |
/* (non-Javadoc) |
|
136 |
* @see org.apache.maven.plugin.AbstractMojo#execute() |
|
137 |
*/ |
|
138 |
@Override |
|
139 |
public void execute() throws MojoExecutionException, MojoFailureException { |
|
140 |
Properties properties = new Properties(); |
|
141 |
// Add project properties |
|
142 |
properties.putAll(project.getProperties()); |
|
143 |
if (includeEnvironmentVariables) { |
|
144 |
// Add environment variables, overriding any existing properties with the same key |
|
145 |
properties.putAll(getEnvironmentVariables()); |
|
146 |
} |
|
147 |
if (includeSystemProperties) { |
|
148 |
// Add system properties, overriding any existing properties with the same key |
|
149 |
properties.putAll(System.getProperties()); |
|
150 |
} |
|
151 |
|
|
152 |
// Remove properties as appropriate |
|
153 |
trim(properties, exclude, include); |
|
154 |
|
|
155 |
String comment = "# " + new Date() + "\n"; |
|
156 |
List<String> escapeTokens = getEscapeChars(escapeChars); |
|
157 |
if (antEchoPropertiesMode) { |
|
158 |
escapeTokens = Arrays.asList(ANT_ESCAPE_CHARS); |
|
159 |
comment = getAntHeader(); |
|
160 |
properties.remove("DSTAMP"); |
|
161 |
properties.remove("TODAY"); |
|
162 |
properties.remove("TSTAMP"); |
|
163 |
} |
|
164 |
|
|
165 |
getLog().info("Creating " + outputFile); |
|
166 |
writeProperties(outputFile, comment, properties, escapeTokens); |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* Provides environment variables. |
|
171 |
* @return environment variables |
|
172 |
*/ |
|
173 |
protected static Properties getEnvironmentVariables() { |
|
174 |
String prefix = "env"; |
|
175 |
Map<String, String> map = System.getenv(); |
|
176 |
Properties props = new Properties(); |
|
177 |
for (String key : map.keySet()) { |
|
178 |
String newKey = prefix + "." + key; |
|
179 |
String value = map.get(key); |
|
180 |
props.setProperty(newKey, value); |
|
181 |
} |
|
182 |
return props; |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* Removes properties which should not be written. |
|
187 |
* @param properties |
|
188 |
* @param omitCSV |
|
189 |
* @param includeCSV |
|
190 |
* @throws MojoExecutionException |
|
191 |
*/ |
|
192 |
protected void trim(Properties properties, String omitCSV, String includeCSV) throws MojoExecutionException { |
|
193 |
List<String> omitKeys = getListFromCSV(omitCSV); |
|
194 |
for (String key : omitKeys) { |
|
195 |
properties.remove(key); |
|
196 |
} |
|
197 |
|
|
198 |
List<String> includeKeys = getListFromCSV(includeCSV); |
|
199 |
// mh: including keys from predefined properties |
|
200 |
if (includePropertyKeysFromFiles!=null && includePropertyKeysFromFiles.length>0) { |
|
201 |
for (String currentIncludeLoc : includePropertyKeysFromFiles) { |
|
202 |
if (validate(currentIncludeLoc)) { |
|
203 |
Properties p = getProperties(currentIncludeLoc); |
|
204 |
for (String key : p.stringPropertyNames()) { |
|
205 |
includeKeys.add(key); |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
if (includeKeys!=null && !includeKeys.isEmpty()) { |
|
211 |
// removing only when include keys provided |
|
212 |
Set<String> keys = properties.stringPropertyNames(); |
|
213 |
for (String key : keys) { |
|
214 |
if (!includeKeys.contains(key)) { |
|
215 |
properties.remove(key); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
protected String toEmpty(String s) { |
|
222 |
if (StringUtils.isBlank(s)) { |
|
223 |
return ""; |
|
224 |
} else { |
|
225 |
return s; |
|
226 |
} |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* Checks whether file exists. |
|
231 |
* @param location |
|
232 |
* @return true when exists, false otherwise. |
|
233 |
*/ |
|
234 |
protected boolean exists(String location) { |
|
235 |
if (StringUtils.isBlank(location)) { |
|
236 |
return false; |
|
237 |
} |
|
238 |
File file = new File(location); |
|
239 |
if (file.exists()) { |
|
240 |
return true; |
|
241 |
} |
|
242 |
ResourceLoader loader = new DefaultResourceLoader(); |
|
243 |
Resource resource = loader.getResource(location); |
|
244 |
return resource.exists(); |
|
245 |
} |
|
246 |
|
|
247 |
/** |
|
248 |
* Validates resource location. |
|
249 |
* @param location |
|
250 |
* @return true when valid, false otherwise |
|
251 |
* @throws MojoExecutionException |
|
252 |
*/ |
|
253 |
protected boolean validate(String location) throws MojoExecutionException { |
|
254 |
boolean exists = exists(location); |
|
255 |
if (exists) { |
|
256 |
return true; |
|
257 |
} |
|
258 |
if (quiet) { |
|
259 |
getLog().info("Ignoring non-existent properties file '" + toEmpty(location) + "'"); |
|
260 |
return false; |
|
261 |
} else { |
|
262 |
throw new MojoExecutionException("Non-existent properties file '" + location + "'"); |
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
/** |
|
267 |
* Provides input stream. |
|
268 |
* @param location |
|
269 |
* @return input stream |
|
270 |
* @throws IOException |
|
271 |
*/ |
|
272 |
protected InputStream getInputStream(String location) throws IOException { |
|
273 |
File file = new File(location); |
|
274 |
if (file.exists()) { |
|
275 |
return new FileInputStream(location); |
|
276 |
} |
|
277 |
ResourceLoader loader = new DefaultResourceLoader(); |
|
278 |
Resource resource = loader.getResource(location); |
|
279 |
return resource.getInputStream(); |
|
280 |
} |
|
281 |
|
|
282 |
/** |
|
283 |
* Creates properties for given location. |
|
284 |
* @param location |
|
285 |
* @return properties for given location |
|
286 |
* @throws MojoExecutionException |
|
287 |
*/ |
|
288 |
protected Properties getProperties(String location) throws MojoExecutionException { |
|
289 |
InputStream in = null; |
|
290 |
try { |
|
291 |
Properties properties = new Properties(); |
|
292 |
in = getInputStream(location); |
|
293 |
if (location.toLowerCase().endsWith(".xml")) { |
|
294 |
properties.loadFromXML(in); |
|
295 |
} else { |
|
296 |
properties.load(in); |
|
297 |
} |
|
298 |
return properties; |
|
299 |
} catch (IOException e) { |
|
300 |
throw new MojoExecutionException("Error reading properties file " + location, e); |
|
301 |
} finally { |
|
302 |
IOUtils.closeQuietly(in); |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Provides ant header. |
|
308 |
* @return ant header |
|
309 |
*/ |
|
310 |
protected String getAntHeader() { |
|
311 |
SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd"); |
|
312 |
SimpleDateFormat today = new SimpleDateFormat("MMMM d yyyy"); |
|
313 |
SimpleDateFormat tstamp = new SimpleDateFormat("HHmm"); |
|
314 |
Date now = new Date(); |
|
315 |
StringBuilder sb = new StringBuilder(); |
|
316 |
sb.append("# Ant properties\n"); |
|
317 |
sb.append("# " + now + "\n"); |
|
318 |
sb.append("DSTAMP=" + dstamp.format(now) + "\n"); |
|
319 |
sb.append("TODAY=" + today.format(now) + "\n"); |
|
320 |
sb.append("TSTAMP=" + tstamp.format(now) + "\n"); |
|
321 |
return sb.toString(); |
|
322 |
} |
|
323 |
|
|
324 |
/** |
|
325 |
* Provides escape characters. |
|
326 |
* @param escapeChars |
|
327 |
* @return escape characters |
|
328 |
*/ |
|
329 |
protected List<String> getEscapeChars(String escapeChars) { |
|
330 |
List<String> tokens = getListFromCSV(escapeChars); |
|
331 |
List<String> realTokens = new ArrayList<String>(); |
|
332 |
for (String token : tokens) { |
|
333 |
String realToken = getRealToken(token); |
|
334 |
realTokens.add(realToken); |
|
335 |
} |
|
336 |
return realTokens; |
|
337 |
} |
|
338 |
|
|
339 |
/** |
|
340 |
* Provides real token. |
|
341 |
* @param token |
|
342 |
* @return real token |
|
343 |
*/ |
|
344 |
protected String getRealToken(String token) { |
|
345 |
if (token.equalsIgnoreCase("CR")) { |
|
346 |
return CR; |
|
347 |
} else if (token.equalsIgnoreCase("LF")) { |
|
348 |
return LF; |
|
349 |
} else if (token.equalsIgnoreCase("TAB")) { |
|
350 |
return TAB; |
|
351 |
} else { |
|
352 |
return token; |
|
353 |
} |
|
354 |
} |
|
355 |
|
|
356 |
/** |
|
357 |
* Returns content. |
|
358 |
* @param comment |
|
359 |
* @param properties |
|
360 |
* @param escapeTokens |
|
361 |
* @return content |
|
362 |
*/ |
|
363 |
protected String getContent(String comment, Properties properties, List<String> escapeTokens) { |
|
364 |
List<String> names = new ArrayList<String>(properties.stringPropertyNames()); |
|
365 |
Collections.sort(names); |
|
366 |
StringBuilder sb = new StringBuilder(); |
|
367 |
if (!StringUtils.isBlank(comment)) { |
|
368 |
sb.append(comment); |
|
369 |
} |
|
370 |
for (String name : names) { |
|
371 |
String value = properties.getProperty(name); |
|
372 |
String escapedValue = escape(value, escapeTokens); |
|
373 |
sb.append(name + "=" + escapedValue + "\n"); |
|
374 |
} |
|
375 |
return sb.toString(); |
|
376 |
} |
|
377 |
|
|
378 |
/** |
|
379 |
* Writes properties to given file. |
|
380 |
* @param file |
|
381 |
* @param comment |
|
382 |
* @param properties |
|
383 |
* @param escapeTokens |
|
384 |
* @throws MojoExecutionException |
|
385 |
*/ |
|
386 |
protected void writeProperties(File file, String comment, Properties properties, List<String> escapeTokens) |
|
387 |
throws MojoExecutionException { |
|
388 |
try { |
|
389 |
String content = getContent(comment, properties, escapeTokens); |
|
390 |
FileUtils.writeStringToFile(file, content); |
|
391 |
} catch (IOException e) { |
|
392 |
throw new MojoExecutionException("Error creating properties file", e); |
|
393 |
} |
|
394 |
} |
|
395 |
|
|
396 |
/** |
|
397 |
* Writes properties. |
|
398 |
* @param properties |
|
399 |
* @param file |
|
400 |
* @throws MojoExecutionException |
|
401 |
*/ |
|
402 |
protected void writeProperties(Properties properties, File file) throws MojoExecutionException { |
|
403 |
OutputStream out = null; |
|
404 |
try { |
|
405 |
out = FileUtils.openOutputStream(outputFile); |
|
406 |
properties.store(out, "Properties"); |
|
407 |
} catch (IOException e) { |
|
408 |
throw new MojoExecutionException("Error creating properties file", e); |
|
409 |
} finally { |
|
410 |
IOUtils.closeQuietly(out); |
|
411 |
} |
|
412 |
} |
|
413 |
|
|
414 |
/** |
|
415 |
* Escapes characters. |
|
416 |
* @param s |
|
417 |
* @param escapeChars |
|
418 |
* @return |
|
419 |
*/ |
|
420 |
protected String escape(String s, List<String> escapeChars) { |
|
421 |
String result = s; |
|
422 |
for (String escapeChar : escapeChars) { |
|
423 |
result = result.replace(escapeChar, getReplacementToken(escapeChar)); |
|
424 |
} |
|
425 |
return result; |
|
426 |
} |
|
427 |
|
|
428 |
/** |
|
429 |
* Provides replacement token. |
|
430 |
* @param escapeChar |
|
431 |
* @return replacement token |
|
432 |
*/ |
|
433 |
protected String getReplacementToken(String escapeChar) { |
|
434 |
if (escapeChar.equals(CR)) { |
|
435 |
return "\\r"; |
|
436 |
} else if (escapeChar.equals(LF)) { |
|
437 |
return "\\n"; |
|
438 |
} else if (escapeChar.equals(TAB)) { |
|
439 |
return "\\t"; |
|
440 |
} else { |
|
441 |
return "\\" + escapeChar; |
|
442 |
} |
|
443 |
} |
|
444 |
|
|
445 |
/** |
|
446 |
* Returns list from csv. |
|
447 |
* @param csv |
|
448 |
* @return list of values generated from CSV |
|
449 |
*/ |
|
450 |
protected static final List<String> getListFromCSV(String csv) { |
|
451 |
if (StringUtils.isBlank(csv)) { |
|
452 |
return new ArrayList<String>(); |
|
453 |
} |
|
454 |
List<String> list = new ArrayList<String>(); |
|
455 |
String[] tokens = StringUtils.split(csv, ","); |
|
456 |
for (String token : tokens) { |
|
457 |
list.add(token.trim()); |
|
458 |
} |
|
459 |
return list; |
|
460 |
} |
|
461 |
|
|
462 |
public boolean isAntEchoPropertiesMode() { |
|
463 |
return antEchoPropertiesMode; |
|
464 |
} |
|
465 |
|
|
466 |
public void setAntEchoPropertiesMode(boolean antEchoPropertiesMode) { |
|
467 |
this.antEchoPropertiesMode = antEchoPropertiesMode; |
|
468 |
} |
|
469 |
|
|
470 |
public boolean isIncludeSystemProperties() { |
|
471 |
return includeSystemProperties; |
|
472 |
} |
|
473 |
|
|
474 |
public void setIncludeSystemProperties(boolean includeSystemProperties) { |
|
475 |
this.includeSystemProperties = includeSystemProperties; |
|
476 |
} |
|
477 |
|
|
478 |
public String getEscapeChars() { |
|
479 |
return escapeChars; |
|
480 |
} |
|
481 |
|
|
482 |
public void setEscapeChars(String escapeChars) { |
|
483 |
this.escapeChars = escapeChars; |
|
484 |
} |
|
485 |
|
|
486 |
public boolean isIncludeEnvironmentVariables() { |
|
487 |
return includeEnvironmentVariables; |
|
488 |
} |
|
489 |
|
|
490 |
public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) { |
|
491 |
this.includeEnvironmentVariables = includeEnvironmentVariables; |
|
492 |
} |
|
493 |
|
|
494 |
public String getExclude() { |
|
495 |
return exclude; |
|
496 |
} |
|
497 |
|
|
498 |
public void setExclude(String exclude) { |
|
499 |
this.exclude = exclude; |
|
500 |
} |
|
501 |
|
|
502 |
public String getInclude() { |
|
503 |
return include; |
|
504 |
} |
|
505 |
|
|
506 |
public void setInclude(String include) { |
|
507 |
this.include = include; |
|
508 |
} |
|
509 |
|
|
510 |
public boolean isQuiet() { |
|
511 |
return quiet; |
|
512 |
} |
|
513 |
|
|
514 |
public void setQuiet(boolean quiet) { |
|
515 |
this.quiet = quiet; |
|
516 |
} |
|
517 |
|
|
518 |
/** |
|
519 |
* Sets property files for which keys properties should be included. |
|
520 |
* @param includePropertyKeysFromFiles |
|
521 |
*/ |
|
522 |
public void setIncludePropertyKeysFromFiles( |
|
523 |
String[] includePropertyKeysFromFiles) { |
|
524 |
if (includePropertyKeysFromFiles!=null) { |
|
525 |
this.includePropertyKeysFromFiles = Arrays.copyOf( |
|
526 |
includePropertyKeysFromFiles, |
|
527 |
includePropertyKeysFromFiles.length); |
|
528 |
} |
|
529 |
} |
|
530 |
} |
|
531 | 0 |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
|
3 |
|
|
4 |
<groupId>eu.dnetlib</groupId> |
|
5 |
<version>0.0.2</version> |
|
6 |
<modelVersion>4.0.0</modelVersion> |
|
7 |
<artifactId>icm-iis-properties-maven-plugin</artifactId> |
|
8 |
<packaging>maven-plugin</packaging> |
|
9 |
|
|
10 |
<scm> |
|
11 |
<developerConnection> |
|
12 |
scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2 |
|
13 |
</developerConnection> |
|
14 |
</scm> |
|
15 |
|
|
16 |
<properties> |
|
17 |
</properties> |
|
18 |
|
|
19 |
<repositories> |
|
20 |
<repository> |
|
21 |
<id>dnet-deps</id> |
|
22 |
<name>dnet-dependencies</name> |
|
23 |
<url>http://maven.research-infrastructures.eu/nexus/content/repositories/dnet-deps</url> |
|
24 |
<layout>default</layout> |
|
25 |
</repository> |
|
26 |
</repositories> |
|
27 |
|
|
28 |
<dependencies> |
|
29 |
<dependency> |
|
30 |
<groupId>org.apache.maven</groupId> |
|
31 |
<artifactId>maven-plugin-api</artifactId> |
|
32 |
<version>2.0</version> |
|
33 |
</dependency> |
|
34 |
<dependency> |
|
35 |
<groupId>org.apache.maven</groupId> |
|
36 |
<artifactId>maven-project</artifactId> |
|
37 |
<version>2.0</version> |
|
38 |
</dependency> |
|
39 |
<dependency> |
|
40 |
<groupId>org.kuali.maven.plugins</groupId> |
|
41 |
<artifactId>properties-maven-plugin</artifactId> |
|
42 |
<version>1.3.2</version> |
|
43 |
</dependency> |
|
44 |
</dependencies> |
|
45 |
<build> |
|
46 |
<directory>target</directory> |
|
47 |
<outputDirectory>target/classes</outputDirectory> |
|
48 |
<finalName>${project.artifactId}-${project.version}</finalName> |
|
49 |
<testOutputDirectory>target/test-classes</testOutputDirectory> |
|
50 |
<plugins> |
|
51 |
<plugin> |
|
52 |
<groupId>org.apache.maven.plugins</groupId> |
|
53 |
<artifactId>maven-compiler-plugin</artifactId> |
|
54 |
<version>2.3.2</version> |
|
55 |
<configuration> |
|
56 |
<source>1.6</source> |
|
57 |
<target>1.6</target> |
|
58 |
</configuration> |
|
59 |
</plugin> |
|
60 |
<plugin> |
|
61 |
<groupId>org.apache.maven.plugins</groupId> |
|
62 |
<artifactId>maven-source-plugin</artifactId> |
|
63 |
<version>2.1.2</version> |
|
64 |
<executions> |
|
65 |
<execution> |
|
66 |
<id>attach-sources</id> |
|
67 |
<phase>verify</phase> |
|
68 |
<goals> |
|
69 |
<goal>jar-no-fork</goal> |
|
70 |
</goals> |
|
71 |
</execution> |
|
72 |
</executions> |
|
73 |
</plugin> |
|
74 |
<plugin> |
|
75 |
<groupId>org.apache.maven.plugins</groupId> |
|
76 |
<artifactId>maven-surefire-plugin</artifactId> |
|
77 |
<version>2.4.3</version> |
|
78 |
<configuration> |
|
79 |
<redirectTestOutputToFile>true</redirectTestOutputToFile> |
|
80 |
<includes> |
|
81 |
<include>**/*Test.java</include> |
|
82 |
</includes> |
|
83 |
<excludes> |
|
84 |
<exclude>**/AllTests.java</exclude> |
|
85 |
<exclude>**/Abstract*Test.java</exclude> |
|
86 |
</excludes> |
|
87 |
</configuration> |
|
88 |
</plugin> |
|
89 |
<plugin> |
|
90 |
<groupId>org.apache.maven.plugins</groupId> |
|
91 |
<artifactId>maven-javadoc-plugin</artifactId> |
|
92 |
<version>2.9</version> |
|
93 |
<configuration> |
|
94 |
<detectLinks>true</detectLinks> |
|
95 |
</configuration> |
|
96 |
</plugin> |
|
97 |
</plugins> |
|
98 |
</build> |
|
99 |
|
|
100 |
<distributionManagement> |
|
101 |
<repository> |
|
102 |
<id>dnet45-bootstrap-release</id> |
|
103 |
<url> |
|
104 |
http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-bootstrap-release |
|
105 |
</url> |
|
106 |
</repository> |
|
107 |
</distributionManagement> |
|
108 |
</project> |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/README.markdown | ||
---|---|---|
1 |
Maven plugin module utilized by `icm-iis-parent-container` for proper `job.properties` file building. |
|
2 |
Based on: |
|
3 |
http://site.kuali.org/maven/plugins/properties-maven-plugin/1.3.2/write-project-properties-mojo.html |
|
4 |
Supplemented with: |
|
5 |
* handling includePropertyKeysFromFiles property allowing writing only properties listed in given property files |
|
6 |
As a final outcome only properties listed in <include> element and listed as a keys in files from <includePropertyKeysFromFiles> element will be written to output file. |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/trunk/src/main/java/eu/dnetlib/maven/plugin/properties/GenerateOoziePropertiesMojo.java | ||
---|---|---|
1 |
package eu.dnetlib.maven.plugin.properties; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.apache.commons.lang.ArrayUtils; |
|
8 |
import org.apache.commons.lang.StringUtils; |
|
9 |
import org.apache.maven.plugin.AbstractMojo; |
|
10 |
import org.apache.maven.plugin.MojoExecutionException; |
|
11 |
import org.apache.maven.plugin.MojoFailureException; |
|
12 |
|
|
13 |
/** |
|
14 |
* Generates oozie properties which were not provided from commandline. |
|
15 |
* @author mhorst |
|
16 |
* |
|
17 |
* @goal generate-properties |
|
18 |
*/ |
|
19 |
public class GenerateOoziePropertiesMojo extends AbstractMojo { |
|
20 |
|
|
21 |
public static final String PROPERTY_NAME_WF_SOURCE_DIR = "workflow.source.dir"; |
|
22 |
public static final String PROPERTY_NAME_SANDBOX_NAME = "sandboxName"; |
|
23 |
|
|
24 |
private final String[] limiters = {"iis", "dnetlib", "eu"}; |
|
25 |
|
|
26 |
@Override |
|
27 |
public void execute() throws MojoExecutionException, MojoFailureException { |
|
28 |
if (System.getProperties().containsKey(PROPERTY_NAME_WF_SOURCE_DIR) && |
|
29 |
!System.getProperties().containsKey(PROPERTY_NAME_SANDBOX_NAME)) { |
|
30 |
String generatedSandboxName = generateSandboxName(System.getProperties().getProperty( |
|
31 |
PROPERTY_NAME_WF_SOURCE_DIR)); |
|
32 |
if (generatedSandboxName!=null) { |
|
33 |
System.getProperties().setProperty(PROPERTY_NAME_SANDBOX_NAME, |
|
34 |
generatedSandboxName); |
|
35 |
} else { |
|
36 |
System.out.println("unable to generate sandbox name from path: " + |
|
37 |
System.getProperties().getProperty(PROPERTY_NAME_WF_SOURCE_DIR)); |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* Generates sandbox name from workflow source directory. |
|
44 |
* @param wfSourceDir |
|
45 |
* @return generated sandbox name |
|
46 |
*/ |
|
47 |
private String generateSandboxName(String wfSourceDir) { |
|
48 |
// utilize all dir names until finding one of the limiters |
|
49 |
List<String> sandboxNameParts = new ArrayList<String>(); |
|
50 |
String[] tokens = StringUtils.split(wfSourceDir, File.separatorChar); |
|
51 |
ArrayUtils.reverse(tokens); |
|
52 |
if (tokens.length>0) { |
|
53 |
for (String token : tokens) { |
|
54 |
for (String limiter : limiters) { |
|
55 |
if (limiter.equals(token)) { |
|
56 |
return sandboxNameParts.size()>0? |
|
57 |
StringUtils.join(sandboxNameParts.toArray()):null; |
|
58 |
} |
|
59 |
} |
|
60 |
if (sandboxNameParts.size()>0) { |
|
61 |
sandboxNameParts.add(0, File.separator); |
|
62 |
} |
|
63 |
sandboxNameParts.add(0, token); |
|
64 |
} |
|
65 |
if (sandboxNameParts.size()>0) { |
|
66 |
return StringUtils.join(sandboxNameParts.toArray()); |
|
67 |
} else { |
|
68 |
return null; |
|
69 |
} |
|
70 |
} else { |
|
71 |
return null; |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
} |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/trunk/src/main/java/eu/dnetlib/maven/plugin/properties/WritePredefinedProjectProperties.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* Licensed under the Educational Community License, Version 2.0 (the "License"); |
|
4 |
* you may not use this file except in compliance with the License. |
|
5 |
* You may obtain a copy of the License at |
|
6 |
* |
|
7 |
* http://www.opensource.org/licenses/ecl2.php |
|
8 |
* |
|
9 |
* Unless required by applicable law or agreed to in writing, software |
|
10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 |
* See the License for the specific language governing permissions and |
|
13 |
* limitations under the License. |
|
14 |
*/ |
|
15 |
package eu.dnetlib.maven.plugin.properties; |
|
16 |
|
|
17 |
import java.io.File; |
|
18 |
import java.io.FileInputStream; |
|
19 |
import java.io.IOException; |
|
20 |
import java.io.InputStream; |
|
21 |
import java.io.OutputStream; |
|
22 |
import java.text.SimpleDateFormat; |
|
23 |
import java.util.ArrayList; |
|
24 |
import java.util.Arrays; |
|
25 |
import java.util.Collections; |
|
26 |
import java.util.Date; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.Properties; |
|
30 |
import java.util.Set; |
|
31 |
|
|
32 |
import org.apache.commons.io.FileUtils; |
|
33 |
import org.apache.commons.io.IOUtils; |
|
34 |
import org.apache.commons.lang.StringUtils; |
|
35 |
import org.apache.maven.plugin.AbstractMojo; |
|
36 |
import org.apache.maven.plugin.MojoExecutionException; |
|
37 |
import org.apache.maven.plugin.MojoFailureException; |
|
38 |
import org.apache.maven.project.MavenProject; |
|
39 |
import org.springframework.core.io.DefaultResourceLoader; |
|
40 |
import org.springframework.core.io.Resource; |
|
41 |
import org.springframework.core.io.ResourceLoader; |
|
42 |
|
|
43 |
/** |
|
44 |
* Writes project properties for the keys listed in specified properties files. |
|
45 |
* Based on: |
|
46 |
* http://site.kuali.org/maven/plugins/properties-maven-plugin/1.3.2/write-project-properties-mojo.html |
|
47 |
|
|
48 |
* @author mhorst |
|
49 |
* @goal write-project-properties |
|
50 |
*/ |
|
51 |
public class WritePredefinedProjectProperties extends AbstractMojo { |
|
52 |
|
|
53 |
private static final String CR = "\r"; |
|
54 |
private static final String LF = "\n"; |
|
55 |
private static final String TAB = "\t"; |
|
56 |
private static final String[] ANT_ESCAPE_CHARS = { CR, LF, TAB, ":", "#", "=" }; |
|
57 |
|
|
58 |
/** |
|
59 |
* @parameter expression="${properties.includePropertyKeysFromFiles}" |
|
60 |
*/ |
|
61 |
private String[] includePropertyKeysFromFiles; |
|
62 |
|
|
63 |
/** |
|
64 |
* @parameter default-value="${project}" |
|
65 |
* @required |
|
66 |
* @readonly |
|
67 |
*/ |
|
68 |
private MavenProject project; |
|
69 |
|
|
70 |
/** |
|
71 |
* The file that properties will be written to |
|
72 |
* |
|
73 |
* @parameter expression="${properties.outputFile}" |
|
74 |
* default-value="${project.build.directory}/properties/project.properties"; |
|
75 |
* @required |
|
76 |
*/ |
|
77 |
private File outputFile; |
|
78 |
|
|
79 |
/** |
|
80 |
* If true, the plugin will silently ignore any non-existent properties files, and the build will continue |
|
81 |
* |
|
82 |
* @parameter expression="${properties.quiet}" default-value="true" |
|
83 |
*/ |
|
84 |
private boolean quiet; |
|
85 |
|
|
86 |
/** |
|
87 |
* Comma separated list of characters to escape when writing property values. cr=carriage return, lf=linefeed, |
|
88 |
* tab=tab. Any other values are taken literally. |
|
89 |
* |
|
90 |
* @parameter default-value="cr,lf,tab" expression="${properties.escapeChars}" |
|
91 |
*/ |
|
92 |
private String escapeChars; |
|
93 |
|
|
94 |
/** |
|
95 |
* If true, the plugin will create the properties file formatted the same way Ant formats properties files using the |
|
96 |
* <code>echoproperties</code> task. This mode adds 3 custom properties at the top of the file, DSTAMP, TODAY, and |
|
97 |
* TSTAMP. In this mode <code>escapeChars</code> is ignored and the 6 characters Ant escapes are used instead |
|
98 |
* <code>CR</code>,<code>LF</code>,<code>TAB</code>,<code>:</code>,<code>#</code>,<code>=</code> |
|
99 |
* |
|
100 |
* @parameter default-value="false" expression="${properties.antEchoPropertiesMode}" |
|
101 |
*/ |
|
102 |
private boolean antEchoPropertiesMode; |
|
103 |
|
|
104 |
/** |
|
105 |
* If true, the plugin will include system properties when writing the properties file. System properties override |
|
106 |
* both environment variables and project properties. |
|
107 |
* |
|
108 |
* @parameter default-value="false" expression="${properties.includeSystemProperties}" |
|
109 |
*/ |
|
110 |
private boolean includeSystemProperties; |
|
111 |
|
|
112 |
/** |
|
113 |
* If true, the plugin will include environment variables when writing the properties file. Environment variables |
|
114 |
* are prefixed with "env". Environment variables override project properties. |
|
115 |
* |
|
116 |
* @parameter default-value="false" expression="${properties.includeEnvironmentVariables}" |
|
117 |
*/ |
|
118 |
private boolean includeEnvironmentVariables; |
|
119 |
|
|
120 |
/** |
|
121 |
* Comma separated set of properties to exclude when writing the properties file |
|
122 |
* |
|
123 |
* @parameter expression="${properties.exclude}" |
|
124 |
*/ |
|
125 |
private String exclude; |
|
126 |
|
|
127 |
/** |
|
128 |
* Comma separated set of properties to write to the properties file. If provided, only the properties matching |
|
129 |
* those supplied here will be written to the properties file. |
|
130 |
* |
|
131 |
* @parameter expression="${properties.include}" |
|
132 |
*/ |
|
133 |
private String include; |
|
134 |
|
|
135 |
/* (non-Javadoc) |
|
136 |
* @see org.apache.maven.plugin.AbstractMojo#execute() |
|
137 |
*/ |
|
138 |
@Override |
|
139 |
public void execute() throws MojoExecutionException, MojoFailureException { |
|
140 |
Properties properties = new Properties(); |
|
141 |
// Add project properties |
|
142 |
properties.putAll(project.getProperties()); |
|
143 |
if (includeEnvironmentVariables) { |
|
144 |
// Add environment variables, overriding any existing properties with the same key |
|
145 |
properties.putAll(getEnvironmentVariables()); |
|
146 |
} |
|
147 |
if (includeSystemProperties) { |
|
148 |
// Add system properties, overriding any existing properties with the same key |
|
149 |
properties.putAll(System.getProperties()); |
|
150 |
} |
|
151 |
|
|
152 |
// Remove properties as appropriate |
|
153 |
trim(properties, exclude, include); |
|
154 |
|
|
155 |
String comment = "# " + new Date() + "\n"; |
|
156 |
List<String> escapeTokens = getEscapeChars(escapeChars); |
|
157 |
if (antEchoPropertiesMode) { |
|
158 |
escapeTokens = Arrays.asList(ANT_ESCAPE_CHARS); |
|
159 |
comment = getAntHeader(); |
|
160 |
properties.remove("DSTAMP"); |
|
161 |
properties.remove("TODAY"); |
|
162 |
properties.remove("TSTAMP"); |
|
163 |
} |
|
164 |
|
|
165 |
getLog().info("Creating " + outputFile); |
|
166 |
writeProperties(outputFile, comment, properties, escapeTokens); |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* Provides environment variables. |
|
171 |
* @return environment variables |
|
172 |
*/ |
|
173 |
protected static Properties getEnvironmentVariables() { |
|
174 |
String prefix = "env"; |
|
175 |
Map<String, String> map = System.getenv(); |
|
176 |
Properties props = new Properties(); |
|
177 |
for (String key : map.keySet()) { |
|
178 |
String newKey = prefix + "." + key; |
|
179 |
String value = map.get(key); |
|
180 |
props.setProperty(newKey, value); |
|
181 |
} |
|
182 |
return props; |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* Removes properties which should not be written. |
|
187 |
* @param properties |
|
188 |
* @param omitCSV |
|
189 |
* @param includeCSV |
|
190 |
* @throws MojoExecutionException |
|
191 |
*/ |
|
192 |
protected void trim(Properties properties, String omitCSV, String includeCSV) throws MojoExecutionException { |
|
193 |
List<String> omitKeys = getListFromCSV(omitCSV); |
|
194 |
for (String key : omitKeys) { |
|
195 |
properties.remove(key); |
|
196 |
} |
|
197 |
|
|
198 |
List<String> includeKeys = getListFromCSV(includeCSV); |
|
199 |
// mh: including keys from predefined properties |
|
200 |
if (includePropertyKeysFromFiles!=null && includePropertyKeysFromFiles.length>0) { |
|
201 |
for (String currentIncludeLoc : includePropertyKeysFromFiles) { |
|
202 |
if (validate(currentIncludeLoc)) { |
|
203 |
Properties p = getProperties(currentIncludeLoc); |
|
204 |
for (String key : p.stringPropertyNames()) { |
|
205 |
includeKeys.add(key); |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
if (includeKeys!=null && !includeKeys.isEmpty()) { |
|
211 |
// removing only when include keys provided |
|
212 |
Set<String> keys = properties.stringPropertyNames(); |
|
213 |
for (String key : keys) { |
|
214 |
if (!includeKeys.contains(key)) { |
|
215 |
properties.remove(key); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
protected String toEmpty(String s) { |
|
222 |
if (StringUtils.isBlank(s)) { |
|
223 |
return ""; |
|
224 |
} else { |
|
225 |
return s; |
|
226 |
} |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* Checks whether file exists. |
|
231 |
* @param location |
|
232 |
* @return true when exists, false otherwise. |
|
233 |
*/ |
|
234 |
protected boolean exists(String location) { |
|
235 |
if (StringUtils.isBlank(location)) { |
|
236 |
return false; |
|
237 |
} |
|
238 |
File file = new File(location); |
|
239 |
if (file.exists()) { |
|
240 |
return true; |
|
241 |
} |
|
242 |
ResourceLoader loader = new DefaultResourceLoader(); |
|
243 |
Resource resource = loader.getResource(location); |
|
244 |
return resource.exists(); |
|
245 |
} |
|
246 |
|
|
247 |
/** |
|
248 |
* Validates resource location. |
|
249 |
* @param location |
|
250 |
* @return true when valid, false otherwise |
|
251 |
* @throws MojoExecutionException |
|
252 |
*/ |
|
253 |
protected boolean validate(String location) throws MojoExecutionException { |
|
254 |
boolean exists = exists(location); |
|
255 |
if (exists) { |
|
256 |
return true; |
|
257 |
} |
|
258 |
if (quiet) { |
|
259 |
getLog().info("Ignoring non-existent properties file '" + toEmpty(location) + "'"); |
|
260 |
return false; |
|
261 |
} else { |
|
262 |
throw new MojoExecutionException("Non-existent properties file '" + location + "'"); |
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
/** |
|
267 |
* Provides input stream. |
|
268 |
* @param location |
|
269 |
* @return input stream |
|
270 |
* @throws IOException |
|
271 |
*/ |
|
272 |
protected InputStream getInputStream(String location) throws IOException { |
|
273 |
File file = new File(location); |
|
274 |
if (file.exists()) { |
|
275 |
return new FileInputStream(location); |
|
276 |
} |
|
277 |
ResourceLoader loader = new DefaultResourceLoader(); |
|
278 |
Resource resource = loader.getResource(location); |
|
279 |
return resource.getInputStream(); |
|
280 |
} |
|
281 |
|
|
282 |
/** |
|
283 |
* Creates properties for given location. |
|
284 |
* @param location |
|
285 |
* @return properties for given location |
|
286 |
* @throws MojoExecutionException |
|
287 |
*/ |
|
288 |
protected Properties getProperties(String location) throws MojoExecutionException { |
|
289 |
InputStream in = null; |
|
290 |
try { |
|
291 |
Properties properties = new Properties(); |
|
292 |
in = getInputStream(location); |
|
293 |
if (location.toLowerCase().endsWith(".xml")) { |
|
294 |
properties.loadFromXML(in); |
|
295 |
} else { |
|
296 |
properties.load(in); |
|
297 |
} |
|
298 |
return properties; |
|
299 |
} catch (IOException e) { |
|
300 |
throw new MojoExecutionException("Error reading properties file " + location, e); |
|
301 |
} finally { |
|
302 |
IOUtils.closeQuietly(in); |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Provides ant header. |
|
308 |
* @return ant header |
|
309 |
*/ |
|
310 |
protected String getAntHeader() { |
|
311 |
SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd"); |
|
312 |
SimpleDateFormat today = new SimpleDateFormat("MMMM d yyyy"); |
|
313 |
SimpleDateFormat tstamp = new SimpleDateFormat("HHmm"); |
|
314 |
Date now = new Date(); |
|
315 |
StringBuilder sb = new StringBuilder(); |
|
316 |
sb.append("# Ant properties\n"); |
|
317 |
sb.append("# " + now + "\n"); |
|
318 |
sb.append("DSTAMP=" + dstamp.format(now) + "\n"); |
|
319 |
sb.append("TODAY=" + today.format(now) + "\n"); |
|
320 |
sb.append("TSTAMP=" + tstamp.format(now) + "\n"); |
|
321 |
return sb.toString(); |
|
322 |
} |
|
323 |
|
|
324 |
/** |
|
325 |
* Provides escape characters. |
|
326 |
* @param escapeChars |
|
327 |
* @return escape characters |
|
328 |
*/ |
|
329 |
protected List<String> getEscapeChars(String escapeChars) { |
|
330 |
List<String> tokens = getListFromCSV(escapeChars); |
|
331 |
List<String> realTokens = new ArrayList<String>(); |
|
332 |
for (String token : tokens) { |
|
333 |
String realToken = getRealToken(token); |
|
334 |
realTokens.add(realToken); |
|
335 |
} |
|
336 |
return realTokens; |
|
337 |
} |
|
338 |
|
|
339 |
/** |
|
340 |
* Provides real token. |
|
341 |
* @param token |
|
342 |
* @return real token |
|
343 |
*/ |
|
344 |
protected String getRealToken(String token) { |
|
345 |
if (token.equalsIgnoreCase("CR")) { |
|
346 |
return CR; |
|
347 |
} else if (token.equalsIgnoreCase("LF")) { |
|
348 |
return LF; |
|
349 |
} else if (token.equalsIgnoreCase("TAB")) { |
|
350 |
return TAB; |
|
351 |
} else { |
|
352 |
return token; |
|
353 |
} |
|
354 |
} |
|
355 |
|
|
356 |
/** |
|
357 |
* Returns content. |
|
358 |
* @param comment |
|
359 |
* @param properties |
|
360 |
* @param escapeTokens |
|
361 |
* @return content |
|
362 |
*/ |
|
363 |
protected String getContent(String comment, Properties properties, List<String> escapeTokens) { |
|
364 |
List<String> names = new ArrayList<String>(properties.stringPropertyNames()); |
|
365 |
Collections.sort(names); |
|
366 |
StringBuilder sb = new StringBuilder(); |
|
367 |
if (!StringUtils.isBlank(comment)) { |
|
368 |
sb.append(comment); |
|
369 |
} |
|
370 |
for (String name : names) { |
|
371 |
String value = properties.getProperty(name); |
|
372 |
String escapedValue = escape(value, escapeTokens); |
|
373 |
sb.append(name + "=" + escapedValue + "\n"); |
|
374 |
} |
|
375 |
return sb.toString(); |
|
376 |
} |
|
377 |
|
|
378 |
/** |
|
379 |
* Writes properties to given file. |
|
380 |
* @param file |
|
381 |
* @param comment |
|
382 |
* @param properties |
|
383 |
* @param escapeTokens |
|
384 |
* @throws MojoExecutionException |
|
385 |
*/ |
|
386 |
protected void writeProperties(File file, String comment, Properties properties, List<String> escapeTokens) |
|
387 |
throws MojoExecutionException { |
|
388 |
try { |
|
389 |
String content = getContent(comment, properties, escapeTokens); |
|
390 |
FileUtils.writeStringToFile(file, content); |
|
391 |
} catch (IOException e) { |
|
392 |
throw new MojoExecutionException("Error creating properties file", e); |
|
393 |
} |
|
394 |
} |
|
395 |
|
|
396 |
/** |
|
397 |
* Writes properties. |
|
398 |
* @param properties |
|
399 |
* @param file |
|
400 |
* @throws MojoExecutionException |
|
401 |
*/ |
|
402 |
protected void writeProperties(Properties properties, File file) throws MojoExecutionException { |
|
403 |
OutputStream out = null; |
|
404 |
try { |
|
405 |
out = FileUtils.openOutputStream(outputFile); |
|
406 |
properties.store(out, "Properties"); |
|
407 |
} catch (IOException e) { |
|
408 |
throw new MojoExecutionException("Error creating properties file", e); |
|
409 |
} finally { |
|
410 |
IOUtils.closeQuietly(out); |
|
411 |
} |
|
412 |
} |
|
413 |
|
|
414 |
/** |
|
415 |
* Escapes characters. |
|
416 |
* @param s |
|
417 |
* @param escapeChars |
|
418 |
* @return |
|
419 |
*/ |
|
420 |
protected String escape(String s, List<String> escapeChars) { |
|
421 |
String result = s; |
|
422 |
for (String escapeChar : escapeChars) { |
|
423 |
result = result.replace(escapeChar, getReplacementToken(escapeChar)); |
|
424 |
} |
|
425 |
return result; |
|
426 |
} |
|
427 |
|
|
428 |
/** |
|
429 |
* Provides replacement token. |
|
430 |
* @param escapeChar |
|
431 |
* @return replacement token |
|
432 |
*/ |
|
433 |
protected String getReplacementToken(String escapeChar) { |
|
434 |
if (escapeChar.equals(CR)) { |
|
435 |
return "\\r"; |
|
436 |
} else if (escapeChar.equals(LF)) { |
|
437 |
return "\\n"; |
|
438 |
} else if (escapeChar.equals(TAB)) { |
|
439 |
return "\\t"; |
|
440 |
} else { |
|
441 |
return "\\" + escapeChar; |
|
442 |
} |
|
443 |
} |
|
444 |
|
|
445 |
/** |
|
446 |
* Returns list from csv. |
|
447 |
* @param csv |
|
448 |
* @return list of values generated from CSV |
|
449 |
*/ |
|
450 |
protected static final List<String> getListFromCSV(String csv) { |
|
451 |
if (StringUtils.isBlank(csv)) { |
|
452 |
return new ArrayList<String>(); |
|
453 |
} |
|
454 |
List<String> list = new ArrayList<String>(); |
|
455 |
String[] tokens = StringUtils.split(csv, ","); |
|
456 |
for (String token : tokens) { |
|
457 |
list.add(token.trim()); |
|
458 |
} |
|
459 |
return list; |
|
460 |
} |
|
461 |
|
|
462 |
public boolean isAntEchoPropertiesMode() { |
|
463 |
return antEchoPropertiesMode; |
|
464 |
} |
|
465 |
|
|
466 |
public void setAntEchoPropertiesMode(boolean antEchoPropertiesMode) { |
|
467 |
this.antEchoPropertiesMode = antEchoPropertiesMode; |
|
468 |
} |
|
469 |
|
|
470 |
public boolean isIncludeSystemProperties() { |
|
471 |
return includeSystemProperties; |
|
472 |
} |
|
473 |
|
|
474 |
public void setIncludeSystemProperties(boolean includeSystemProperties) { |
|
475 |
this.includeSystemProperties = includeSystemProperties; |
|
476 |
} |
|
477 |
|
|
478 |
public String getEscapeChars() { |
|
479 |
return escapeChars; |
|
480 |
} |
|
481 |
|
|
482 |
public void setEscapeChars(String escapeChars) { |
|
483 |
this.escapeChars = escapeChars; |
|
484 |
} |
|
485 |
|
|
486 |
public boolean isIncludeEnvironmentVariables() { |
|
487 |
return includeEnvironmentVariables; |
|
488 |
} |
|
489 |
|
|
490 |
public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) { |
|
491 |
this.includeEnvironmentVariables = includeEnvironmentVariables; |
|
492 |
} |
|
493 |
|
|
494 |
public String getExclude() { |
|
495 |
return exclude; |
|
496 |
} |
|
497 |
|
|
498 |
public void setExclude(String exclude) { |
|
499 |
this.exclude = exclude; |
|
500 |
} |
|
501 |
|
|
502 |
public String getInclude() { |
|
503 |
return include; |
|
504 |
} |
|
505 |
|
|
506 |
public void setInclude(String include) { |
|
507 |
this.include = include; |
|
508 |
} |
|
509 |
|
|
510 |
public boolean isQuiet() { |
|
511 |
return quiet; |
|
512 |
} |
|
513 |
|
|
514 |
public void setQuiet(boolean quiet) { |
|
515 |
this.quiet = quiet; |
|
516 |
} |
|
517 |
|
|
518 |
/** |
|
519 |
* Sets property files for which keys properties should be included. |
|
520 |
* @param includePropertyKeysFromFiles |
|
521 |
*/ |
|
522 |
public void setIncludePropertyKeysFromFiles( |
|
523 |
String[] includePropertyKeysFromFiles) { |
|
524 |
if (includePropertyKeysFromFiles!=null) { |
|
525 |
this.includePropertyKeysFromFiles = Arrays.copyOf( |
|
526 |
includePropertyKeysFromFiles, |
|
527 |
includePropertyKeysFromFiles.length); |
|
528 |
} |
|
529 |
} |
|
530 |
} |
|
531 | 0 |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/trunk/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
|
3 |
|
|
4 |
<groupId>eu.dnetlib</groupId> |
|
5 |
<version>0.0.2</version> |
|
6 |
<modelVersion>4.0.0</modelVersion> |
|
7 |
<artifactId>icm-iis-properties-maven-plugin</artifactId> |
|
8 |
<packaging>maven-plugin</packaging> |
|
9 |
|
|
10 |
<scm> |
|
11 |
<developerConnection> |
|
12 |
scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2 |
|
13 |
</developerConnection> |
|
14 |
</scm> |
|
15 |
|
|
16 |
<properties> |
|
17 |
</properties> |
|
18 |
|
|
19 |
<repositories> |
|
20 |
<repository> |
|
21 |
<id>dnet-deps</id> |
|
22 |
<name>dnet-dependencies</name> |
|
23 |
<url>http://maven.research-infrastructures.eu/nexus/content/repositories/dnet-deps</url> |
|
24 |
<layout>default</layout> |
|
25 |
</repository> |
|
26 |
</repositories> |
|
27 |
|
|
28 |
<dependencies> |
|
29 |
<dependency> |
|
30 |
<groupId>org.apache.maven</groupId> |
|
31 |
<artifactId>maven-plugin-api</artifactId> |
|
32 |
<version>2.0</version> |
|
33 |
</dependency> |
|
34 |
<dependency> |
|
35 |
<groupId>org.apache.maven</groupId> |
|
36 |
<artifactId>maven-project</artifactId> |
|
37 |
<version>2.0</version> |
|
38 |
</dependency> |
|
39 |
<dependency> |
|
40 |
<groupId>org.kuali.maven.plugins</groupId> |
|
41 |
<artifactId>properties-maven-plugin</artifactId> |
|
42 |
<version>1.3.2</version> |
|
43 |
</dependency> |
|
44 |
</dependencies> |
|
45 |
<build> |
|
46 |
<directory>target</directory> |
|
47 |
<outputDirectory>target/classes</outputDirectory> |
|
48 |
<finalName>${project.artifactId}-${project.version}</finalName> |
|
49 |
<testOutputDirectory>target/test-classes</testOutputDirectory> |
|
50 |
<plugins> |
|
51 |
<plugin> |
|
52 |
<groupId>org.apache.maven.plugins</groupId> |
|
53 |
<artifactId>maven-compiler-plugin</artifactId> |
|
54 |
<version>2.3.2</version> |
|
55 |
<configuration> |
|
56 |
<source>1.6</source> |
|
57 |
<target>1.6</target> |
|
58 |
</configuration> |
|
59 |
</plugin> |
|
60 |
<plugin> |
|
61 |
<groupId>org.apache.maven.plugins</groupId> |
|
62 |
<artifactId>maven-source-plugin</artifactId> |
|
63 |
<version>2.1.2</version> |
|
64 |
<executions> |
|
65 |
<execution> |
|
66 |
<id>attach-sources</id> |
|
67 |
<phase>verify</phase> |
|
68 |
<goals> |
|
69 |
<goal>jar-no-fork</goal> |
|
70 |
</goals> |
|
71 |
</execution> |
|
72 |
</executions> |
|
73 |
</plugin> |
|
74 |
<plugin> |
|
75 |
<groupId>org.apache.maven.plugins</groupId> |
|
76 |
<artifactId>maven-surefire-plugin</artifactId> |
|
77 |
<version>2.4.3</version> |
|
78 |
<configuration> |
|
79 |
<redirectTestOutputToFile>true</redirectTestOutputToFile> |
|
80 |
<includes> |
|
81 |
<include>**/*Test.java</include> |
|
82 |
</includes> |
|
83 |
<excludes> |
|
84 |
<exclude>**/AllTests.java</exclude> |
|
85 |
<exclude>**/Abstract*Test.java</exclude> |
|
86 |
</excludes> |
|
87 |
</configuration> |
|
88 |
</plugin> |
|
89 |
<plugin> |
|
90 |
<groupId>org.apache.maven.plugins</groupId> |
|
91 |
<artifactId>maven-javadoc-plugin</artifactId> |
|
92 |
<version>2.9</version> |
|
93 |
<configuration> |
|
94 |
<detectLinks>true</detectLinks> |
|
95 |
<additionalparam>-Xdoclint:none</additionalparam> |
|
96 |
</configuration> |
|
97 |
</plugin> |
|
98 |
</plugins> |
|
99 |
</build> |
|
100 |
|
|
101 |
<distributionManagement> |
|
102 |
<repository> |
|
103 |
<id>dnet45-bootstrap-release</id> |
|
104 |
<url> |
|
105 |
http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-bootstrap-release |
|
106 |
</url> |
|
107 |
</repository> |
|
108 |
</distributionManagement> |
|
109 |
</project> |
modules/icm-iis-properties-maven-plugin/tags/icm-iis-properties-maven-plugin-0.0.2/trunk/README.markdown | ||
---|---|---|
1 |
Maven plugin module utilized by `icm-iis-parent-container` for proper `job.properties` file building. |
|
2 |
Based on: |
|
3 |
http://site.kuali.org/maven/plugins/properties-maven-plugin/1.3.2/write-project-properties-mojo.html |
|
4 |
Supplemented with: |
|
5 |
* handling includePropertyKeysFromFiles property allowing writing only properties listed in given property files |
|
6 |
As a final outcome only properties listed in <include> element and listed as a keys in files from <includePropertyKeysFromFiles> element will be written to output file. |
Also available in: Unified diff
Removing tag after mvn release:rollback.