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 org.apache.geronimo.genesis.MojoSupport;
23
24 import org.apache.commons.lang.SystemUtils;
25
26 import org.apache.maven.plugin.MojoFailureException;
27
28 /**
29 * Forces the build to fail if the version of Java is not compatible.
30 *
31 * @goal require-java-version
32 * @phase validate
33 *
34 * @version $Rev: 467523 $ $Date: 2006-10-24 17:33:32 -0700 (Tue, 24 Oct 2006) $
35 */
36 public class RequireJavaVersionMojo
37 extends MojoSupport
38 {
39 /**
40 * Specify the required version of Java (1.1, 1.2, 1.3, 1.4, 1.5).
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=1.4+ would be allowed on a JDK 1.5 VM, version=1.5*
46 * would allow any JDK 1.5, but not JDK 1.6.
47 *
48 * @parameter
49 * @required
50 */
51 private String version = null;
52
53 /**
54 * Flag to skip the version check.
55 *
56 * @parameter expression="${requirejavaversion.skip}" default-value="false"
57 */
58 private boolean skip = false;
59
60 protected void doExecute() throws Exception {
61 if (skip) {
62 log.warn("Skipping Java version check");
63 }
64
65 version = version.trim();
66
67 if (version.endsWith("*")) {
68 version = version.substring(0, version.length() - 1).trim();
69
70 log.debug("Checking Java version is in the same group as: " + version);
71
72 String tmp = SystemUtils.JAVA_VERSION_TRIMMED;
73
74 log.debug("Requested version: " + tmp);
75 log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
76
77 if (!tmp.startsWith(version)) {
78 throw new MojoFailureException("This build requires Java version " + version +
79 " or a greater version in the same group, found version: " +
80 SystemUtils.JAVA_VERSION_FLOAT);
81 }
82 }
83 else if (version.endsWith("+")) {
84 version = version.substring(0, version.length() - 1).trim();
85
86 log.debug("Checking Java version is greater than: " + version);
87
88 float tmp = Float.parseFloat(version);
89
90 log.debug("Requested version: " + tmp);
91 log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
92
93 if (tmp > SystemUtils.JAVA_VERSION_FLOAT) {
94 throw new MojoFailureException("This build requires Java version " + version +
95 " or greater, found version: " + SystemUtils.JAVA_VERSION_FLOAT);
96 }
97 }
98 else {
99 log.debug("Checking Java version is equal to: " + version);
100
101 float tmp = Float.parseFloat(version);
102
103 log.debug("Requested version: " + tmp);
104 log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
105
106 if (tmp != SystemUtils.JAVA_VERSION_FLOAT) {
107 throw new MojoFailureException("This build requires Java version " + version +
108 ", found version: " + SystemUtils.JAVA_VERSION_FLOAT);
109 }
110 }
111 }
112 }