8

pom.xml正在运行 Ant 任务以使用 FTP 部署文件。-Dftp=true但是,只有在 Maven 命令(即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,如果我没有-Dftp在我的 Maven 命令中定义属性,则不会执行 Ant 任务。但是,如果我为此属性指定任何类型的值,例如-Dftp=false,Ant 任务就会运行,这是不正确的。

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

ps:我知道我可以使用仅在等于profile时才有效的 a 。这个解决方案有效,但出于某种原因,我想要我的 Antrun 插件块。ftptruebuild

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

3 回答 3

10

Ant-contrib中有一个if任务可以使用:

  <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>,这只是为了演示目的。

于 2010-02-24T08:37:45.893 回答
3

使用 maven-antrun-plugin:1.8 您可以在 <target/> 配置中指定属性以执行或不执行 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>

根据您的要求,但使用 <target/> 而不是已弃用的 <tasks/>

于 2015-07-23T06:35:32.867 回答
2

如果你不喜欢 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>
于 2012-05-04T10:22:38.930 回答