1

我有以下 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之后,下一个目标也将被执行......

4

1 回答 1

2

确保使用fork=truefailonerror=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-buildmain不会运行,因为运行目标由于主类抛出的异常而失败。请注意,以下行是在运行目标中执行技巧的行

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

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

构建文件:/home/team/workspace/CI/trunk/build.xml

clean: [delete] 删除目录 /home/team/workspace/CI/trunk/build

compile: [mkdir] Created dir: /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

jar:[mkdir] 创建目录:/home/team/workspace/CI/trunk/build/jar [jar] 构建 jar:/home/team/workspace/CI/trunk/build/jar/jonathanmv.tests.jar

运行:[java] 持续集成运行 [java] 0 [main] INFO jonathanmv.tests.ContinuousIntegration - 持续集成 > 运行 [java] 线程“main”中的异常 java.lang.Exception:这应该发生在 [java] jonathanmv.tests.ContinuousIntegration.main(未知来源)

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

总时间:1秒

于 2013-08-01T16:26:58.493 回答