Passing testng command line arguments from ant

How to pass testng command line arguments dynamically when Ant is used for invoking testng task.

Below is the sample section of testng task target defined in Ant build.xml file:

  <target name="runTest" depends="compile">
      <testng classpath="${cp}:${build.dir}" groups="${groups}">
          <xmlfileset dir="${basedir}" includes="testng.xml"></xmlfileset>

Then from command line, by using the jvmarg properties, you can provide the value of “groups” (which is a standard testng command line attribute) dynamically like this:

  $ ant runTest -Dgroups group1,group2,...,groupN

Above is equivalent to invoking testng from command line using java:

  $ java org.testng.TestNG -groups group1,group2,...,groupN testng.xml

java automation ant testng