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 public class ParseVersionPropertiesMojo extends AbstractMojo {
33
34
35
36
37
38
39 private MavenProject project;
40
41
42
43
44
45
46
47 private String[] properties;
48
49 @Override
50 public void execute() throws MojoExecutionException {
51 Properties props = project.getProperties();
52 for (String key : properties) {
53 String value = getProperty(key);
54 if (StringUtils.isBlank(value)) {
55 continue;
56 }
57 Version version = parseVersion(value);
58 setProjectProperty(key, "major", version.getMajor(), props);
59 setProjectProperty(key, "minor", version.getMinor(), props);
60 setProjectProperty(key, "incremental", version.getIncremental(), props);
61 setProjectProperty(key, "qualifier", version.getQualifier(), props);
62 }
63 }
64
65 protected void setProjectProperty(String key, String suffix, String value, Properties props) {
66 if (StringUtils.isBlank(value)) {
67 return;
68 }
69 props.setProperty(key + "." + suffix, value);
70 getLog().info("Setting " + key + "." + suffix + "=" + value);
71 }
72
73 protected Version parseVersion(String s) {
74 Version version = new Version();
75 String[] tokens = StringUtils.split(s, ".-");
76 if (tokens.length > 0) {
77 version.setMajor(tokens[0]);
78 }
79 if (tokens.length > 1) {
80 version.setMinor(tokens[1]);
81 }
82 if (tokens.length > 2) {
83 version.setIncremental(tokens[2]);
84 }
85 if (tokens.length > 3) {
86 version.setQualifier(tokens[3]);
87 }
88 return version;
89 }
90
91 protected String getProperty(String key) {
92 String sys = System.getProperty(key);
93 String proj = project.getProperties().getProperty(key);
94 if (!StringUtils.isBlank(sys)) {
95 return sys;
96 } else {
97 return proj;
98 }
99
100 }
101
102 public String[] getProperties() {
103 return properties;
104 }
105
106 public void setProperties(String[] properties) {
107 this.properties = properties;
108 }
109 }