Welcome to Srini's blog

Tuesday, July 27, 2010

Call MAVEN task from ANT

Few days back I got a requirement like call maven task from ant script. For this I wrote one shell script and in which I defined maven task(build war). From ant I called this script using 'exec' cmd, see below

<target>
exec executable="/bin/bash">
<arg value="${src.dir}/SAMPLE_MAVEN.sh"/>
</exec>

</target>



Today I came to know that we can call maven task from ant using 'marcodef', Its very easy to use and its worked for me . See the equavalent macrodef script for the above ant script

<target name="build.sw" depends="env, init, prepare">
<maven basedir="${pom.dir}" goal="clean" resultproperty="maven.build.result" options="-e -Dspring.root=${firware.build.root} -Dbuild.for.prod=false -Djavac.debug=true -Djavac.optimize=false"/>
<maven basedir="${pom.dir}" goal="package" resultproperty="maven.build.result" options="-e -Dspring.root=${firware.build.root} -Dbuild.for.prod=false -Djavac.debug=true -Djavac.optimize=false"/>
<
/target>

<!--
This is the macro definition for maven Ant task
-->
<macrodef name="maven">
<attribute name="options" default="" />
<attribute name="goal" />
<attribute name="basedir" />
<attribute name="resultproperty" default="maven.result" />
<element name="args" implicit="true" optional="true" />
<sequential>
<java classname="org.codehaus.classworlds.Launcher" fork="true" dir="@{basedir}" resultproperty="@{resultproperty}">
<jvmarg value="-Xmx512m" />
<classpath>
<fileset dir="${{maven.src.dir}/boot">
<include name="*.jar" />
</fileset>
<fileset dir="${{maven.src.dir}/lib">
<include name="*.jar" />
</fileset>
</classpath>
<sysproperty key="classworlds.conf" value="${{maven.src.dir}/bin/m2.conf" />
<sysproperty key="maven.home" value="${maven.src.dir}" />
<arg line="--batch-mode @{options} @{goal}" />
</java>
</sequential>
</macrodef>


From the above scratch 'attributes' are basic info about maven tasks.
Options : here we need to provide command line options to build task.
Goal : maven goal (clean, package, install)
Basedir : our pom.xml file path
resultproperty : it is optional.

No comments:

Post a Comment