1

I have a JAVA Ant task which is-

<target name="javatask">
<java classname="arq.sparql" fork="true" outputproperty="javaresult" errorproperty="javaerror1">
       <arg value="--data=${tools.dir}/build-config/SPARQL/cpldm.ttl"/>
       <arg value="--query=${queryFile}"/>
       <jvmarg value="-Xmx1024M"/>
       <classpath>
          <path>
              <fileset dir="${jena.dir}/lib">
                <include name="*.jar"/>
              </fileset>
         </path>
       </classpath>
   </java>
       <echo message="Error at: ${javaerror1} in ${queryFile}"/>
       <echo message="Result for ${queryFile} is: ${javaresult}"/>
</target>

Now I want the error message to be echo-ed only if there is a 'javaerror' and the Result message to be echoed if there is no error. So basically its kind of if-else condition, i.e if there is error echo error message , else- Give result message. How can I achieve that

Som Sarkar
  • 289
  • 1
  • 5
  • 24

2 Answers2

1
<target name="javataskfailure" if="javaerror1" depends="javatask">
    <echo message="Error at: ${javaerror1} in ${queryFile}"/>
    <echo message="Result for ${queryFile} is: ${javaresult}"/>
</target>

The task will only execute if the "javaerror1" property exists.

MarkOfHall
  • 3,334
  • 1
  • 26
  • 30
  • Did you run the javatarkfailure target? It will run the javatask because it depends on it. – MarkOfHall Mar 03 '14 at 16:56
  • Right, thats done, now the problem is 'javaerror1' will get set all the time whether there is error or not, so even if there is no error the error messahe is echoed. Any help? – Som Sarkar Mar 03 '14 at 17:07
  • You're asking me why there is output to standard error out when there is no error? LOL Echo the standard error and figure out what is writing to it when there is no error.... – MarkOfHall Mar 03 '14 at 17:17
  • No, you got me wrong, the value of 'javaerror1' is set to "" if there is no error, but still it getting set to a value which is null in this case, hence 'if="javaerror1"' condition will always pass. – Som Sarkar Mar 03 '14 at 17:25
  • It seems the fork is causing the return of the empty string.... at least in my test. – MarkOfHall Mar 03 '14 at 17:54
  • As an alternative you could uses 'resultproperty' and call System.exit(-1) in your java code. Then use a conditional task to check for -1 and set javataskfailure. – MarkOfHall Mar 03 '14 at 18:06
1

The latest version of Ant includes a new If and Unless attributes that can be set in almost any task. I've had a few problems with getting it to work, and you do need to have Ant 1.9.1 or higher, but it does make if/else conditions much easier to handle.

For example, you could put this in your <echo/> statement:

<echo unless:blank="javaerror1">Error at: ${javaerror1} in ${queryFile}</echo>

You might be able to use the set attribute instead:

<echo if:set"javaerror1">Error at: ${javaerror1} in ${queryFile}</echo>
David W.
  • 105,218
  • 39
  • 216
  • 337
  • Thanks David, the problem is "javaerror1" will always get set whenver the Java ant task is run, so even if there is no error which gets caught by the args2, the error message is getting echoed. – Som Sarkar Mar 03 '14 at 17:12