When using a beanshell script, Ant shows a special behavior together with references. To have beanshell, I used bsf-2.3.0.jar and bsh-2.0b4.jar in the ${ant.home}/lib directory. <?xml version="1.0"?> <project name="antbug"> <macrodef name="compileMapper" > <attribute name="objDir" /> <attribute name="id" default="compileMapperID" /> <sequential> <mapper id="@{id}"> <chainedmapper > <flattenmapper/> <globmapper from="*" to="@{objDir}/*.o"/> </chainedmapper> </mapper> </sequential> </macrodef> <target name="failes"> <script language="beanshell"><![CDATA[ // nothing ]]></script> <!-- throws java.lang.ClassCastException: org.apache.tools.ant.types.Mapper --> <compileMapper objDir="." /> </target> <target name="works"> <compileMapper objDir="." /> </target> <target name="override" > <script language="beanshell"><![CDATA[ // nothing ]]></script> <!-- Overriding previous definition of reference to anID --> <path id="anID"> <fileset dir="." includes="*.ant" /> </path> </target> <target name="noOverride" > <path id="anID"> <fileset dir="." includes="*.ant" /> </path> </target> </project>
Created attachment 17074 [details] Ant build file to reproduce the bug
Just changing the name of bug as it is a general script/reference problem. It is most likely related to the way references are processed - they are looked at both at xml parse time (when they are unknown elements) and at run time where they get resolved. There are a large number of bugs associated with this behaviour - some get fixed by hacks but a large number remain. Personally I think that the way refernces are handled should be looked at - so that they are processed at runtime. However I cannot see any way to do this in a backward compatible fashion.
I have added a workaround in ant 1.7. A new attribute "setbeans" has been added to the <script> task. This is by default "true". However if is set to false, only the "project" and "self" variables will be set.
A proper fix has been done, there is now no need for the setbeans attribute. I am leaving it in to allow <script> to be used without populating it with lots of properties, references and targets.