Apache Ant Less Than

How do you evaluate the number property less than in Apache Ant?

<property name="small" value="15"/> <property name="big" value="156"/> <fail message="small is less than big!"> <condition> <lessthan val1="${small}" val2="${big}"/> </condition> </fail> 

From what I saw (I'm new to Ant), can you only do <equal/> ?

+6
source share
6 answers

You can use <scriptcondition> (see http://ant.apache.org/manual/Tasks/conditions.html ).

Read the documentation carefully, because you will need to install additional jar dependencies in ant.

The condition may look like this (not verified):

 <scriptcondition language="javascript"> var small = parseInt(project.getProperty("small")); var big = parseInt(project.getProperty("big")); self.setValue(small < big); </scriptcondition> 
+4
source

Cheers JB Nizet finally got there.

 <!-- Test the Ant Version --> <property name="burning-boots-web-build.required-ant-version" value="1.8.2"/> <script language="javascript"> <![CDATA[ var current = project.getProperty("ant.version").match(/([0-9](\.)?)+/)[0].replace(/\./g,""); var required = project.getProperty("burning-boots-web-build.required-ant-version").match(/([0-9](\.)?)+/)[0].replace(/\./g,""); project.setProperty('ant.valid-version', current < required ? "false" : "true"); ]]> </script> <fail message="This build requires Ant version ${burning-boots-web-build.required-ant-version}."> <condition> <isfalse value="${ant.valid-version}"/> </condition> </fail> 
+2
source

Here is the use of the <isgreaterthan> condition task without any scripts:

 <if> <isgreaterthan arg1="100" arg2="10"/> <then> <echo>Number 100 is greater than number 10</echo> </then> </if> 

In addition, the value of arg1, arg2 may be a property variable.

Note: <isgreaterthan> is an optional condition available with Ant -Contrib:

http://ant-contrib.sourceforge.net/tasks/tasks/more_conditions.html

+2
source

And "less" comparioson properties are impossible without any custom tasks or a built-in script.

But in most cases, you can leave by applying the test not to the property, but to the source of the values. In the build system, these "sources" are usually files. In files you can use isfileselected condition along with selector . Most selectors accept when attributes, such as less , more or equal .

The manual for the isfileselected condition shows an example.

0
source

Ant addon Flaka provides a crash task that evaluates EL expressions, i.e.

 <project name="demo" xmlns:fl="antlib:it.haefelinger.flaka"> <property name="small" value="15"/> <property name="big" value="156"/> <fl:fail message="small is less than big!" test="small lt big"/> </project> 

conclusion:

 BUILD FAILED /home/rosebud/workspace/Ant/demo.xml:7: small is less than big! 

see Flaka manual for more details

0
source

Actually, the answer provided by @GnanaPrakash is incomplete. From Ant Contrib documentation:

These conditions are suitable for use in an element. Unfortunately, they cannot be used in <condition> but all conditions for a task can be used with <bool> and <bool> can be used anywhere where <condition> can be used.

So, islessthan or alternative isgreaterthan elements should be wrapped in a bool element as follows:

 <property name="small" value="15" /> <property name="big" value="156" /> <if> <bool> <islessthan arg1="${small}" arg2="${big}"/> </bool> <then> <echo message="small is less than big!" /> </then> <else> <echo message="big is less than small!" /> </else> </if> 

If you do not, you will receive an error message:

 if doesn't support the nested "islessthan" element. 
0
source

Source: https://habr.com/ru/post/898412/


All Articles