How to use script output in Ant Conditional

I want to do something like the following

<target name="complex-conditional">
  <if>
    <exec command= "python some-python-script-that-returns-true-or-false-strings-to-stout.py/>
    <then>
      <echo message="I did sometheing" />
    </then>
    <else>
      <echo message="I did something else" />
    </else>
  </if>
</target>

How can I evaluate the result of running some script inside an ant condition?

+3
source share
2 answers

<exec>task has parameters outputproperty, errorpropertyand resultpropertywhich allow you to specify the names of properties in which to save the output of the command / error / result code.

Then you can use one (or more) of them in your conditional statements:

<exec command="python some-python-script-that-returns-true-or-false-strings-to-stout.py"
      outputproperty="myout" resultproperty="myresult"/>
<if>
  <equals arg1="${myresult}" arg2="1" />
  <then>
    <echo message="I did sometheing" />
  </then>
  <else>
    <echo message="I did something else" />
  </else>
</if>
+3
source

I created a macro to help with this:

<!-- A macro for running something with "cmd" and then setting a property
     if the output from cmd contains a given string -->
<macrodef name="check-cmd-output">
    <!-- Program to run -->
    <attribute name="cmd" />
    <!-- String to look for in program output -->
    <attribute name="contains-string" />
    <!-- Property to set if "contains-string" is present -->
    <attribute name="property" />
    <sequential>
        <local name="temp-command-output" />
        <exec executable="cmd" outputproperty="temp-command-output">
            <arg value="/c @{cmd}" />
        </exec>
        <condition property="@{property}" >
            <contains string="${temp-command-output}" substring="@{contains-string}" />
        </condition>
    </sequential>
</macrodef>

Then you can use it like this:

<target name="check-mysql-started" depends="init" >
    <check-cmd-output cmd="mysqladmin ping" contains-string="mysqld is alive" property="mysql-started" />
</target>

and then do something similar to the following:

<target name="start-mysql" depends="check-mysql-started" unless="mysql-started">
    <!-- code to start mysql -->
</target>

, Ant . , .

0

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


All Articles