As explained by @Moataz and @jheddings, ANT macrodef is the best way to abstract these repetitive and parameterized actions.
The following working example also demonstrates some additional enhancements to your build, such as using properties to abstract the location of your project files and using ANT paths to manage runtime class paths.
<project default="compile"> <description> Example build file. Demonstrates the following - Properties - Build paths - Running java programs using a macrodef </description> <property name="src.dir" location="src/main/java"/> <property name="build.dir" location="build"/> <property name="classes.dir" location="build/classes"/> <path id="runtime.path"> <pathelement location="${classes.dir}"/> </path> <macrodef name="call-prog"> <attribute name="classname"/> <attribute name="arg1"/> <attribute name="arg2" default=""/> <sequential> <java classname='@{classname}' classpathref="runtime.path"> <arg value='@{arg1}'/> <arg value='@{arg2}'/> </java> </sequential> </macrodef> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac destdir="${classes.dir}" srcdir='${src.dir}' includeantruntime="false"/> </target> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="prob1" depends='compile'> <call-prog classname="org.demo.App" arg1="Justin" arg2="Gertrude"/> </target> <target name="prob2" depends='compile'> <call-prog classname="org.demo.App" arg1="28"/> </target> .. .. </project>
Finally, when you have created a repository of common macro codes that you use in all your assemblies, you can consider them as ANTlib . The best way to share assembly logic in ANT, in my opinion. For this simple example, this would be redundant.
source share