Maven 2 如何使用maven antrun插件有条件地执行任务?
我需要根据作为maven build命令的参数传入的环境变量执行一些ant命令 目前,我有3个任务块,只执行没有条件的任务块Maven 2 如何使用maven antrun插件有条件地执行任务?,maven-2,maven-antrun-plugin,Maven 2,Maven Antrun Plugin,我需要根据作为maven build命令的参数传入的环境变量执行一些ant命令 目前,我有3个任务块,只执行没有条件的任务块 <tasks name="isProdCheck"> <condition property="isProd"> <equals arg1="${environment}" arg2="PROD" /> </condition> </tasks> <tasks if="isProd" de
<tasks name="isProdCheck">
<condition property="isProd">
<equals arg1="${environment}" arg2="PROD" />
</condition>
</tasks>
<tasks if="isProd" depends="isProdCheck">
...
</tasks>
<tasks>
... I am the only block executed
</tasks>
...
... 我是唯一被执行的街区
我做错了什么,有更好的方法吗?首先,根据Maven 1.x网站,Maven 1.x目前的稳定版本是1.1版,而不是1.4版。其次,没有AntRun插件版本1.7,据我所知,这是一个Maven 2插件。第三,您使用的语法似乎与Maven 2非常相似 所以,我可能遗漏了一些东西,但是,这非常令人困惑,你应该在你的问题中澄清这些要点 无论如何,正如你明确提到的Maven 1,我将尝试回答。如果我记得很清楚的话,我会写一个自定义目标并使用Jelly's或。为此,请在
maven.xml
中提供如下内容:
<project xmlns:j="jelly:core" xmlns:ant="jelly:ant">
<goal name="my-goal">
<j:if test="${environment == 'PROD'}">
<ant:xxx .../>
</j:if>
</goal>
</project>
然后调用mvncile-Dfoo=bar
(这只是一个示例)
但是,所有这些都不是“马文式”做事方式。现在我对你想要做的事情有了更好的理解(但不完全是因为你没有解释你的最终目标),我认为使用会更合适,并且在阅读了你自己的答案后,我认为你把事情复杂化了(而且你走错了路)
我知道你是一个Maven初学者,但我建议你尝试使用它,而不是依赖Ant,否则你将无法从中获益。此外,当你开始提问时,与其问具体的解决方案,不如解释你的一般问题,这样你会得到更好的答案。在这里,我不能提供更多的指导,因为我不知道你真正想要实现什么 通过在项目根目录中的build.xml文件中创建具有“if”属性和条件属性的多个命名目标,解决了此问题,如下所示
//做点什么
传递我所需的命令行开关的属性,并从Maven POM的tasks部分调用build.xml文件中的ant目标,如下所示:
<tasks>
<ant antfile="${basedir}/build.xml">
<property name="environment" value="${environment}"/>
<target name="prod"/>
</ant>
</tasks>
在
maven antrun plugin 1.5
之后,您不需要使用AntContrib
,该插件使用
而不是
。此标记的工作方式与相同,但在此标记中可以添加如下示例所示的条件
<properties>
<execute.my.target>true</execute.my.target>
</properties>
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>config</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<!-- this target will be executed always -->
<target>
<echo message="Hello there, I'm a simple target" />
</target>
<!--
This target will be executed if and only if
the property is set to true
-->
<target name="my-target" if="execute.my.target">
<echo message="Conditional target..." />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
真的
...
org.apache.maven.plugins
maven antrun插件
配置
包裹
跑
...
上面的代码始终执行第一个目标,但第二个目标取决于属性值。您也可以为父模块和子模块项目配置它,在
标记上定义插件,并在子模块上调用一些属性,然后调用插件
更新:
确保在不使用${}
的情况下使用if
参数,因为它可能会导致注释部分中提到的问题。此处的可运行示例
另一个解决方案是:将ant-contrib-1.0b3.jar保留为一个路径,然后像这样定义它
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
然后
我在这里提供了完整的代码示例,您可以下载抱歉,这对Maven来说是全新的。我在JDK1.4中使用Maven 2.0.9。没问题,这更有意义。我会相应地更新我的答案。谢谢你的帮助。你上面的解决方案看起来正是我想要实现的。我以前无法使用“if”语句,因为我不知道需要包含ant contrib资源。本质上,我试图实现的是在应用服务器上创建一个目录结构,这取决于我是部署到开发/测试环境还是生产环境。我刚刚快速浏览了构建概要文件,听起来这是一种更好的方法。谢谢你能确认一下你是如何调用maven并传递参数的吗?我看对了吗,你的第二个任务没有名字?无意冒犯,但这是用maven解决问题的一种可怕的方式。为什么你需要调用一个特定的目标,然后检查你调用的目标是否正确?你不能根据环境从maven调用正确的目标吗(例如,根据环境名称命名任务)?谢谢!仅当定义了“execute.my.target”且等于“true”、“on”或“yes”时,才会执行“my target”。在1.8中不起作用。如果我在
中放入两个目标,插件只执行最后一个目标(Intellij IDEA在pom.xml上用错误标记最后一个目标)。如果等于一个值呢?正如@Dherik所说,在
下不处理多个目标。但是,当您使用每个
一个
定义多个
(具有唯一的ID)时,它会起作用
<properties>
<execute.my.target>true</execute.my.target>
</properties>
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>config</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<!-- this target will be executed always -->
<target>
<echo message="Hello there, I'm a simple target" />
</target>
<!--
This target will be executed if and only if
the property is set to true
-->
<target name="my-target" if="execute.my.target">
<echo message="Conditional target..." />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="doSomething">
<if>
<equals arg1="${someProp}" arg2="YES" />
<then>
<echo message="It is YES" />
</then>
<else>
<echo message="It is not YES" />
</else>
</if>
</target>