【问题标题】:How to execute tasks conditionally using the maven-antrun-plugin?如何使用 maven-antrun-plugin 有条件地执行任务?
【发布时间】:2009-12-28 22:33:51
【问题描述】:

我需要根据作为参数传入 maven 构建命令的环境变量执行一些 ant 命令。

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

<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并传递参数的吗?
  • 我看对了吗,你的第二个任务没有名字?

标签: maven-2 maven-antrun-plugin


【解决方案1】:

首先,根据 Maven 1.x 网站,目前 maven 1.x 的稳定版本是 1.1 版,而不是 1.4 版。其次,没有 AntRun 插件 1.7 版,据我所知,这是一个 Maven 2 插件。第三,您使用的语法似乎与 Using Attributes 非常相似,同样是关于 Maven 2。

所以,我可能遗漏了一些东西,但这很令人困惑,您也许应该在您的问题中澄清这些要点。

无论如何,正如您明确提到的 Maven 1,我会尝试回答。如果我没记错的话,我会编写一个自定义目标并使用 Jelly 的 core:ifcore:when。为此,请在 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)。但我想你会的。 scripting reference 可以帮助你。

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

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

  <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(这只是一个例子)。

但是,这一切都不是做事的“行家之道”。现在我更好地理解了你想要做什么(但不完全是因为你没有解释你的最终目标),我认为使用 build profiles 会更合适,并且在阅读了你自己的答案后,我认为你把事情复杂化了(而且你走错了路)。

我了解您是 Maven 初学者,但我建议您尝试使用它,而不是依赖 Ant,否则您将无法从中受益。此外,在提出问题时,与其要求特定的解决方案,不如解释您的一般问题,您会得到更好的答案。在这里,我无法提供更多指导,因为我不知道您真正想要实现什么。

【讨论】:

  • 抱歉,对 Maven 来说还是个新手。我将 Maven 2.0.9 与 JDK 1.4 一起使用。
  • 没问题,这样更有意义。我会相应地更新我的答案。
  • 感谢您的帮助帕斯卡。您上面的解决方案看起来正是我想要实现的。我之前无法使用“if”语句,因为我不知道我需要包含一个 ant-contrib 资源。基本上我想要实现的是在应用服务器上创建一个目录结构,这取决于我是部署到开发/测试环境还是生产环境。我刚刚快速浏览了一下构建配置文件,听起来这将是一种更好的做事方式。谢谢!
【解决方案2】:

根据plugin usage,您不需要在使用&lt;target&gt; 而不是&lt;tasks&gt;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>

上面的代码总是执行第一个目标,但是第二个目标依赖于一个属性值。您也可以为父模块和子模块项目配置它,在&lt;pluginsManagement&gt;标签上定义插件并在子模块中调用一些属性,然后调用插件。

更新: 请务必使用不带${}if 参数,因为它可能会导致cmets 部分中提到的问题。

【讨论】:

  • 谢谢!仅当定义了“execute.my.target”并且等于“true”、“on”或“yes”时才会执行“my-target”。
  • 不适用于 1.8。如果我在&lt;configuration&gt; 中放了两个target,插件只会执行最后一个(Intellij IDEA 在 pom.xml 上标记最后一个目标错误)。
  • 如果等于一个值呢??
  • 正如@Dherik 所说,不使用&lt;configuration&gt; 下的多个目标。但是,当您使用每个 &lt;execution&gt; 一个 &lt;target&gt; 定义多个 &lt;execution&gt;s(具有唯一 ID)时有效
【解决方案3】:

通过在项目根目录的 build.xml 文件中创建多个具有“if”属性和条件属性的命名目标来解决此问题,如下所示。

<target name="prod" if="isProd" depends="isProdCheck">
    // do something
</target>

传递了我需要的命令行开关的属性,并从 Maven POM 的任务部分调用 build.xml 文件中的 ant 目标,如下所示:

<tasks>
 <ant antfile="${basedir}/build.xml">
    <property name="environment" value="${environment}"/>        
    <target name="prod"/>
 </ant>          
</tasks>

【讨论】:

  • 无意冒犯,但这是使用 maven 解决问题的可怕方式。
  • 为什么你需要调用一个特定的目标而不是检查你是否调用了正确的目标?您不能根据环境从 maven 调用正确的目标(例如根据环境名称命名的任务)吗?
【解决方案4】:

此处可运行示例https://www.surasint.com/run-ant-with-if-from-maven/

另一种解决方案是:将 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>

然后

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

我在这里放了完整的代码示例,你可以下载https://www.surasint.com/2017/04/09/run-ant-with-if-from-maven/

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多