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">
      <![CDATA[
        // 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");
          echo.setMessage(i*i);
          echo.perform();
        }

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

 

Run it.
ant-loop-example-1
 

Done =)

Reference:

Advertisements

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 )

Google+ photo

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

Connecting to %s