Section 5.4. Running the Build File


5.4. Running the Build File

That completes the build file that runs the JUnit tests in Project.java. You can see the final version of this file, build.xml in Example 5-1Example 5-1.

Example 5-3. Using Junit ch05/junit/build.xml
<?xml version="1.0" ?> <project default="main">     <property name="message" value="Building the project...." />     <property name="testsOK" value="Tested OK...." />     <property name="src" location="source" />     <property name="output" location="." />     <property name="results" location="results" />     <property name="jars" location="jars" />     <property name="dist" location="user" />     <property name="junit.fork" value="true"/>     <target name="main" depends="init, compile, test, compress, deploy">         <echo>             ${message}         </echo>     </target>        <target name="init">         <mkdir dir="${output}" />         <mkdir dir="${results}" />         <mkdir dir="${jars}" />     </target>        <target name="compile">         <javac srcdir="${src}" destdir="${output}" />     </target>     <target name="test" depends="test1, test2, test3, test4, test5, test6">         <echo>             ${testsOK}         </echo>     </target>     <target name="test1" depends="compile">         <java fork="true"              classname="junit.textui.TestRunner"              classpath="${ant.home}/lib/junit.jar;.">             <arg value="org.antbook.Project"/>         </java>     </target>     <target name="test2" depends="compile">         <junit              printsummary="yes"              errorProperty="test.failed"             failureProperty="test.failed"             fork="${junit.fork}"             haltonfailure="yes">             <formatter type="plain"/>             <classpath path="."/>              <test todir="${results}" name="org.antbook.Project"/>         </junit>         <fail message="Tests failed!" if="test.failed"/>     </target>     <target name="test3" depends="compile">         <junit printsummary="yes" fork="yes" haltonfailure="yes">             <formatter type="brief" usefile="true"/>             <classpath path="."/>              <test todir="${results}" name="org.antbook.Project"/>         </junit>     </target>     <target name="test4" depends="compile">         <junit printsummary="yes" fork="yes" haltonfailure="yes">             <formatter type="xml"/>             <classpath path="."/>              <test todir="${results}" name="org.antbook.Project"/>         </junit>     </target>     <target name="test5" depends="compile">         <junit printsummary="yes" fork="yes" haltonfailure="yes">             <formatter type="xml"/>             <classpath path="."/>              <test todir="${results}" name="org.antbook.Project"/>         </junit>         <junitreport todir="${results}">             <fileset dir="${results}">                 <include name="TEST-*.xml"/>             </fileset>             <report format="frames" todir="${results}"/>         </junitreport>     </target>     <target name="test6" depends="compile">         <junit printsummary="yes" haltonfailure="yes">             <formatter type="brief" usefile="true"/>             <classpath path="."/>              <batchtest todir="${results}">                 <fileset dir="." includes="**/Project.class"/>             </batchtest>         </junit>     </target>     <target name="compress">         <jar destfile="${jars}/Project.jar" basedir="${output}">             <include name="**/*.class"/>         </jar>     </target>     <target name="deploy">         <delete dir="${dist}" />         <mkdir dir="${dist}" />         <copy todir="${dist}">             <fileset dir="${jars}">                 <include name="*.jar"/>             </fileset>         </copy>     </target> </project>

Here's what you see when you run the build file:

%ant Buildfile: build.xml init:     [mkdir] Created dir: /home/ant/ch05/junit/results     [mkdir] Created dir: /home/ant/ch05/junit/jars compile:     [javac] Compiling 1 source file to /home/ant/ch05/junit test1:      [java] ...      [java] Time: 0      [java] OK (3 tests) test2:     [junit] Running org.antbook.Project     [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.01 sec test3:     [junit] Running org.antbook.Project     [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.01 sec test4:     [junit] Running org.antbook.Project     [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.04 sec test5:     [junit] Running org.antbook.Project     [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.04 sec test6:     [junit] Running org.antbook.Project     [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.01 sec [junitreport] Using Xalan version: Xalan Java 2.4.1 [junitreport] Transform time: 1191ms test:      [echo]      [echo]           Tested OK....      [echo] compress:       [jar] Building jar: /home/ant/ch05/junit/jars\Project.jar deploy:     [mkdir] Created dir: /home/ant/ch05/junit/user      [copy] Copying 1 file to /home/ant/ch05/junit/user main:      [echo]      [echo]           Building the project....      [echo] BUILD SUCCESSFUL Total time: 7 seconds

Because all tests ran successfully, the build was allowed to continue on to deployment.



    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net