How to call for a Maven goal within an Ant script?
Solution 1
An example of use of exec task utilizing Maven run from the Windows CLI would be:
<target name="buildProject" description="Builds the individual project">
<exec dir="${source.dir}\${projectName}" executable="cmd">
<arg value="/C"/>
<arg value="${env.MAVEN_HOME}\bin\mvn.bat"/>
<arg line="clean install" />
</exec>
</target>
Solution 2
Since none of the solutions worked for me, this is what I came up with:
Assuming you are running on Windows:
<target name="mvn">
<exec dir="." executable="cmd">
<arg line="/c mvn clean install" />
</exec>
</target>
or on UNIX:
<target name="mvn">
<exec dir="." executable="sh">
<arg line="-c 'mvn clean install'" />
</exec>
</target>
or if you want it to work on both UNIX and Windows:
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isUnix">
<os family="unix" />
</condition>
<target name="all" depends="mvn_windows, mvn_unix"/>
<target name="mvn_windows" if="isWindows">
<exec dir="." executable="cmd">
<arg line="/c mvn clean install" />
</exec>
</target>
<target name="mvn_unix" if="isUnix">
<exec dir="." executable="sh">
<arg line="-c 'mvn clean install'" />
</exec>
</target>
Solution 3
You can also look at maven ant tasks which is now retired though as commented below. This allows you to run specific maven goals from within your ant build script. You can look at this SO question as well.
Solution 4
You may use a java
task (this example is similar to @mateusz.fiolka answer but also works on Linux)
<target name="mvn-install">
<property environment="env" />
<path id="classpath">
<fileset dir="${env.M2_HOME}/boot">
<include name="plexus-classworlds-*.jar" />
</fileset>
</path>
<property name="mvn.mainclass" value="org.codehaus.plexus.classworlds.launcher.Launcher" />
<java classname="${mvn.mainclass}" classpathref="classpath" fork="true" failonerror="true">
<jvmarg value="-Dclassworlds.conf=${env.M2_HOME}/bin/m2.conf" />
<jvmarg value="-Dmaven.home=${env.M2_HOME}" />
<arg value="install" />
</java>
</target>
tested with maven 3.0.5
Solution 5
Here there is a complete solution:
<target name="mvn_windows_setup" if="isWindows">
<property name="mvn.executable" value="cmd" />
<property name="mvn.args" value="/c" />
</target>
<target name="mvn_unix_setup" if="isUnix">
<property name="mvn.executable" value="sh" />
<property name="mvn.args" value="-c" />
</target>
<target name="run-mvn-goals" depends="mvn_windows_setup, mvn_unix_setup">
<exec dir="${basedir}" executable="${mvn.executable}">
<arg line="${mvn.args} 'mvn ${p_goals}'" />
</exec>
</target>
<!-- EXAMPLES OF HOW TO USE -->
<!-- let's say you want to run mvn clean install -->
<target name="mvn-clean-install">
<antcall target="run-mvn-goals">
<param name="p_goals" value="clean install"/>
</antcall>
</target>
<!-- or maybe you want to clean, package but skipping tests -->
<target name="mvn-clean-package-notests">
<antcall target="run-mvn-goals">
<param name="p_goals" value="clean package -DskipTests"/>
</antcall>
</target>
The output is something like this...
Buildfile: /home/.../build.xml
deploy-complete:
deploy-complete:
mvn_windows_setup:
mvn_unix_setup:
run-mvn-goals:
[exec] [INFO] Scanning for projects...
[exec] [INFO]
[exec] [INFO] ------------------------------------------------------------------------
[exec] [INFO] Building wpm 0.0.1-SNAPSHOT
[exec] [INFO] ------------------------------------------------------------------------
[exec] [INFO]
[exec] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ wpm ---
...
...
...
[exec] [INFO] BUILD SUCCESS
[exec] [INFO] ------------------------------------------------------------------------
[exec] [INFO] Total time: 28.817 s
[exec] [INFO] Finished at: 2016-11-14T14:01:34-05:00
[exec] [INFO] Final Memory: 84M/872M
[exec] [INFO] ------------------------------------------------------------------------
BUILD SUCCESSFUL
Total time: 31 seconds
Dunith Dhanushka
Updated on September 23, 2020Comments
-
Dunith Dhanushka over 3 years
Is it possible to call or execute a Maven goal within an Ant script?
Say I have an ant target called 'distribute' and inside which I need to call a maven 'compile' goal from another pom.xml.
-
pankaj almost 8 yearsMy requirement is I have old project which is build on ant and I have new project which is build on maven. I want build maven project from ant using build.xml. I used this and build my project but it did not work for me. My old project build successfully but new project did not.
-
pankaj almost 8 yearsI copy <target name="mvn"> <exec dir="." executable="cmd"> <arg line="/c mvn clean install" /> </exec> this in my build.xml and run ant all through cmd but pom.xml did not build. can you please tell what is wrong ? </target>
-
Adam Siemion almost 8 years@pankaj submit a new question for your problem
-
shonky linux user over 7 yearsMaven Ant Tasks are now retired
-
Payne almost 7 years@pankaj Did you add mvn target as depends or default target? New topic would be nice