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