是否可以在Ant脚本中调用或执行Maven目标?
假设我有一个名为'distribute'的ant目标,我需要从另一个pom.xml中调用maven'compile'目标。
答案 0 :(得分:31)
由于没有一个解决方案适合我,这就是我提出的:
假设您在Windows上运行:
<target name="mvn">
<exec dir="." executable="cmd">
<arg line="/c mvn clean install" />
</exec>
</target>
或在UNIX上:
<target name="mvn">
<exec dir="." executable="sh">
<arg line="-c 'mvn clean install'" />
</exec>
</target>
或者如果您希望它在UNIX和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>
答案 1 :(得分:21)
使用来自Windows CLI的Maven运行的exec任务的一个示例是:
<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 :(得分:11)
您也可以查看现已退休的maven ant tasks,如下所示。这允许您从ant构建脚本中运行特定的maven目标。您也可以查看this SO question。
答案 3 :(得分:7)
您可以使用java
任务(此示例类似于@ mateusz.fiolka答案,但也适用于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>
使用maven 3.0.5进行测试
答案 4 :(得分:5)
这里有一个完整的解决方案:
<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>
输出是这样的......
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
答案 5 :(得分:1)
我使用了上面this answer的@Adam Siemion,但我不想在Windows或Linux上运行单独的目标。相反,我只是将属性设置在我的蚂蚁脚本的顶部附近:
<condition property="isAntRunningOnWindows">
<os family="windows" />
</condition>
然后在我的脚本中间我使用if-then-else语句:
<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>
答案 6 :(得分:1)
我使用以下目标来运行maven clean
,install
和clean install
目标。工作正常。
<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>
您的maven.home属性应指向maven主目录。例如,
<property name="maven.home" value="D:\\Downloads\\apache-maven-3.0.5"/>
答案 7 :(得分:0)
您可以使用exec task并将mvn compile
作为终端命令。这并不理想,因为您无法控制执行,但我认为没有办法执行Maven目标。
答案 8 :(得分:0)
从Thiagoh回答,我必须添加它才能使其正常工作。
<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>
答案 9 :(得分:0)
plexus
似乎是嵌入Maven的较旧解决方案。
This article paragraph概述了将Maven嵌入Java代码, 没有 本地安装的Maven可执行文件。
答案 10 :(得分:0)
Installation
在项目的根目录中创建一个[
{
"orderId": 1,
"orderNumber": "42253",
"articles": [
{
"articleId": 4,
"name": "Truck",
"labels": [{"labelId": 1, "name": "label1"}]
}
]
},
{
"orderId": 2,
"orderNumber": "42254",
"articles": [{
"articleId": 4,
"name": "Truck",
"labels": [{"labelId": 1, "name": "label1"}]
}
]
},
{
"orderId": 3,
"orderNumber": "42255",
"articles": [
{
"articleId": 2,
"name": "Roads",
"labels": [{"labelId": 1,"name": "label1"}]
},
{
"articleId": 4,
"name": "Truck",
"labels": [{"labelId": 1,"name": "label1"}]
}
]
}]
````
目录,并放置 maven-ant-tasks.jar binaries,sonatype 文件在里面。
lib
{ant} <?xml version="1.0" encoding="UTF-8"?>
<project name="maven" default="build" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property environment="env" />
<!-- <get src="http://search.maven.org/remotecontent?filepath=org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar" dest="lib/maven-ant-tasks-2.1.3.jar"/> -->
<path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" />
<record name="build.log" loglevel="verbose" action="start"/>
<target name="build" description="project build ant script">
<fail unless="env.MAVEN_HOME" message="Environment variable MAVEN_HOME was not found on your system!" />
<artifact:mvn mavenHome="${env.MAVEN_HOME}" fork="true">
<arg value="clean" />
<arg value="install" />
<arg value="-Pqa" />
</artifact:mvn>
</target>
</project>
和typedef
之间这些元素之间的信息很少。
Typedef
:使用taskdef
声明可让您将Ant Tasks的库存储在所需的任何位置(例如源代码控制),并将其放置在构建文件。
typedef
Taskdef
将任务定义添加到当前项目,以便该新任务可以在当前项目中使用。
通过外部将PropertyFiles.java任务添加到taskdef中。
<typedef name="urlset" classname="com.mydomain.URLSet"/>
所有<taskdef name = "propertyfiles"
classname = "org.apache.tools.ant.taskdefs.optional.PropertyFiles" />
在taskdef
属性文件中进行配置。但是我创建了一个外部类,并将其放置在jar文件中。但是,不是在apache-ant-1.9.4\lib\ant.jar\org\apache\tools\ant\taskdefs\defaults.properties
文件中添加条目,而是通过defaults.properties
从外部通知任务。
第二种方法,您可以创建一个jar文件,在一个属性文件中列出所有taskdef
并引用资源
antcontrib您可以将JAR放在您的Ant lib目录中,并设置类路径。
tasksef
答案 11 :(得分:0)
这是旧文章,但仍然有意义(无论如何对我而言)。我认为自原始帖子以来情况发生了变化。事实证明,解决方案非常简单,you don't need all the ant-maven integration fluff。这让我感到奇怪,为什么这不是八年前的答案,因为我很确定它会像现在一样起作用。我应该提到这是针对Windows的。您可能需要对其进行调整,以支持Unix或同时支持Unix。
<target name="mvnPackage" description="Creates war file"> <fail unless="env.MAVEN_HOME" message="Environment variable MAVEN_HOME was not found on your system!" /> <exec executable="${env.MAVEN_HOME}\bin\mvn.cmd"> <arg value="clean" /> <arg value="package" /> </exec> </target>