仅当设置了属性时,才在Maven中运行Ant任务 [英] Run a Ant task in Maven only if a property is set

查看:118
本文介绍了仅当设置了属性时,才在Maven中运行Ant任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的pom.xml正在运行Ant任务,以使用FTP部署文件.但是,只有在Maven命令中给出-Dftp=true参数(即mvn clean install -Dftp=true)时,才必须进行此部署.因此,我编写了以下代码:

My pom.xml is running an Ant task to deploy a file using FTP. However, this deployment must be only done if the -Dftp=true argument is given in the Maven command (i.e. mvn clean install -Dftp=true). Thus, I wrote the following code:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks if="ftp">
                            <echo message="Deploying files through FTP..."/>
                            ...
                        </tasks>
                    </configuration>
                </execution>
            </executions>

使用此pom.xml,如果我未在Maven命令中定义-Dftp属性,则不会执行Ant任务.但是,如果我为此属性提供任何类型的值,例如-Dftp=false,则将运行Ant任务,这是不正确的.

Using this pom.xml, the Ant task is not executed if I do not define the -Dftp property in my Maven command. However, if I give any kind of value for this property, for example -Dftp=false, the Ant task is run, which is not correct.

如何将AntRun任务配置为仅在给定属性具有给定值时才运行?

How configure the AntRun task to be run only if a given property has a given value?

ps:我知道我可以使用仅在ftp等于true时有效的profile.此解决方案有效,但由于某些原因,我想使用Antrun插件build块.

ps: I know I can use a profile that is only active when ftp is equal to true. This solution works, but for some reason, I want to have my Antrun plugin build block.

<profiles>
    <profile>
        <id>deployment</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>ftp</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    ... (define the Ant task here)

推荐答案

有一个 Ant-contrib 中的> if 任务,您可以使用:

There is an if task in Ant-contrib that you could use:

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

您不需要<else>,这只是出于演示目的.

You don't need the <else>, this was just for demo purpose.

这篇关于仅当设置了属性时,才在Maven中运行Ant任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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