OSDN Git Service

Add Ant version check in setup task.
authorXavier Ducrohet <xav@android.com>
Thu, 9 Dec 2010 01:32:19 +0000 (17:32 -0800)
committerXavier Ducrohet <xav@android.com>
Thu, 9 Dec 2010 01:33:52 +0000 (17:33 -0800)
Change-Id: Id2237ae2fd64a1ccae5b1a1957099c218cdaf9a5

anttasks/src/com/android/ant/SetupTask.java

index 4f14d62..4d1bac6 100644 (file)
@@ -34,6 +34,7 @@ import org.apache.tools.ant.Project;
 import org.apache.tools.ant.taskdefs.ImportTask;
 import org.apache.tools.ant.types.Path;
 import org.apache.tools.ant.types.Path.PathElement;
+import org.apache.tools.ant.util.DeweyDecimal;
 import org.xml.sax.InputSource;
 
 import java.io.File;
@@ -64,6 +65,7 @@ import javax.xml.xpath.XPathExpressionException;
  *
  */
 public final class SetupTask extends ImportTask {
+    private final static String ANT_MIN_VERSION = "1.8.0";
     // main rules file
     private final static String RULES_MAIN = "main_rules.xml";
     // test rules file - depends on android_rules.xml
@@ -77,6 +79,17 @@ public final class SetupTask extends ImportTask {
     public void execute() throws BuildException {
         Project antProject = getProject();
 
+        // check the Ant version
+        DeweyDecimal version = getVersion(antProject);
+        DeweyDecimal atLeast = new DeweyDecimal(ANT_MIN_VERSION);
+        if (atLeast.isGreaterThan(version)) {
+            throw new BuildException(
+                    "The Android Ant-based build system requires Ant " +
+                    ANT_MIN_VERSION +
+                    " or later. Current version is " +
+                    version);
+        }
+
         // get the SDK location
         File sdkDir = TaskHelper.getSdkLocation(antProject);
         String sdkOsPath = sdkDir.getPath();
@@ -548,4 +561,33 @@ public final class SetupTask extends ImportTask {
 
         return libraries;
     }
+
+    /**
+     * Returns the Ant version as a {@link DeweyDecimal} object.
+     *
+     * This is based on the implementation of
+     * org.apache.tools.ant.taskdefs.condition.AntVersion.getVersion()
+     *
+     * @param antProject the current ant project.
+     * @return the ant version.
+     */
+    private DeweyDecimal getVersion(Project antProject) {
+        char[] versionString = antProject.getProperty("ant.version").toCharArray();
+        StringBuffer sb = new StringBuffer();
+        boolean foundFirstDigit = false;
+        for (int i = 0; i < versionString.length; i++) {
+            if (Character.isDigit(versionString[i])) {
+                sb.append(versionString[i]);
+                foundFirstDigit = true;
+            }
+            if (versionString[i] == '.' && foundFirstDigit) {
+                sb.append(versionString[i]);
+            }
+            if (Character.isLetter(versionString[i]) && foundFirstDigit) {
+                break;
+            }
+        }
+        return new DeweyDecimal(sb.toString());
+    }
+
 }