How to force ant print junit's report with colour - java

Below is my build.xml
<target name="tdd" description="Run unittest" depends="jar">
<mkdir dir="${build.test.dir}"/>
<javac srcdir="${test.src.dir}" destdir="${build.test.dir}"
includeAntRuntime="false">
<classpath refid="lib.junit"/>
<classpath refid="lib.dataquery"/>
</javac>
<jar destfile="${build.jar.dir}/dataqueryTDD.jar">
<fileset dir="${build.test.dir}">
<include name="**/*.class"/>
</fileset>
</jar>
<mkdir dir="${build.test.report.dir}"/>
<junit printsummary="yes">
<classpath refid="lib.junit"/>
<classpath refid="lib.dataquery"/>
<classpath refid="lib.dataquery.tdd"/>
<formatter type="xml"/>
<batchtest todir="${build.test.report.dir}">
<fileset dir="${test.src.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
<junitreport todir="${build.test.report.dir}" tofile="TEST-Summary.xml">
<fileset dir="${build.test.report.dir}" includes="*.xml"/>
</junitreport>
</target>
I can run them and print reports without errors.
However, ant will only tell me if there is a failed test. I need to manually checkout the test report by command cat. I am wondering how to force ant dumping the failed test report(s) on stdout with colour.
Thanks

Try the following:
<!-- new testwithcat target -->
<target name="testwithcat" depends="tdd, catreport"/>
<target name="tdd" description="Run unittest" depends="jar">
<mkdir dir="${build.test.dir}"/>
<javac srcdir="${test.src.dir}" destdir="${build.test.dir}"
includeAntRuntime="false">
<classpath refid="lib.junit"/>
<classpath refid="lib.dataquery"/>
</javac>
<jar destfile="${build.jar.dir}/dataqueryTDD.jar">
<fileset dir="${build.test.dir}">
<include name="**/*.class"/>
</fileset>
</jar>
<mkdir dir="${build.test.report.dir}"/>
<!-- add errorProperty, failureProperty to junit task -->
<junit printsummary="yes" errorProperty="test.failed" failureProperty="test.failed">
<classpath refid="lib.junit"/>
<classpath refid="lib.dataquery"/>
<classpath refid="lib.dataquery.tdd"/>
<formatter type="xml"/>
<batchtest todir="${build.test.report.dir}">
<fileset dir="${test.src.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
<junitreport todir="${build.test.report.dir}" tofile="TEST-Summary.xml">
<fileset dir="${build.test.report.dir}" includes="*.xml"/>
</junitreport>
</target>
<!-- add cat report target that runs if test.failed has been set in junit task -->
<target name="catreport" depends="test" if="test.failed">
<echo message="Test failed - cat the test report"/>
<!-- customise the following to cat your report in colour -->
<exec executable="cat">
<arg value="${build.test.report.dir}/TEST-Summary.xml"/>
</exec>
</target>
Run using:
ant testwithcat
Output:
Test failed - cat the test report
[content of ${build.test.report.dir}/TEST-Summary.xml]
You will need to customise
<exec executable="cat">
<arg value="${build.test.report.dir}/TEST-Summary.xml"/>
</exec>
with your own command to "cat your report in colour".
To get plain text output take a look at Ant JUnit Nice Output Formatter.
You can use
<report format="frames" todir="${build.test.report.dir}"/>
or
<report format="noframes" todir="${build.test.report.dir}"/>
in the junitreport task to generate html output.
You can replace
<exec executable="cat">
<arg value="${build.test.report.dir}/TEST-Summary.xml"/>
</exec>
with a call to display the html in a browser. See Exec for example code to do this.

Related

JUnit Ant ClassNotFoundException

I am getting a java.lang.ClassNotFoundException when I run my target TranslatorWorkflow that is supposed to execute a JUnit test. I am running a build.xml file with the targets: build TranslatorWorkflow. It compiles but fails on the JUnit test immediately.
My TranslatorWorkflow.class file is in {basedir}/bin/testScripts/. My classpath and target are:
classpath:
<path id="classpath">
<fileset dir="${basedir}/lib/binaries" includes="*.jar" />
<pathelement location="${basedir}/bin/testScripts/" />
</path>
TranslatorWorkflow target in my build.xml file:
<target name="TranslatorWorkflow">
<mkdir dir="${junit.output.dir}" />
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml" />
<test name="testScripts.TranslatorWorkflow" todir="${junit.output.dir}" />
<classpath refid="classpath" />
</junit>
</target>
I attempted to emulate this answer to a similar question by adding the pathelement line shown in my classpath section above, but received the same exception. I've looked at this question as well as it seems like the same deal. I'd imagine there is something super obvious that I'm missing but alas I don't seem to be getting it.
The classpath should reference ${basedir}/bin not ${basedir}/bin/testScripts (i.e. it should reference the root of classes directory, not the package in which the class exists):
<path id="classpath">
<fileset dir="${basedir}/lib/binaries" includes="*.jar" />
<pathelement location="${basedir}/bin/" />
</path>
Dumpcats, try this...
<path id="base.path">
<pathelement path="${sun.boot.class.path}"/>
<pathelement path="${sun.boot.library.path}"/>
<fileset dir="${basedir}">
<include name="**.jar"/>
</fileset>
</path>
And then in the target element ...
<target name="runTest">
<mkdir dir="test_reports"/>
<junit
fork="true"
forkmode="once"
maxmemory="256m">
<formatter type="plain" usefile="false"/>
<formatter type="xml"/>
<classpath refid="base.path"/>
<batchtest
haltonerror="true" haltonfailure="true"
todir="test_reports">
<fileset dir="${test.build}">
<include name="**/*Test*.class"/>
<include name="**/Test*.class"/>
<include name="**/*Test.classp"/>
<!-- Exclusions -->
<exclude name="**/util/*.class"/>
</fileset>
</batchtest>
</junit>
</target>
At least is that is how i manage classpath reference and everything works like a charm.

Ant Classpath Error When Unzipping Input File Java

I'm a beginner with ant and am wondering what this error means. "failed to create task or type classpath" It gives me that error on the line that is unzipping the file.
<project name="Project2" default="run">
<target name="compile" depends="clean">
<javac srcdir="." includeantruntime="false"/>
</target>
<target name="clean">
<delete>
<fileset dir="." includes="**/*.class"/>
</delete>
<delete>
<fileset dir="." includes="META-INF"/>
</delete>
<delete>
<fileset dir="." includes="*.*~"/>
</delete>
</target>
<target name="getInput">
<get src="file.zip" dest="."/>
<unzip src="folder.zip" dest="folder"/>
</target>
<target name="run" depends= "compile, who, getInput" >
<java classname="Driver" fork="yes"/>
<classpath path = "."/>
</target>
<target name="who">
<echo message="Name"/>
</target>
<target name="jar">
<jar destfile="project2.jar" basedir="."/>
</target>
Any help would be appreciated!
this is what you should do:
put the src values to have the same value
check if the source file exists under the project root

ant failing to import R.java

I am working on an ant build file (stand alone from eclipse) to build my android application.
I need to generate the R.java file. I have done so successfully, however, when it tries to build my project from the src directory it complains that it cannot find the imported R.java file. I see it in the gen directory... but it still will not build.
It fails in the build-project target. Fails saying that it "cannot find symbol" and it is reffering to the R.java file. And the line of code it is pointing at is the import line.
Here is my xml file, please assist, thank you! Let me know if you need more information.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="ATB">
<target name="init">
<mkdir dir="../bin/classes"/>
<copy includeemptydirs="false" todir="../bin/classes">
<fileset dir="../src">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="../bin/classes">
<fileset dir="../gen">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="clean" name="cleanall"/>
<target depends="resource-src,build-subprojects,build-project,jar" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true"
debuglevel="${debuglevel}"
destdir="../bin/classes"
source="${source}"
target="${target}"
executable="${env.JAVAHOME}/javac"
fork="true"
>
<src path="../src"/>
<classpath refid="ATB.classpath"/>
</javac>
<javac
debug="true"
debuglevel="${debuglevel}"
destdir="../bin/classes"
source="${source}"
target="${target}"
executable="${env.JAVAHOME}/javac"
fork="true"
>
<src path="../gen"/>
<classpath refid="ATB.classpath"/>
</javac>
</target>
<target depends="init" name="jar">
<jar destfile="../bin/${ant.project.name}">
<fileset dir="../src/">
</fileset>
</jar>
</target>
<target name="resource-src" description="Generate the R.java file for this project's resources.">
<exec executable="${aapt}" failonerror="true">
<arg value="package"/>
<arg value="-f"/>
<arg value="-v"/>
<arg value="-M"/>
<arg path="../AndroidManifest.xml"/>
<arg value="-A"/>
<arg path="../assets"/>
<arg value="-I"/>
<arg path="${android_jar}"/>
<arg value="-m"/>
<arg value="-J"/>
<arg path="../gen"/> <!-- Create R.java in the gen directory -->
<arg value="-S"/>
<arg path="../res"/>
</exec>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects">
<ant antfile="build.xml" dir="${Comms.location}" inheritAll="false" target="clean"/>
<ant antfile="build.xml" dir="${Comms.location}" inheritAll="false" target="build"/>
</target>
</project>
Your script doesn't include {gen} path as compile source. Just add into <javac>:
<src path="../src"/>
<src path="../gen"/>

Ant build failure due to non-loading dependent Task class

Buildfile: /.../build.xml
build:
buildtests:
tests:
BUILD FAILED
/.../build.xml:43: Problem: failed to create task or type junit
Cause: Could not load a dependent class org/apache/tools/ant/Task
It is not enough to have Ant's optional JARs
you need the JAR files that the optional tasks depend upon.
Ant's optional task dependencies are listed in the manual.
Action: Determine what extra JAR files are needed, and place them in one of:
-/Applications/eclipse/plugins/org.apache.ant_1.7.1.v20090120-1145/lib
-/Users/cjwalsh/.ant/lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem.
The commonest cause is a missing JAR.
This is not a bug; it is a configuration problem
Total time: 512 milliseconds
... and I've checked all the recommended directories, and yet I still keep getting this build failure. The specific JAR 'ant.jar', which I've looked into, has the class 'Task', and it is in the '/Applications/eclipse/plugins/org.apache.ant_1.7.1.v20090120-1145/lib' directory. Do I need to manually add this classpath to my build.xml as well? Here is my build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="springapp" basedir="." default="build">
<property file="build.properties"></property>
<property name="src.dir" value="src"></property>
<property name="web.dir" value="war"></property>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"></property>
<property name="name" value="springapp"></property>
<property name="test.dir" value="test"></property>
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${appserver.lib}">
<include name="servlet*.jar"></include>
</fileset>
<pathelement path="${build.dir}"></pathelement>
</path>
<target name="build" description="Compile main source tree java files">
<mkdir dir="${build.dir}" />
<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false"
failonerror="true">
<src path="${src.dir}" />
<classpath refid="master-classpath" />
</javac>
</target>
<target name="buildtests" description="Compile test tree java files">
<mkdir dir="${build.dir}" />
<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
deprecation="false" optimize="false" failonerror="true">
<src path="${test.dir}"></src>
<classpath refid="master-classpath"></classpath>
</javac>
</target>
<target name="tests" depends="build, buildtests" description="Run tests">
<junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed"
showoutput="true">
<classpath refid="master-classpath"></classpath>
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${build.dir}">
<include name="**/*Tests.*" />
</fileset>
</batchtest>
</junit>
<fail if="tests.failed">
tests.failed=${tests.failed}
***********************************************************************
***********************************************************************
**** One or more tests failed! Check the output ... ***************
***********************************************************************
***********************************************************************
</fail>
</target>
<target name="deploy" depends="build" description="Deploy the application">
<copy todir="${deploy.path}/${name}" preservelastmodified="true">
<fileset dir="${web.dir}">
<include name="**/*.*" />
</fileset>
</copy>
</target>
<target name="deploywar" depends="build" description="Deploy application as WAR file">
<war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}">
<include name="**/*.*"></include>
</fileset>
</war>
<copy todir="${deploy.path}" preservelastmodified="true">
<fileset dir=".">
<include name="*.war"></include>
</fileset>
</copy>
</target>
<path id="catalina-ant-classpath">
<fileset dir="${appserver.lib}">
<include name="catalina-ant.jar"></include>
</fileset>
</path>
<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
<classpath refid="catalina-ant-classpath"></classpath>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
<classpath refid="catalina-ant-classpath"></classpath>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
<classpath refid="catalina-ant-classpath" />
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
<classpath refid="catalina-ant-classpath"></classpath>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
<classpath refid="catalina-ant-classpath"></classpath>
</taskdef>
<target name="install" description="Install application in Tomcat">
<install url="${tomcat.manager.url}" username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${name}"
war="${name}" />
</target>
<target name="reload" description="Reload applicatio in Tomcat">
<reload url="${tomcat.manager.url}" username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${name}" />
</target>
<target name="start" description="Start tomcat application">
<start url="${tomcat.manager.url}" username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${name}" />
</target>
<target name="stop" description="Stop Tomcat application">
<stop url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${name}" />
</target>
<target name="list" description="List Tomcat applications">
<list url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}" />
</target>
</project>
Are you starting ant from within Eclipse?
Please check in the menu {Run | External Tools Configuration...} that the Classpath tab contains "Ant Home". After expanding, does it point to valid files?

Ant+Junit works on first run and never again

Hey guys I've been trying all day to get an ant file to automatically build my project. The file (appended below) was on a web page I found and is pretty thorough.
My problem is that it works as long as I don't run the "clean" target. Once the I run the "clean" target the "test" target ceases to work. I get NoClassFound errors on all test classes. Even though it worked earlier.
I'm working on a Macbook (10.5.8) using the latest version of eclipse. This error occurs running the file from eclipse and from a terminal using Ant version 1.7.1
I modified the file slightly to adapt it to my file structure which is as follows:
src/
packageA.packageB/
ClassA.java
...
ClassN.java
unittests/
packageA.packageB/
AllClassTests.java
ClassATest.java
...
ClassNTest.java
lib/
junit-4.7.jar
The ant file is:
<project name="SampleJUnitTests" default="dist" basedir=".">
<description>
DataTypes Build File
</description>
<!-- set global properties for this build -->
<property name="project_name" value="DataTypes"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="lib"/>
<property name="reports" location="reports"/>
<property name="tests" location="unittests"/>
<property name="tmp" location="tmp_file"/>
<!-- the names of various distributable files -->
<property name="jar_name" value="${project_name}.jar"/>
<property name="war_name" value="${project_name}.war"/>
<!-- top level targets -->
<target name="compile" depends="init" description="compile the source code " >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="dist" depends="compile" description="generate the distributable files " >
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/${jar_name}" basedir="${build}"/>
</target>
<target name="clean" description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
<delete dir="${tmp}"/>
</target>
<target name="run-tests" depends="compile" description="run your test suite" >
<junit printsummary="yes" haltonfailure="no" showoutput="yes" tempdir="${tmp}">
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${tests}">
<include name="**/*.java"/>
<exclude name="**/All*Tests.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name ="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}\html\"/>
</junitreport>
</target>
<target name ="run" depends="" description="if this project can be run, run it" >
</target>
<!-- supporting targets -->
<target name="init" description="initialize the build environment" >
<!-- Create the time stamp -->
<tstamp/>
<!-- Create directory structures -->
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
<mkdir dir="${dist}/lib"/>
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/>
<mkdir dir="${tmp}"/>
</target>
<target name="all" depends="clean, test">
</target>
I give up and hope that someone can shine a light on my problem.
Thanks a lot!
You haven't got any Ant target which builds the test classes. You could do this as part of the compile target if you wanted:
<target name="compile" depends="init" description="compile the source code " >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
<javac srcdir="${tests}" destdir="${build}">
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>

Categories