7

我正在使用带有maven-antrun-plugin的FTP Ant 任务

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>ftp</id>
            <phase>generate-resources</phase>
            <configuration>
                <tasks>
                    <ftp action="get"
                         server="${ftp.server.ip}"
                         userid="${ftp.server.userid}"
                         password="${ftp.server.password}"
                         remotedir="${ftp.server.remotedir}"
                         depends="yes" verbose="yes"
                         skipFailedTransfers="true"
                         ignoreNoncriticalErrors="true">
                        <fileset dir="target/test-classes/testdata">
                            <include name="**/*.html" />
                        </fileset>
                    </ftp>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
...

问题是当文件夹 ${ftp.server.remotedir} 不存在时我的构建失败。
我试图指定

skipFailedTransfers="true"
ignoreNoncriticalErrors="true

但是这些并不能解决问题,并且构建不断失败。

An Ant BuildException has occured: could not change remote directory: 550 /myBadDir: The system cannot find the file specified.

你知道如何指示我的 maven 构建不关心这个 Ant 任务错误/或者如何指示 Ant 在缺少目录的情况下不要失败吗?

编辑:
彼得的解决方案有效。
如果你有问题

[INFO] Error configuring: org.apache.maven.plugins:maven-antrun-plugin. Reason: java.lang.NoSuchMethodError: org.apache.tools.ant.util.FileUtils.close(Ljava/io/InputStream;)V

只需从 ant-contrib 中排除 ant

<dependency>
    <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>${ant-contrib.ver}</version>
    <exclusions>
        <exclusion>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
         </exclusion>
    </exclusions>
</dependency>
4

2 回答 2

8

在这种情况下,也许您需要更像Ant而不是Maven那样思考。

这是一种解决方案。使用ant-contrib trycatch任务。这是一个示例 pom.xml。将代码块复制到一个名为的文件pom.xml并运行mvn validate以查看它的工作情况。



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.q2666794</groupId>
  <artifactId>trycatch</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>trycatch</name>
  <url>http://maven.apache.org</url>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>trycatch</id>
            <phase>validate</phase>
            <configuration>
              <tasks>
                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                <trycatch>
                  <try>
                    <fail>Failing ftp task should go here</fail>
                  </try>
                  <catch>
                    <echo>See the error was caught and ignored</echo>
                  </catch>
                </trycatch>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
            <exclusions>
              <exclusion>
                <artifactId>ant</artifactId>
                <groupId>ant</groupId>
              </exclusion>
            </exclusions>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>
于 2010-04-23T02:03:31.290 回答
2

Since maven-antrun-plugin 1.7 you can add in the configuration the tag failOnError

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>ftp</id>
            <phase>generate-resources</phase>
            <configuration>
                <failOnError>false</failOnError>
                <tasks>
                    <ftp action="get"
                         server="${ftp.server.ip}"
                         userid="${ftp.server.userid}"
                         password="${ftp.server.password}"
                         remotedir="${ftp.server.remotedir}"
                         depends="yes" verbose="yes"
                         skipFailedTransfers="true"
                         ignoreNoncriticalErrors="true">
                        <fileset dir="target/test-classes/testdata">
                            <include name="**/*.html" />
                        </fileset>
                    </ftp>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2016-11-10T16:47:54.140 回答