Category Archives: ANT

Groovy + ANT

Using groovy script in ANT to read XML file, update build.properties with XML content and call another ANT task to for each repeating XML node.

XML file content:

<?xml version = “1.0” encoding = “UTF-8”?>
<BuildConfig CI_BuildConfig.xsd”>
<BuildName>NightlyBuild</BuildName>
<Target_ENV>INTEGRATION</Target_ENV>
<TagArtifacts>false</TagArtifacts>
<Applications>
<ApplicationConfig>
<Name>name1</Name>
<SVN_Path>path1</SVN_Path>
</ApplicationConfig>
<ApplicationConfig>
<Name>name2</Name>
<SVN_Path>path2</SVN_Path>
</ApplicationConfig>
<ApplicationConfig>
<Name>name3</Name>
<SVN_Path>path3</SVN_Path>
</ApplicationConfig>
</Applications>
</BuildConfig>

ANT Build.xml contents:

 

<path id=”groovy.classpath”>
<pathelement location=”${basedir}/lib/groovy-all-2.3.6.jar” />
</path>
<taskdef name=”groovy” classname=”org.codehaus.groovy.ant.Groovy” classpathref=”groovy.classpath”/>

<target name=”build” description=”builds all applications”>
<groovy>

import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

def ant = new AntBuilder()
//file.path is defined in build.properties
File buildfile = new File(“${properties.’file.path’}”)

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(buildfile);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();

XPathExpression expr2 = xpath.compile(“BuildConfig”);
NodeList mnl = (NodeList) expr2.evaluate(doc, XPathConstants.NODESET);

for (int i = 0; i < mnl.getLength() ; i++) {

String buildName = (String)mnl.item(i).getElementsByTagName(“BuildName”).item(0).getChildNodes().item(0).getNodeValue();
String targetEnv = (String)mnl.item(i).getElementsByTagName(“Target_ENV”).item(0).getChildNodes().item(0).getNodeValue();
t = project.createTask(“set”)
t.setDynamicAttribute(“name”, “shipment.env”)
t.setDynamicAttribute(“value”, “${targetEnv}”)
t.perform()

t.setDynamicAttribute(“name”, “BuildName”)
t.setDynamicAttribute(“value”, “${buildName}”)
t.perform()
}

def today = new Date().format(“yyyyMMdd_HHmmss”)
t.setDynamicAttribute(“name”, “build.timestamp”)
t.setDynamicAttribute(“value”, “${today}”)
t.perform()

XPathExpression expr = xpath.compile(“BuildConfig/Applications/ApplicationConfig”);
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

for (int i = 0; i < nl.getLength() ; i++) {
String compName = (String)nl.item(i).getElementsByTagName(“Name”).item(0).getChildNodes().item(0).getNodeValue();
String compPath = (String)nl.item(i).getElementsByTagName(“SVN_Path”).item(0).getChildNodes().item(0).getNodeValue();
t = project.createTask(“set”)
t.setDynamicAttribute(“name”, “applications”)
t.setDynamicAttribute(“value”, “${compName}”)
t.perform()

t.setDynamicAttribute(“name”, “${compName}.path”)
t.setDynamicAttribute(“value”, “${compPath}”)
t.perform()

ant.ant( ){
target(name: ‘app.build’)
}
}

</groovy>
</target>

<macrodef name=”set” >
<attribute name=”name”/>
<attribute name=”value”/>
<sequential>
<propertyfile file=”${basedir}/build.properties”>
<entry key=”@{name}” value=”@{value}”/>
</propertyfile>
</sequential>
</macrodef>

 

Good Luck!