简体   繁体   English

如何在Ant任务失败时阻止Maven构建失败?

[英]How do I prevent Maven build fail on Ant task failure?

I'm using the FTP Ant task with maven-antrun-plugin 我正在使用maven-antrun-pluginFTP 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>
...

the problem is that my build fails when the folder ${ftp.server.remotedir} doesn't exist. 问题是当$ {ftp.server.remotedir}文件夹不存在时,我的构建失败。
I tried to specify 我试图指明

skipFailedTransfers="true"
ignoreNoncriticalErrors="true

but these don't fix the problem and the build keeps failing. 但这些并没有解决问题,而且构建仍然失败。

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

Do you know how to instruct my maven build to don't care about this Ant task error / or how to instruct Ant to don't fail in the case of a missing directory? 您是否知道如何指示我的maven构建不关心此Ant任务错误/或如何指示Ant在丢失目录时不会失败?

Edit: 编辑:
Peter's solution works. 彼得的解决方案有效。
If you a problem like 如果你有问题的话

[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

Just exclude ant from ant-contrib 只需从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>

Perhaps you need to think more like Ant and less like Maven in this case. 在这种情况下,也许你需要更像Ant,而不是像Maven

Here is one solution. 这是一个解决方案。 Use the ant-contrib trycatch task. 使用ant-contrib trycatch任务。 Here is an example pom.xml. 这是一个示例pom.xml。 Copy the code block to a file named pom.xml and run mvn validate to see it work. 将代码块复制到名为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>

Since maven-antrun-plugin 1.7 you can add in the configuration the tag failOnError 从maven-antrun-plugin 1.7开始,您可以在配置中添加标记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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM