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:45

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>
查看更多
forever°为你锁心
3楼-- · 2019-01-22 06:46

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
查看更多
再贱就再见
4楼-- · 2019-01-22 06:49

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>
查看更多
戒情不戒烟
5楼-- · 2019-01-22 06:50

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.

查看更多
放荡不羁爱自由
6楼-- · 2019-01-22 06:52

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

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-22 06:56

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>
查看更多
登录 后发表回答