Apache Ant – Get and Set Ant property in Javascript when using the script task

In Ant script we could write Javascript code using the <script> task. In the following example, i will pass 2 Ant properties to Javascript and print the square value to the console. Also at the end, i will assign the greeting to a new Ant property.

<project name="AntLoopExample" default="run" basedir=".">
  <target name="run">
    <property name="loop.start" value="4"/>
    <property name="loop.end" value="7"/>
    <script language="javascript">
        // Print the square value of the number within the range
        var start = AntLoopExample.getProperty("loop.start");
        var end   = AntLoopExample.getProperty("loop.end");
        for (i = start; i <= end; i++) {
          echo = AntLoopExample.createTask("echo");

        // Set a new property which could be retrieved by the ant script
        AntLoopExample.setProperty("greeting", "Hello World!")
    <echo message="From Javascript: ${greeting}"/>


Run it.

Done =)



4 thoughts on “Apache Ant – Get and Set Ant property in Javascript when using the script task”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.