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 java.util.Properties; 023 import java.io.InputStream; 024 025 import org.apache.geronimo.genesis.MojoSupport; 026 import org.apache.maven.plugin.MojoFailureException; 027 028 /** 029 * Forces the build to fail if the version of Maven is not compatible. 030 * 031 * @goal require-maven-version 032 * @phase validate 033 * 034 * @version $Rev: 508189 $ $Date: 2007-02-15 14:04:50 -0800 (Thu, 15 Feb 2007) $ 035 */ 036 public class RequireMavenVersionMojo 037 extends MojoSupport 038 { 039 /** 040 * Specify the required version of Maven (2.0.4, 2.0.4). 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=2.0+ would be allowed with any Maven 2.1.x, version=2.0* 046 * would allow any Maven 2.0.x, but not Maven 2.1.x. 047 * 048 * @parameter 049 * @required 050 */ 051 private String version = null; 052 053 /** 054 * Flag to skip the version check. 055 * 056 * @parameter expression="${requiremavenversion.skip}" default-value="false" 057 */ 058 private boolean skip = false; 059 060 /** 061 * The version of Maven we are running in. 062 */ 063 private String mavenVersion; 064 065 private Properties loadProperties() throws Exception { 066 InputStream input = getClass().getClassLoader().getResourceAsStream("META-INF/maven/org.apache.maven/maven-core/pom.properties"); 067 if (input == null) { 068 throw new MojoFailureException("Missing 'maven-core/pom.properties', can't find Maven version"); 069 } 070 071 Properties props = new Properties(); 072 try { 073 props.load(input); 074 } 075 finally { 076 input.close(); 077 } 078 079 return props; 080 } 081 082 /** 083 * Parse a float from '1.2.3', '1.2.3', '1.2.3.4', etc. 084 */ 085 private float parseFloat(final String input) { 086 assert input != null; 087 088 StringBuffer buff = new StringBuffer(); 089 boolean haveDot = false; 090 for (int i=0; i<input.length(); i++) { 091 char c = input.charAt(i); 092 if (!haveDot) { 093 buff.append(c); 094 if (c == '.') { 095 haveDot = true; 096 } 097 } 098 else { 099 // have a dot 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 }