Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using the FTP Ant task with maven-antrun-plugin

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

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

<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>
like image 750
mickthompson Avatar asked Apr 19 '10 11:04

mickthompson


People also ask

Can we use Maven and Ant together?

As with any other Maven plugin, to make use of AntRun plugin, we need to define executions. Since we declared our plugin to run during Maven's package phase, running Maven's package goal will execute our plugin configuration above.

Which goal is used to invoke Ant tasks for a Maven build?

The maven-antrun-plugin has only one goal, run . This allows Maven to run Ant tasks.

What is Maven Ant Tasks?

The Maven Ant Tasks allow several of Maven's artifact handling features to be used from within an Ant build. These include: Dependency management - including transitive dependencies, scope recognition and SNAPSHOT handling. Artifact deployment - deployment to a Maven repository (file integrated, other with extensions)

How do I run an Ant script from Maven?

Action. Configure the run goal form the Maven AntRun plugin. Define any properties you wish to pass to the external Ant build, and then call the external Ant build with the ant task, specifying the antfile and target you wish to execute. To execute this external Ant build, run mvn package.


2 Answers

Perhaps you need to think more like Ant and less like Maven in this case.

Here is one solution. Use the ant-contrib trycatch task. Here is an example pom.xml. Copy the code block to a file named pom.xml and run mvn validate to see it work.



<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>
like image 144
Peter Lynch Avatar answered Sep 19 '22 18:09

Peter Lynch


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>
like image 43
Sylvain Avatar answered Sep 20 '22 18:09

Sylvain