Browse Source

Introduce an "atmost" attribute for the javaversion condition

master
Jaikiran Pai 4 years ago
parent
commit
079c134fd5
4 changed files with 67 additions and 8 deletions
  1. +5
    -0
      WHATSNEW
  2. +10
    -2
      manual/Tasks/conditions.html
  3. +38
    -6
      src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
  4. +14
    -0
      src/tests/antunit/taskdefs/condition/javaversion-test.xml

+ 5
- 0
WHATSNEW View File

@@ -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
=====================================


+ 10
- 2
manual/Tasks/conditions.html View File

@@ -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>


+ 38
- 6
src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java View File

@@ -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,12 +100,32 @@ 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.


+ 14
- 0
src/tests/antunit/taskdefs/condition/javaversion-test.xml View File

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


Loading…
Cancel
Save