How to call for a Maven goal within an Ant script?

2019-01-22 06:17发布

问题:

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.

回答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>


回答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>


回答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.



回答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



回答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


回答6:

I used this answer from @Adam Siemion above, but I didn't want separate targets for running on Windows or Linux. Instead I just set the property near the top of my ant script:

<condition property="isAntRunningOnWindows">
    <os family="windows" />
</condition>

Then in the middle of my script I use an if-then-else statement:

<if>
    <equals arg1="${isAntRunningOnWindows}" arg2="true" />
    <then>
        <echo message="OS is Windows" />
        <exec dir="." executable="cmd">
            <arg line="/c mvn clean package" />
        </exec>
    </then>
    <else>
        <echo message="OS is Linux/Unix" />
        <exec dir="." executable="sh">
            <arg line="-c 'mvn clean package'" />
        </exec>
    </else>
</if>


回答7:

I am using the following target to run maven clean, install and clean install goals. Works fine.

<project name="Maven run goals" basedir="."  
         xmlns:artifact="antlib:org.apache.maven.artifact.ant"
         xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">

    <target name="maven clean">
        <artifact:mvn mavenHome="${maven.home}" fork="true">
            <arg value="clean" />
        </artifact:mvn>
    </target>

    <target name="maven install">
        <artifact:mvn mavenHome="${maven.home}" fork="true">
            <arg value="install" />
        </artifact:mvn>
    </target>

    <target name="maven clean-install">
        <artifact:mvn mavenHome="${maven.home}" fork="true">
            <arg value="clean" />
            <arg value="install" />
        </artifact:mvn>
    </target>

</project>

Your maven.home property should point to the maven home directory. For example,

<property name="maven.home" value="D:\\Downloads\\apache-maven-3.0.5"/>


回答8:

You can use the exec task and call mvn compile as a terminal command. This is not ideal since you won't have any control over the execution, but otherwise I don't think there is a way to execute a Maven goal.



回答9:

From Thiagoh answer, I have to add this to make it work.

 <condition property="isWindows">
                    <os family="windows" />
 </condition>

 <condition property="isLinux">
                    <os family="unix" />
 </condition>

<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="isLinux">
    <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>


标签: java ant maven