Skip to main content

Macrodef makes Ant stronger

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:


<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

Popular posts from this blog

cucumber-jvm and Android Studio

During this Winter holidays, I've been exploring a Behavior Driven Development and experimented with a couple of testing frameworks: Calabash and cucumber-jvm . In this post, I would like to describe setting up cucumber-jvm for Android Studio projects. I've extended cucumber-jvm examples with a version for Android Studio. Here is a copy in my github branch: https://github.com/mdzyuba/cucumber-jvm/tree/add_android_studio_example/examples/android/android-studio/Cukeulator . In addition to the example code, here is some details on how it was created. Dependencies The cucumber-jvm requires several libraries to be added to a project:  https://github.com/cucumber/cucumber-jvm/tree/master/android I've found it's easier to add those dependencies with the Android Studio project setup dialog because it takes care of the jar versions for you. File > Project Structure > Modules > app > Dependencies > + > Library dependency   > type "cuc...

MySQL macport install on Mac OS X Tiger

I have installed mysql5 macport on the MacBookPro laptop running OS X Tiger 10.4. There are a few steps that needs to be done once the port is installed. These steps are not documented (see Ticket #12694 on macports.org). So, after reading a few blogs and analyzing mysql startup error messages, I figured out what needs to be done in order to get it running. That info might be useful to others, so I’ve decided to publish the solution here. First of all, we need to create the initial database database $ sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql Here is the output of the command: $ sudo ./mysql_install_db --user=mysql Installing MySQL system tables... 071118 0:06:29 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive OK Filling help tables... 071118 0:06:29 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive OK To start mysqld at boot time you hav...

Building a Flex project with Ant

Here is a quick sample on how to build a simple Flex "hello world" project with Ant. The "hello world" project contains a src folder with one Flex application file and an Ant build.xml file: ./project_home/ ./src/ app.mxml build.xml The app.mxml is the main module of the project. It simply has a label with a "Hello World!" text: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Label text="Hello World!"/> </mx:Application> Here is the build.xml source: <?xml version="1.0"?> <project name="fx_practice7" default="all"> <!-- Init the build process --> <target name="init" unless="initialized"> <!-- Name of project and version --> <property name="FLEX_HOME" location="/Users/mykola/java/flex"/> ...