001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.genesis.plugins.tools;
021    
022    import org.apache.geronimo.genesis.MojoSupport;
023    
024    import org.apache.commons.lang.SystemUtils;
025    
026    import org.apache.maven.plugin.MojoFailureException;
027    
028    /**
029     * Forces the build to fail if the version of Java is not compatible.
030     *
031     * @goal require-java-version
032     * @phase validate
033     *
034     * @version $Rev: 467523 $ $Date: 2006-10-24 17:33:32 -0700 (Tue, 24 Oct 2006) $
035     */
036    public class RequireJavaVersionMojo
037        extends MojoSupport
038    {
039        /**
040         * Specify the required version of Java (1.1, 1.2, 1.3, 1.4, 1.5).
041         *
042         * Can specify a suffix of '+' to allow any version equal to or newer or '*'
043         * to allow versions in the same group.
044         *
045         * For example, version=1.4+ would be allowed on a JDK 1.5 VM, version=1.5*
046         * would allow any JDK 1.5, but not JDK 1.6.
047         *
048         * @parameter
049         * @required
050         */
051        private String version = null;
052        
053        /**
054         * Flag to skip the version check.
055         *
056         * @parameter expression="${requirejavaversion.skip}" default-value="false"
057         */
058        private boolean skip = false;
059        
060        protected void doExecute() throws Exception {
061            if (skip) {
062                log.warn("Skipping Java version check");
063            }
064            
065            version = version.trim();
066            
067            if (version.endsWith("*")) {
068                version = version.substring(0, version.length() - 1).trim();
069                
070                log.debug("Checking Java version is in the same group as: " + version);
071                
072                String tmp = SystemUtils.JAVA_VERSION_TRIMMED;
073                
074                log.debug("Requested version: " + tmp);
075                log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
076                
077                if (!tmp.startsWith(version)) {
078                    throw new MojoFailureException("This build requires Java version " + version + 
079                        " or a greater version in the same group, found version: " + 
080                        SystemUtils.JAVA_VERSION_FLOAT);
081                }
082            }
083            else if (version.endsWith("+")) {
084                version = version.substring(0, version.length() - 1).trim();
085                
086                log.debug("Checking Java version is greater than: " + version);
087                
088                float tmp = Float.parseFloat(version);
089                
090                log.debug("Requested version: " + tmp);
091                log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
092                
093                if (tmp > SystemUtils.JAVA_VERSION_FLOAT) {
094                    throw new MojoFailureException("This build requires Java version " + version + 
095                        " or greater, found version: " + SystemUtils.JAVA_VERSION_FLOAT);
096                }
097            }
098            else {
099                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    }