You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2020/11/21 06:48:25 UTC

[ant] branch master updated: Introduce an "atmost" attribute for the javaversion condition

This is an automated email from the ASF dual-hosted git repository.

jaikiran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new 079c134  Introduce an "atmost" attribute for the javaversion condition
079c134 is described below

commit 079c134fd5df0ded3b2e0c58fce25c40f3f4c144
Author: Jaikiran Pai <ja...@apache.org>
AuthorDate: Sat Nov 21 12:17:57 2020 +0530

    Introduce an "atmost" attribute for the javaversion condition
---
 WHATSNEW                                           |  5 +++
 manual/Tasks/conditions.html                       | 12 +++++-
 .../tools/ant/taskdefs/condition/JavaVersion.java  | 44 +++++++++++++++++++---
 .../taskdefs/condition/javaversion-test.xml        | 14 +++++++
 4 files changed, 67 insertions(+), 8 deletions(-)

diff --git a/WHATSNEW b/WHATSNEW
index 1c6e28c..15ab134 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -6,6 +6,11 @@ Fixed bugs:
  * SCP (with sftp=true) task would fail if fetching file located in root directory
    Bugzilla Report 64742
 
+Other changes:
+--------------
+
+ * javaversion condition now has a new "atmost" attribute. See the javaversion
+   manual for more details
 
 Changes from Ant 1.10.8 TO Ant 1.10.9
 =====================================
diff --git a/manual/Tasks/conditions.html b/manual/Tasks/conditions.html
index 846733f..247f2dd 100644
--- a/manual/Tasks/conditions.html
+++ b/manual/Tasks/conditions.html
@@ -945,11 +945,19 @@ are supported, the <var>property</var> attribute is redundant and will be ignore
     <th scope="col">Required</th>
   </tr>
   <tr>
-    <td>atleast</td>
+    <td class="left">atleast</td>
     <td>The version that this JVM is of at least. The format
       is <code>major.minor.point</code>. Starting with Java 9 really only the major number is
       determined.</td>
-    <td rowspan="2">Exactly one of the two</td>
+    <td rowspan="3">Exactly one of the three</td>
+  </tr>
+  <tr>
+    <td>atmost</td>
+    <td class="left">The version that this JVM is of at most. The format
+      is <code>major.minor.point</code>. Starting with Java 9 really only the major number is
+      determined.<br/>
+      <em>Since Ant 1.10.10</em>
+    </td>
   </tr>
   <tr>
     <td>exactly</td>
diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java b/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
index e121c30..a221495 100644
--- a/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
+++ b/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
@@ -23,10 +23,11 @@ import org.apache.tools.ant.util.JavaEnvUtils;
 
 /**
  * An Java version condition.
- * @since Java 1.10.2
+ * @since Ant 1.10.2
  */
 public class JavaVersion implements Condition {
 
+    private String atMost = null;
     private String atLeast = null;
     private String exactly = null;
 
@@ -44,16 +45,19 @@ public class JavaVersion implements Condition {
         if (null != exactly) {
             return actual.isEqual(new DeweyDecimal(exactly));
         }
+        if (atMost != null) {
+            return actual.isLessThanOrEqual(new DeweyDecimal(atMost));
+        }
         //default
         return false;
     }
 
     private void validate() throws BuildException {
-        if (atLeast != null && exactly != null) {
-            throw new BuildException("Only one of atleast or exactly may be set.");
+        if (atLeast != null && exactly != null && atMost != null) {
+            throw new BuildException("Only one of atleast or atmost or exactly may be set.");
         }
-        if (null == atLeast && null == exactly) {
-            throw new BuildException("One of atleast or exactly must be set.");
+        if (null == atLeast && null == exactly && atMost == null) {
+            throw new BuildException("One of atleast or atmost or exactly must be set.");
         }
         if (atLeast != null) {
             try {
@@ -64,6 +68,14 @@ public class JavaVersion implements Condition {
                     "The 'atleast' attribute is not a Dewey Decimal eg 1.1.0 : "
                     + atLeast);
             }
+        } else if (atMost != null) {
+            try {
+                new DeweyDecimal(atMost); //NOSONAR
+            } catch (NumberFormatException e) {
+                throw new BuildException(
+                        "The 'atmost' attribute is not a Dewey Decimal eg 1.1.0 : "
+                                + atMost);
+            }
         } else {
             try {
                 // only created for side effect
@@ -88,13 +100,33 @@ public class JavaVersion implements Condition {
      * Set the atleast attribute.
      * This is of the form major.minor.point.
      * For example 1.7.0.
-     * @param atLeast the version to check against.
+     * @param atLeast the version to set
      */
     public void setAtLeast(String atLeast) {
         this.atLeast = atLeast;
     }
 
     /**
+     * Get the atmost attribute.
+     * @return the atmost attribute.
+     * @since Ant 1.10.10
+     */
+    public String getAtMost() {
+        return atMost;
+    }
+
+    /**
+     * Set the atmost attribute.
+     * This is of the form major.minor.point.
+     * For example 11.0.2
+     * @param atMost the version to set
+     * @since Ant 1.10.10
+     */
+    public void setAtMost(String atMost) {
+        this.atMost = atMost;
+    }
+
+    /**
      * Get the exactly attribute.
      * @return the exactly attribute.
      */
diff --git a/src/tests/antunit/taskdefs/condition/javaversion-test.xml b/src/tests/antunit/taskdefs/condition/javaversion-test.xml
index a5d9a21..586cad0 100644
--- a/src/tests/antunit/taskdefs/condition/javaversion-test.xml
+++ b/src/tests/antunit/taskdefs/condition/javaversion-test.xml
@@ -25,6 +25,20 @@
     </au:assertTrue>
   </target>
 
+  <target name="test-atmost">
+    <au:assertTrue message="Expected javaversion ${java.version} to be at most 1000.111.211">
+      <!-- a high version value so that the check passes -->
+      <javaversion atmost="1000.111.211" />
+    </au:assertTrue>
+  </target>
+
+  <target name="test-atmost-negative">
+    <au:assertFalse message="Expected javaversion ${java.version} to be at most 1.4.0">
+      <!-- Ant 1.10.x requires Java 8 at runtime - so this check is expected to return false -->
+      <javaversion atmost="1.4.0" />
+    </au:assertFalse>
+  </target>
+
   <target name="test-exactly">
     <au:assertTrue message="Expected javaversion of ${ant.java.version}">
       <javaversion exactly="${ant.java.version}" />