1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.genesis.plugins.tools;
21
22 import java.util.Properties;
23 import java.io.InputStream;
24
25 import org.apache.geronimo.genesis.MojoSupport;
26 import org.apache.maven.plugin.MojoFailureException;
27
28 /**
29 * Forces the build to fail if the version of Maven is not compatible.
30 *
31 * @goal require-maven-version
32 * @phase validate
33 *
34 * @version $Rev: 508189 $ $Date: 2007-02-15 14:04:50 -0800 (Thu, 15 Feb 2007) $
35 */
36 public class RequireMavenVersionMojo
37 extends MojoSupport
38 {
39 /**
40 * Specify the required version of Maven (2.0.4, 2.0.4).
41 *
42 * Can specify a suffix of '+' to allow any version equal to or newer or '*'
43 * to allow versions in the same group.
44 *
45 * For example, version=2.0+ would be allowed with any Maven 2.1.x, version=2.0*
46 * would allow any Maven 2.0.x, but not Maven 2.1.x.
47 *
48 * @parameter
49 * @required
50 */
51 private String version = null;
52
53 /**
54 * Flag to skip the version check.
55 *
56 * @parameter expression="${requiremavenversion.skip}" default-value="false"
57 */
58 private boolean skip = false;
59
60 /**
61 * The version of Maven we are running in.
62 */
63 private String mavenVersion;
64
65 private Properties loadProperties() throws Exception {
66 InputStream input = getClass().getClassLoader().getResourceAsStream("META-INF/maven/org.apache.maven/maven-core/pom.properties");
67 if (input == null) {
68 throw new MojoFailureException("Missing 'maven-core/pom.properties', can't find Maven version");
69 }
70
71 Properties props = new Properties();
72 try {
73 props.load(input);
74 }
75 finally {
76 input.close();
77 }
78
79 return props;
80 }
81
82 /**
83 * Parse a float from '1.2.3', '1.2.3', '1.2.3.4', etc.
84 */
85 private float parseFloat(final String input) {
86 assert input != null;
87
88 StringBuffer buff = new StringBuffer();
89 boolean haveDot = false;
90 for (int i=0; i<input.length(); i++) {
91 char c = input.charAt(i);
92 if (!haveDot) {
93 buff.append(c);
94 if (c == '.') {
95 haveDot = true;
96 }
97 }
98 else {
99
100 if (c != '.') {
101 buff.append(c);
102 }
103 }
104 }
105
106 return Float.parseFloat(buff.toString());
107 }
108
109 protected void doExecute() throws Exception {
110 if (skip) {
111 log.warn("Skipping Maven version check");
112 }
113
114 Properties props = loadProperties();
115 mavenVersion = props.getProperty("version");
116 if (mavenVersion == null) {
117 throw new MojoFailureException("Missing 'version' property in 'maven-core/pom.properties'");
118 }
119 log.debug("Current Maven version: " + mavenVersion);
120
121 float mavenVersionFloat = parseFloat(mavenVersion);
122
123 version = version.trim();
124
125 if (version.endsWith("*")) {
126 version = version.substring(0, version.length() - 1).trim();
127
128 log.debug("Checking Maven version is in the same group as: " + version);
129
130 if (!mavenVersion.startsWith(version)) {
131 throw new MojoFailureException("This build requires Maven version " + version +
132 " or a greater version in the same group, found version: " + mavenVersion);
133 }
134 }
135 else if (version.endsWith("+")) {
136 version = version.substring(0, version.length() - 1).trim();
137
138 log.debug("Checking Maven version is greater than: " + version);
139
140 float tmp = parseFloat(version);
141
142 if (tmp > mavenVersionFloat) {
143 throw new MojoFailureException("This build requires Maven version " + version +
144 " or greater, found version: " + mavenVersion);
145 }
146 }
147 else {
148 log.debug("Checking Maven version is equal to: " + version);
149
150 float tmp = parseFloat(version);
151
152 if (tmp != mavenVersionFloat) {
153 throw new MojoFailureException("This build requires Maven version " + version +
154 ", found version: " + mavenVersion);
155 }
156 }
157 }
158 }