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.
To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.
You can use the maven-antrun-plugin to invoke the ant build. Then use the build-helper-maven-plugin to attach the jar produced by ant to the project. The attached artifact will be installed/deployed alongside the pom. If you specify your project with packaging pom , Maven will not conflict with the ant build.
The maven-antrun-plugin has only one goal, run . This allows Maven to run Ant tasks.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With