ANT, Development Tools

ANT’s XmlProperty task


Recently I needed to extract all the EJBs declared in an ear’s application.xml from within an ANT build script. At first I thought this may need a custom ANT task in order to do it cleanly or else I may have to do some messy file/string processing using ‘existing’ (to my knowledge) ANT tasks.

But if ANT itself is based on XML I’m sure it should have some tasks for working with XML files. Googling resulted in a task I had never heard of before, XmlProperty.

This is a really powerful yet easy to use ANT task which parses an input XML file and places the contents in ANT properties that correspond to tag names defined in the XML file. Exactly what I wanted!!!!!

So here goes…my application.xml file looks something like this

<application version="5" 
             [...removed for brevity...]">
   <display-name>My Application</display-name>
   <module>
      <ejb>customermanager-ejb.jar</ejb>
   </module>
   <module>
      <ejb>suppliermanager-ejb.jar</ejb>
   </module>
   <module>
      <ejb>ordermanager-ejb.jar</ejb>
   </module>
   <module>
      <ejb>movierentals-ejb.jar</ejb>
   </module>
 </application>  

What I need is to get a delimited list of ejb’s declared in the XML file above. ANT to the rescue! Note that the ANT property name application.module.ejb below corresponds to the tag names found in the application.xml above.

<target name="ejb-lister">
     <xmlproperty file="${my.app.ear}/META-INF/application.xml"/> 
     <echo>#### ${application.module.ejb} ####</echo>
</target>  

Shown below is the output when I execute the “ejb-lister” target. As you can see I get a comma separated list of EJBs declared in the application.xml

ejb-lister:
#### customermanager-ejb.jar, suppliermanager-ejb.jar, ordermanager-ejb.jar, movierentals-ejb.jar####
BUILD SUCCESSFUL (total time: 0 seconds)

The tasks has quite a few parameters (refer ANT doc) especially the delimiter, which can be given if you need to use something other than comma which is the default.

Of course this task can be used to parse any well-formed XML file which in my case happens to be an EAR’s application.xml!

Advertisements

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