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 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                  // 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 }