Ant Scripting

Recently I discovered an interesting task in Optional Ant Tasks, it was <script>. At the beginning, I needed add deleting the oldest files in a storage. There are no standard ant ability to do this, so I took a look at some libraries, e.g. Ant-Contrib, but in the end I selected scripts because of their comfort. Here is small example of using this task:

<script language=”javascript”>
   <![CDATA[
   // here is some code
   ]]>
</script>

You can see “javascript” which is selected as a script language, but it doesn’t mean that we cannot use java code! We CAN, but wuth some changes. For instance, import of a class will be written as:

importClass(package.name.ClassName);

Also, it’s not allowed to declare variables. There is one interesting feature, when method or field name coincides with javascript keyword (if you can see thrown sun.org.mozilla.javascript.internal.EvaluatorException), it’s necessary to use quotes for that word. In my case this error was when I was trying delete a file:

aFile.delete();

Ant was angry at this snippet and said:

javax.script.ScriptException:
sun.org.mozilla.javascript.internal.EvaluatorException: missing name after .
operator (#48) in at line number 48

It was right to use the following line instead:

aFile["delete"]();

That’s all for the moment. Thanks for your attention.