如何使用maven-antrun-plugin有条件地执行任务? [英] How to execute tasks conditionally using the maven-antrun-plugin?

查看:163
本文介绍了如何使用maven-antrun-plugin有条件地执行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据作为参数传递给maven build命令的环境变量执行一些ant命令.

I need to execute some ant commands depending on an environment variable passed in as a parameter to the maven build command.

目前我有3个任务块,只有没有条件的任务块正在执行.

At the moment I have 3 tasks blocks and only the tasks block with no condition is being executed.

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

我在做什么错,有更好的方法吗?

What am I doing wrong, is there a better way to do this?

推荐答案

首先,根据Maven 1.x网站,maven 1.x的当前稳定版本是1.1版,而不是1.4版.其次,没有AntRun插件1.7版,据我所知,这是一个Maven 2插件.第三,您使用的语法似乎与使用属性还是关于Maven 2的.

First, according to Maven 1.x website, the current stable release for maven 1.x is version 1.1, not 1.4. Second, there is no AntRun Plugin version 1.7 and, to my knowledge, this is a Maven 2 plugin. Third, the syntax you are using seems very similar to Using Attributes which, again, is about Maven 2.

所以,我可能会遗漏一些东西,但这非常令人困惑,您也许应该在问题中阐明这些要点.

So, I may be missing something but, this is very confusing and you should maybe clarify these points in your question.

无论如何,正如您明确提到的Maven 1,我将尽力回答.如果我还记得的话,我会写一个自定义目标,并使用Jelly的 core:if core:when .为此,请在maven.xml中提供以下内容:

Anyway, as you explicitly mentioned Maven 1, I'll try to answer. If I remember well, I would write a custom goal and use Jelly's core:if or core:when. To do so, provide something like this in 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>

我真的不确定语法,所有这些Maven 1东西都太远了,我没有对其进行测试(我懒得安装Maven 1).但是我想你会的. 脚本参考可能会对您有所帮助.

I'm really not sure of the syntax, all this Maven 1 stuff is just too far away, and I didn't test it (I'm too lazy to install Maven 1). But I guess you will. The scripting reference may help you.

说实话,我真的希望您有充分的理由更喜欢Maven 1.x而不是Maven 2.x:)

To be honest, I really hope you have a good reason to prefer Maven 1.x over Maven 2.x :)

更新:看来OP实际上正在使用Maven 2,因此我将相应地更新我的问题.要实现所需的行为,您可以使用Ant-contrib的 if 任务如下图所示:

UPDATE: It appears that the OP is actually using Maven 2 so I'll update my question accordingly. To implement the desired behavior, you could use Ant-contrib's if task as shown below:

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                  classpathref="maven.plugin.classpath" />
                <if>
                  <equals arg1="${foo}" arg2="bar" />
                  <then>
                    <echo message="The value of property foo is bar" />
                  </then>
                  <else>
                    <echo message="The value of property foo is not bar" />
                  </else>
                </if>
              </tasks>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>20020829</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

然后调用mvn compile -Dfoo=bar(这只是一个示例).

And then call mvn compile -Dfoo=bar (this is just an example).

但是,这不是做事情的行之有效的方法".现在,我对您要尝试做的事情有了更好的了解(但由于您没有解释您的最终目标而不能完全理解),我认为使用

But, all this is not the "maven way" to do things. Now that I understand a bit better what you are trying to do (but not entirely as you didn't explain your ultimate goal), I think that using build profiles would be more appropriate and, having read your own answer, I think that you are over complicating things (and that you are on the wrong path).

我了解您是Maven的初学者,但我建议您尝试使用它,尽管不要依赖于Ant,否则您将无法从中受益.另外,在提出问题时,您无需解释特定的解决方案,而应该解释一般性的问题,这样您会得到更好的答案.在这里,由于我不知道您真正想要实现的目标,因此我无法提供更多指导.

I understand that you are a Maven beginner but I'd suggest to try to use it though instead of falling back on Ant or you won't get the benefits of it. Also, when opening a question, instead of asking for a specific solution, you should rather explain your general problem, you'll get better answers. Here, I can't provide more guidance as I don't know what you are really trying to achieve.

这篇关于如何使用maven-antrun-plugin有条件地执行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆