开发者

failing An ANT build file if the java program it's referring to throws an Exception

I have the following Ant target :

<target name="getArchiverStatus" depends="exportContent">
<java classname="com.test.cms.build.GetErrorCountForArchiver"  failonerror="true">
<classpath>
<pathelement location="${cs.home}/${env}/main/main.jar" /> 
<fileset dir="${cs.home}/${env}/lib" includes="*.jar" />
</classpath>
<arg value="${cs.url}" />
<arg value="${cs.username}" />
<arg value="${cs.password}" />
<arg value="${ucm.archive.name}" />
<arg value="${ucm.workflow.logs.di开发者_如何学Cr}" />
</java>
</target>

I want that this particular target should fail terminating further execution of the build file when the java class GetErrorCountForArchiver has thrown an Exception.Even after using Failonerror= true the next target is getting executed...


Make sure you use the fork=true and failonerror=true. I was running into the same issue but after having those two properties set I got it working as I wanted.

I had simple class

package jonathanmv.tests;
public class ContinuousIntegration {
    public static void main(String[] args) throws Exception {
        throw new Exception("This is supposed to happen");
    }
}

Then a simple ant build file

<project name="Continuous Integration Build" basedir="." default="main">

    <property name="package" value="jonathanmv.tests" />
    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="jar.file" value="${jar.dir}/${package}.jar" />
    <property name="lib.dir" value="lib" />

    <property name="main-class" value="${package}.ContinuousIntegration" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>
    <path id="application" location="${jar.file}" />

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false" />
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.file}" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}" failonerror="true">
            <classpath>
                <path refid="classpath" />
                <path refid="application"/>
            </classpath>
        </java>
    </target>

    <target name="clean-build" depends="clean,jar" />

    <target name="main" depends="clean,run" />

</project>

clean-build and main do not run because the run target fails due to the exception thrown by the main class. Notice that the following line is the one that does the trick in the run target

<!-- ... -->
<java fork="true" classname="${main-class}" failonerror="true">
<!-- ... -->

When I execute ant this is what I get

Buildfile: /home/team/workspace/CI/trunk/build.xml

clean: [delete] Deleting directory /home/team/workspace/CI/trunk/build

compile: [mkdir] Created dir: /home/team/workspace/CI/trunk/build/classes [javac] Compiling 2 source files to /home/team/workspace/CI/trunk/build/classes [copy] Copying 1 file to /home/team/workspace/CI/trunk/build/classes

jar: [mkdir] Created dir: /home/team/workspace/CI/trunk/build/jar [jar] Building jar: /home/team/workspace/CI/trunk/build/jar/jonathanmv.tests.jar

run: [java] Continuous Integration ran [java] 0 [main] INFO jonathanmv.tests.ContinuousIntegration - Continuous Integration > ran [java] Exception in thread "main" java.lang.Exception: This is supposed to happen [java] at jonathanmv.tests.ContinuousIntegration.main(Unknown Source)

BUILD FAILED /home/team/workspace/CI/trunk/build.xml:41: Java returned: 1

Total time: 1 second

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜