I found new macrodef feature in ant extremely useful in my code development. It allows me to reuse lots of code and save time. Here is a quick example on how to use macrodef and why it is cool
Let’s say I have several web applications that I need to deploy on the same web server. If I use a regular Ant task, I would have to replicate these lines of code for every application:
I know it does not look that bad, but this is just a short sample. In a real world case, I also need to compile the application, generate a WAR file, deploy it, generate a client stub, etc. So, the Ant build.xml tends to grow very fast. In one of my previous project with about #5,000 the build.xml was over 700 lines of code.
Here is a solution that I like. I define a macro that deploys my application:
Once it is defined, I can deploy a set of applications simply by calling:
The macro above can be enhanced to do other tasks. I find it very convenient. I think it is a good practice to replace lots of repetitive tasks in Ant build file with a macro.
Let’s say I have several web applications that I need to deploy on the same web server. If I use a regular Ant task, I would have to replicate these lines of code for every application:
<wldeploy action="deploy"
verbose="${verbose}"
debug="${debug}"
name="myApp"
source="${output.dir}/myApp.war"
user="${admin.username}"
password="${admin.password}"
adminurl="${adminurl}"
targets="myWebSiteDomain" />
I know it does not look that bad, but this is just a short sample. In a real world case, I also need to compile the application, generate a WAR file, deploy it, generate a client stub, etc. So, the Ant build.xml tends to grow very fast. In one of my previous project with about #5,000 the build.xml was over 700 lines of code.
Here is a solution that I like. I define a macro that deploys my application:
<macrodef name="myApp.deploy">
<attribute name="name" />
<sequential>
<wldeploy action="deploy"
verbose="${verbose}"
debug="${debug}"
name="@{name}"
source="${output.dir}/@{name}.war"
user="${admin.username}"
password="${admin.password}"
adminurl="${adminurl}"
targets=" myWebSiteDomain " />
</sequential>
</macrodef>
Once it is defined, I can deploy a set of applications simply by calling:
<myApp.deploy name="myApp1" />
<myApp.deploy name="myApp2" />
…
<myApp.deploy name="myAppN" />
The macro above can be enhanced to do other tasks. I find it very convenient. I think it is a good practice to replace lots of repetitive tasks in Ant build file with a macro.
Comments