仅当设置了属性时才在 Maven 中运行 Ant 任务

发布于 2024-08-22 22:39:15 字数 1875 浏览 6 评论 0原文

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

<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 任务就会运行,这是不正确的。

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

ps:我知道我可以使用仅当 ftp 等于 true 时才处于活动状态的 profile。这个解决方案有效,但由于某种原因,我想要我的 Antrun 插件 build 块。

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

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>

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.

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反目相谮 2024-08-29 22:39:15

if 任务位于 Ant-contrib 您可以使用:

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

您不需要 >,这仅用于演示目的。

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>

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

江城子 2024-08-29 22:39:15

使用 maven-antrun-plugin:1.8 可以在中指定属性配置是否执行 Ant 任务,具体取决于 Maven 中所述的某些条件antrun 插件文档

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <target if="ftp">
            <echo message="To run, just call mvn package -Dftp=true"/>
          </target>
        </configuration>
      </execution>
    </executions>
  </plugin>

按照您的要求,但使用;而不是已弃用的

With maven-antrun-plugin:1.8 You can specify attributes in the <target/> configuration to execute or not Ant tasks depending some conditions as described in Maven antrun plugin documentation

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <target if="ftp">
            <echo message="To run, just call mvn package -Dftp=true"/>
          </target>
        </configuration>
      </execution>
    </executions>
  </plugin>

As you requested, but using <target/> instead of deprecated <tasks/>

水中月 2024-08-29 22:39:15

如果您不喜欢 Ant-contrib 中的 IF 语法,您可以使用 antelopetasks。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <inherited>false</inherited>
    <configuration>
        <target>
            <taskdef name="if" classname="ise.antelope.tasks.IfTask"/>

            <if name="maven.ant.target">
                <ant target="${maven.ant.target}"/>
                <else>
                    <fail message="Please specify a target to execute in 'maven.ant.target' property" />
                </else>
            </if>
        </target>
    </configuration>
    <dependencies>
        <!-- http://antelope.tigris.org/nonav/docs/manual/bk03.html -->
        <dependency>
            <groupId>org.tigris.antelope</groupId>
            <artifactId>antelopetasks</artifactId>
            <version>3.2.10</version>
        </dependency>
    </dependencies>
</plugin>

In case you don't like IF syntax in Ant-contrib you can use antelopetasks.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <inherited>false</inherited>
    <configuration>
        <target>
            <taskdef name="if" classname="ise.antelope.tasks.IfTask"/>

            <if name="maven.ant.target">
                <ant target="${maven.ant.target}"/>
                <else>
                    <fail message="Please specify a target to execute in 'maven.ant.target' property" />
                </else>
            </if>
        </target>
    </configuration>
    <dependencies>
        <!-- http://antelope.tigris.org/nonav/docs/manual/bk03.html -->
        <dependency>
            <groupId>org.tigris.antelope</groupId>
            <artifactId>antelopetasks</artifactId>
            <version>3.2.10</version>
        </dependency>
    </dependencies>
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文