View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }