8

我需要根据作为参数传入 maven build 命令的环境变量来执行一些 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>

我做错了什么,有没有更好的方法来做到这一点?

4

4 回答 4

12

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

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

无论如何,正如您明确提到的 Maven 1,我将尝试回答。如果我没记错的话,我会写一个自定义目标并使用 Jelly'score: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)。但我想你会的。脚本参考可能会对您有所帮助。

老实说,我真的希望你有充分的理由更喜欢 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(这只是一个例子)。

但是,这一切都不是做事的“行家之道”。既然我对您正在尝试做的事情有了更好的了解(但不完全是因为您没有解释您的最终目标),我认为使用构建配置文件会更合适,并且在阅读了您自己的答案后,我认为您把事情复杂化了(而且你走错了路)。

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

于 2009-12-29T16:55:05.473 回答
7

你不需要在使用AntContrib之后maven-antrun-plugin 1.5使用,<target>而不是<tasks>根据插件使用情况。此标签的工作方式与 相同,但在此标签中,您可以添加如下示例所示的条件。

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

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

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

于 2015-03-11T12:40:12.110 回答
0

通过在项目根目录的 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>
于 2009-12-29T23:46:18.620 回答
0

此处可运行示例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/

于 2017-03-30T21:48:32.623 回答