如果它引用的java程序抛出异常,则ANT构建文件失败

时间:2011-09-26 10:12:05

标签: java ant failonerror

我有以下Ant目标:

<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.dir}" />
</java>
</target>

我希望当java类GetErrorCountForArchiver在使用Failonerror = true之后抛出Exception.Even后,这个特定目标应该无法终止构建文件的进一步执行...下一个目标正在执行...

1 个答案:

答案 0 :(得分:2)

确保使用 fork = true failonerror = true 。我遇到了同样的问题但是在设置了这两个属性之后我就按照自己的意愿运行了。

我有简单的课程

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

然后是一个简单的ant构建文件

<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 main 不运行,因为 run 目标由于主类抛出异常而失败。请注意,以下行是在运行目标

中执行操作的行
<!-- ... -->
<java fork="true" classname="${main-class}" failonerror="true">
<!-- ... -->

当我执行ant时,这就是我得到的

  

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

     

清洁:      [删除]删除目录/ home / team / workspace / CI / trunk / build

     

编译:       [mkdir]创建目录:/ home / team / workspace / CI / trunk / build / classes       [javac]将2个源文件编译为/ home / team / workspace / CI / trunk / build / classes        [copy]将1个文件复制到/ home / team / workspace / CI / trunk / build / classes

     

罐:       [mkdir]创建了dir:/ home / team / workspace / CI / trunk / build / jar         [jar]建筑罐子:/home/team/workspace/CI/trunk/build/jar/jonathanmv.tests.jar

     

运行:        [java]持续集成运行        [java] 0 [main] INFO jonathanmv.tests.ContinuousIntegration - 持续集成&gt;跑        [java]线程“main”中的异常java.lang.Exception:这应该发生        [java] at jonathanmv.tests.ContinuousIntegration.main(Unknown Source)

     

建立失败   /home/team/workspace/CI/trunk/build.xml:41:Java返回:1

     

总时间:1秒