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.util.Properties;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.maven.plugin.AbstractMojo;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.project.MavenProject;
24
25
26
27
28
29
30
31
32
33
34
35 public class ParseVersionPropertiesMojo extends AbstractMojo {
36 public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
37
38
39
40
41
42
43 private MavenProject project;
44
45
46
47
48
49
50
51 private String[] properties;
52
53
54
55
56
57
58
59 private boolean silent;
60
61 @Override
62 public void execute() throws MojoExecutionException {
63 Properties props = project.getProperties();
64 for (String key : properties) {
65 String value = getProperty(key);
66 if (StringUtils.isBlank(value)) {
67 continue;
68 }
69 Version version = parseVersion(value);
70 setProjectProperty(key, "major", version.getMajor(), props);
71 setProjectProperty(key, "minor", version.getMinor(), props);
72 setProjectProperty(key, "incremental", version.getIncremental(), props);
73 setProjectProperty(key, "qualifier", version.getQualifier(), props);
74 setProjectProperty(key, "trimmed", trimSnapshot(value), props);
75 }
76 }
77
78 protected String trimSnapshot(String version) {
79 if (version.toUpperCase().endsWith("-" + MAVEN_SNAPSHOT_TOKEN)) {
80 int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
81 return StringUtils.left(version, version.length() - length);
82 } else {
83 return version;
84 }
85 }
86
87 protected void setProjectProperty(String key, String suffix, String value, Properties props) {
88 if (StringUtils.isBlank(value)) {
89 return;
90 }
91 props.setProperty(key + "." + suffix, value);
92 if (!silent) {
93 getLog().info("Setting " + key + "." + suffix + "=" + value);
94 }
95 }
96
97 protected Version parseVersion(String s) {
98 boolean snapshot = s.toUpperCase().endsWith("-" + MAVEN_SNAPSHOT_TOKEN);
99 Version version = new Version();
100 version.setSnapshot(snapshot);
101 String[] tokens = StringUtils.split(s, ".-");
102 if (tokens.length > 0) {
103 version.setMajor(tokens[0]);
104 }
105 if (tokens.length > 1) {
106 version.setMinor(tokens[1]);
107 }
108 if (tokens.length > 2) {
109 version.setIncremental(tokens[2]);
110 }
111 String qualifier = getQualifier(tokens);
112 version.setQualifier(qualifier);
113 return version;
114 }
115
116 protected String getQualifier(String[] tokens) {
117 if (tokens.length <= 3) {
118 return null;
119 }
120 StringBuilder sb = new StringBuilder();
121 for (int i = 3; i < tokens.length; i++) {
122 if (tokens[i].toUpperCase().equals(MAVEN_SNAPSHOT_TOKEN)) {
123 break;
124 }
125 if (i != 3) {
126 sb.append("-");
127 }
128 sb.append(tokens[i]);
129 }
130 return sb.toString();
131 }
132
133 protected String getProperty(String key) {
134 String sys = System.getProperty(key);
135 String proj = project.getProperties().getProperty(key);
136 if (!StringUtils.isBlank(sys)) {
137 return sys;
138 } else {
139 return proj;
140 }
141
142 }
143
144 public String[] getProperties() {
145 return properties;
146 }
147
148 public void setProperties(String[] properties) {
149 this.properties = properties;
150 }
151
152 public boolean isSilent() {
153 return silent;
154 }
155
156 public void setSilent(boolean silent) {
157 this.silent = silent;
158 }
159 }