预警,这是我第一次尝试创建ANT构建脚本。我正在努力使我的测试目标仅在compiled
属性设置为true时运行,此外我希望我的 dist 目标运行,除非junit.failure
属性设置为true。但由于某种原因,以下代码似乎没有按照我的意图行事;这意味着当我尝试运行ant
时它不会执行测试,因为compiled
未设置为true(我相信),但如果我查看bin文件夹,我可以看到.class文件是确实产生了。因此,这意味着dist将不会运行,因为它依赖于编译和测试目标。关于我可能做错的任何想法或想法?
<project name="project" default="dist" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
<description>
Build file for the project
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="lib" location="lib"/>
<property name="bin" location="bin"/>
<property name="dist" location="dist"/>
<property name="distDirName" value="dist"/>
<!-- including the external jar files -->
<path id="proj.classpath">
<fileset dir="${lib}" includes="*.jar"/>
<pathelement location="${bin}"/>
</path>
<target name="compile" description="compile the source">
<!-- Create the distribution directory -->
<mkdir dir="${bin}"/>
<!-- Compile the java code from ${src} into ${bin} -->
<javac srcdir="${src}" classpathref="proj.classpath" destdir="${bin}"/>
<!-- Set condition for dependent target -->
<condition property="compiled">
<and>
<available file="**/SampleTest.class"/>
<available file="**/DBConnection.class"/>
</and>
</condition>
</target>
<target name="test" depends="compile" if="compiled">
<junit failureproperty="junit.failure" errorproperty="junit.failure" printsummary="withOutAndErr" fork="yes" haltonfailure="true" haltonerror="true">
<formatter type="xml"/>
<test name="this.one.dir.SampleTest" todir="${bin}"/>
<classpath refid="proj.classpath"/>
</junit>
</target>
<target name="dist" depends="compile,test" unless="junit.failure" description="generate the distribution">
<!-- Create the distribution directory -->
<mkdir dir="${dist}"/>
<!-- Put everything in ${bin} into the distribution.jar file -->
<jar jarfile="${dist}/distribution.jar" basedir="${basedir}" excludes="**/test/,${distDirName}/**"/>
</target>
</project>
答案 0 :(得分:0)
您需要评估您的变量,例如使用${junit.failure}
<target name="dist" depends="compile,test" unless="${junit.failure}" description="generate the distribution">
</target>