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.IOException;
20 import java.io.OutputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Properties;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.io.IOUtils;
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.project.MavenProject;
31 import org.codehaus.plexus.util.StringUtils;
32
33
34
35
36
37 public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
38
39
40
41
42
43
44 MavenProject project;
45
46
47
48
49
50
51
52 File outputFile;
53
54
55
56
57
58
59
60
61 OutputStyle outputStyle;
62
63
64
65
66
67
68 String prefix;
69
70 protected void writeProperties(File file, Properties properties, OutputStyle outputStyle, String prefix) throws MojoExecutionException {
71 Properties prefixed = getPrefixedProperties(properties, prefix);
72 Properties formatted = getFormattedProperties(prefixed, outputStyle);
73 Properties sorted = getSortedProperties(formatted);
74 OutputStream out = null;
75 try {
76 out = FileUtils.openOutputStream(file);
77 sorted.store(out, "Created by the properties-maven-plugin");
78 } catch (IOException e) {
79 throw new MojoExecutionException("Error creating properties file", e);
80 } finally {
81 IOUtils.closeQuietly(out);
82 }
83 }
84
85 protected Properties getStandardMavenProperties(MavenProject project) {
86 Properties properties = new Properties();
87 properties.setProperty("project.groupId", project.getGroupId());
88 properties.setProperty("project.artifactId", project.getArtifactId());
89 properties.setProperty("project.version", project.getVersion());
90 properties.setProperty("project.basedir", project.getBasedir().getAbsolutePath());
91 properties.setProperty("project.build.directory", project.getBuild().getDirectory());
92 return properties;
93 }
94
95 protected SortedProperties getSortedProperties(Properties properties) {
96 SortedProperties sp = new SortedProperties();
97 sp.putAll(properties);
98 return sp;
99 }
100
101 protected Properties getFormattedProperties(Properties properties, OutputStyle style) {
102 switch (style) {
103 case NORMAL:
104 return properties;
105 case ENVIRONMENT_VARIABLE:
106 return getEnvironmentVariableProperties(properties);
107 default:
108 throw new IllegalArgumentException(outputStyle + " is unknown");
109 }
110 }
111
112 protected Properties getPrefixedProperties(Properties properties, String prefix) {
113 if (StringUtils.isBlank(prefix)) {
114 return properties;
115 }
116 List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
117 Collections.sort(keys);
118 Properties newProperties = new Properties();
119 for (String key : keys) {
120 String value = properties.getProperty(key);
121 String newKey = prefix + "." + key;
122 newProperties.setProperty(newKey, value);
123 }
124 return newProperties;
125 }
126
127 protected Properties getEnvironmentVariableProperties(Properties properties) {
128 List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
129 Collections.sort(keys);
130 Properties newProperties = new Properties();
131 for (String key : keys) {
132 String value = properties.getProperty(key);
133 String newKey = key.toUpperCase().replace(".", "_");
134 newProperties.setProperty(newKey, value);
135 }
136 return newProperties;
137 }
138 }