Friday, September 19, 2008

Ant contrib if/else, foreach, propertyregex

<project ... xmlns:contrib="antlib:net.sf.antcontrib">
<target name="deploy-apps">
<contrib:equals arg1="${appserver}" arg2="weblogic" />
<antcall target="weblogic-deploy-all" />

It means if ${appserver}==weblogic then execute task "weblogic-deploy-all".
To make it work, the ant contrib jar file should be put in the lib directory of ant.

Here is the sample of foreach:
<target name="extractDeploymentFiles">
<contrib:foreach list="${deployFiles}" target="extractDeployment" trim="true" param="deploymentFileName" />
In the properties file, it is

So the first time it will pickup storefront.war and use it as the value of "deploymentFileName" to call another task "extractDeployment".

Here is the sample to use propertyregex. The functionality of my sample here is like substring.
<contrib:propertyregex property="deploymentName" input="${deploymentFileName}" 
regexp="([^\.]*)\.\w" select="\1" casesensitive="true" />
For example, the value of input "deploymentFileName" is storefront.war, then the value of output "deploymentName" will be storefront. The propertyregex divides the input string by the regexp and select="\1" means put the first one into the variable "deploymentName".

No comments:

Post a Comment