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

2019-01-22 06:19发布

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.

标签: java ant maven
9条回答
疯言疯语
2楼-- · 2019-01-22 06:59

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"/>
查看更多
你好瞎i
3楼-- · 2019-01-22 07:07

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>
查看更多
叼着烟拽天下
4楼-- · 2019-01-22 07:08

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.

查看更多
登录 后发表回答