You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Basin Ilya <ba...@gmail.com> on 2019/04/14 11:10:08 UTC
overriding built-in tasks (not targets)
Hi.
My IDE generates an Ant script with the run-deploy and run-undeploy targets. They internally use the standard <get> task, but they don't check the outcome and sometimes return false positive.
The build is customizable by editing the file build.xml that includes this generated project file. I want to replace <get> with a macro that additionally greps the http response body for the success message.
The main problem is: when the macro tries to call the real <get>, it either fails with a stack overflow or with the message: "antcall task calling its own parent target". I thought I could fix it with name spaces, but it was too complex for me. My current workaround is to re-import org.apache.tools.ant.taskdefs.Get with a different task name.
There are some other issues with overriding <get> with a macrodef: the generated script calls get either with or without a password, but a macro cannot pass unknown attributes to the wrapped tasks. On the other hand, <presetdef> can pass them, but it won't let me add the additional checker task.
Macro attributes with default values don't work for the the "password" property of <get>, because null and empty string treated differently. There's no way to avoid calling the property setter, if the property has a default value in the macro instance. My workaround is to have two <real-get> elements in the custom script: with and without the password.
This leads to another problem: to invoke one of the <real-get> tasks conditionally I have to use <antcall> and because of that I can't support the optional children elements of <get>.
<?xml version="1.0" encoding="utf-8"?>
<project default="build" >
<taskdef name="real-get" classname="org.apache.tools.ant.taskdefs.Get" />
<macrodef name="get">
<attribute name="src"/>
<attribute name="dest"/>
<attribute name="username" default="NOT-SET" />
<attribute name="password" default="NOT-SET" />
<sequential>
<condition property="mymacro.no.creds">
<and>
<equals arg1="@{username}" arg2="NOT-SET"/>
<equals arg1="@{password}" arg2="NOT-SET"/>
</and>
</condition>
<property name="mymacro.src" value="@{src}"/>
<property name="mymacro.dest" value="@{dest}"/>
<property name="mymacro.username" value="@{username}"/>
<property name="mymacro.password" value="@{password}"/>
<antcall target="-get-without-creds"/>
<antcall target="-get-with-creds"/>
<!--
TODO: check download result and optionally fail
-->
<echo message="some additional check"/>
</sequential>
</macrodef>
<target name="-get-without-creds" if="mymacro.no.creds">
<real-get src="${mymacro.src}" dest="${mymacro.dest}" />
</target>
<target name="-get-with-creds" unless="mymacro.no.creds">
<real-get src="${mymacro.src}" dest="${mymacro.dest}" username="${mymacro.username}" password="${mymacro.password}" />
</target>
<target name="build">
<get src="http://www.calife.com/HTML/test.php" dest="out.html"/>
</target>
</project>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
AW: overriding built-in tasks (not targets)
Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
I am not sure if you could check the response headers.
The <get> manual shows an example how to add a MD5 checksum test after a download (macro "get-and-checksum").
Maybe the best way is to implement your own download in Java (reusing Get's sourcecode, or use a http lib).
Or I haven't got your points ...
Jan
> -----Ursprüngliche Nachricht-----
> Von: Basin Ilya [mailto:basinilya@gmail.com]
> Gesendet: Sonntag, 14. April 2019 13:10
> An: user@ant.apache.org
> Betreff: overriding built-in tasks (not targets)
>
> Hi.
>
> My IDE generates an Ant script with the run-deploy and run-undeploy
> targets. They internally use the standard <get> task, but they don't
> check the outcome and sometimes return false positive.
>
> The build is customizable by editing the file build.xml that includes
> this generated project file. I want to replace <get> with a macro that
> additionally greps the http response body for the success message.
>
> The main problem is: when the macro tries to call the real <get>, it
> either fails with a stack overflow or with the message: "antcall task
> calling its own parent target". I thought I could fix it with name
> spaces, but it was too complex for me. My current workaround is to re-
> import org.apache.tools.ant.taskdefs.Get with a different task name.
>
> There are some other issues with overriding <get> with a macrodef: the
> generated script calls get either with or without a password, but a
> macro cannot pass unknown attributes to the wrapped tasks. On the other
> hand, <presetdef> can pass them, but it won't let me add the additional
> checker task.
> Macro attributes with default values don't work for the the "password"
> property of <get>, because null and empty string treated differently.
> There's no way to avoid calling the property setter, if the property
> has a default value in the macro instance. My workaround is to have two
> <real-get> elements in the custom script: with and without the
> password.
>
> This leads to another problem: to invoke one of the <real-get> tasks
> conditionally I have to use <antcall> and because of that I can't
> support the optional children elements of <get>.
>
>
> <?xml version="1.0" encoding="utf-8"?>
> <project default="build" >
>
> <taskdef name="real-get"
> classname="org.apache.tools.ant.taskdefs.Get" />
>
> <macrodef name="get">
> <attribute name="src"/>
> <attribute name="dest"/>
> <attribute name="username" default="NOT-SET" />
> <attribute name="password" default="NOT-SET" />
> <sequential>
> <condition property="mymacro.no.creds">
> <and>
> <equals arg1="@{username}"
> arg2="NOT-SET"/>
> <equals arg1="@{password}"
> arg2="NOT-SET"/>
> </and>
> </condition>
> <property name="mymacro.src" value="@{src}"/>
> <property name="mymacro.dest" value="@{dest}"/>
> <property name="mymacro.username"
> value="@{username}"/>
> <property name="mymacro.password"
> value="@{password}"/>
> <antcall target="-get-without-creds"/>
> <antcall target="-get-with-creds"/>
> <!--
> TODO: check download result and optionally fail
> -->
> <echo message="some additional check"/>
> </sequential>
> </macrodef>
>
> <target name="-get-without-creds" if="mymacro.no.creds">
> <real-get src="${mymacro.src}" dest="${mymacro.dest}"
> />
> </target>
>
> <target name="-get-with-creds" unless="mymacro.no.creds">
> <real-get src="${mymacro.src}" dest="${mymacro.dest}"
> username="${mymacro.username}" password="${mymacro.password}" />
> </target>
>
> <target name="build">
> <get src="http://www.calife.com/HTML/test.php"
> dest="out.html"/>
> </target>
>
> </project>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional
> commands, e-mail: user-help@ant.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org