I am trying to generate the Code coverage report from jacoco.exec file using ant.
My ant build is:
<?xml version="1.0"?>
<project xmlns:jacoco="antlib:org.jacoco.ant" name="Example Ant Build with JaCoCo" default="rebuild">
<description>
Example Ant build file that demonstrates how a JaCoCo coverage report can be itegrated into an existing build in three simple steps.
</description>
<property name="src.dir" location="./java"/>
<property name="result.dir" location="./target"/>
<property name="result.classes.dir" location="./classes"/>
<property name="result.report.dir" location="${result.dir}/site/jacoco"/>
<property name="result.exec.file" location="./jacoco.exec"/>
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="./jacocoant.jar"/>
</taskdef>
<target name="compile">
<mkdir dir="${result.classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false"/>
</target>
<target name="test" depends="compile">
<!--
Step 2: Wrap test execution with the JaCoCo coverage task
-->
<jacoco:coverage destfile="${result.exec.file}">
<java classname="org.jacoco.examples.parser.Main" fork="true">
<classpath path="${result.classes.dir}"/>
<arg value="2 * 3 + 4"/>
<arg value="2 + 3 * 4"/>
<arg value="(2 + 3) * 4"/>
<arg value="2 * 2 * 2 * 2"/>
<arg value="1 + 2 + 3 + 4"/>
<arg value="2 * 3 + 2 * 5"/>
</java>
</jacoco:coverage>
</target>
<target name="report" depends="test">
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!--
This task needs the collected execution data and ...
-->
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}"/>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}"/>
<csv destfile="${result.report.dir}/report.csv"/>
<xml destfile="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>
<target name="rebuild" depends="compile,test,report"/>
</project>
But while compiling the ant build I get the errors related to undefined symbols in the code. How do I remove them?
An example of the error is:
compile:
[javac] Compiling 13 source files to C:\Documents and Settings\user\Desktop\jacoco\classes
[javac] C:\Documents and Settings\user\Desktop\jacoco\java\file.java:13: error: package org.abc.def.ghi.primitives does not exist
[javac] import org.abc.def.ghi.primitives.Request;
These imports are from my code and internally defined but does ant doesn't recognize them. I have already copied all the .java and .class files.
Any pointers will be much appreciated.
This is not a code coverage problem. Your code is failing at the compile step. It's the javac command throwing the error.You need to solve this problem first.
I don't understand what you mean by:
.... I have already copied all the .java and .class files.
Related
I have an ant script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="AntTest" basedir="." default="clean">
<property name="src.dir" value="src"/>
<property name="classes.dir" value="classes"/>
<target name="clean" description="delete all generated files">
<delete dir="${classes.dir}" failonerror="false"/>
<echo message="Hello" />
<delete dir="${ant.project.name}.jar"/>
</target>
<target name="compile" description="compile the task">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" description="create jars of task" depends="compile">
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}" />
</target>
<target name="use" description="use the created jars" depends="jar">
<taskdef name="ntTest" classname="AntTest" classpath="${ant.project.name}.jar" />
<ntTest/>
</target>
</project>
And output is
Buildfile: D:\Work\D3000\AntTest\Build.xml
clean:
[delete] Deleting directory D:\Work\D3000\AntTest\classes
[echo] Hello
compile:
[mkdir] Created dir: D:\Work\D3000\AntTest\classes
[javac] Compiling 1 source file to D:\Work\D3000\AntTest\classes
compile:
jar:
[jar] Building jar: D:\Work\D3000\AntTest\AntTest.jar
compile:
jar:
use:
BUILD FAILED
D:\Work\D3000\AntTest\Build.xml:24: taskdef class AntTest cannot be found
using the classloader AntClassLoader[D:\Work\D3000\AntTest\AntTest.jar]
Total time: 758 milliseconds
Can anybody tell me why this error is coming:
BUILD FAILED D:\Work\D3000\AntTest\Build.xml:24: taskdef class AntTest
cannot be found using the classloader
AntClassLoader[D:\Work\D3000\AntTest\AntTest.jar]
My class file contains class named AntTest
Seems like the classname is wrong, you need the full qualified classname.
instead of :
<taskdef name="ntTest" classname="AntTest" classpath="${ant.project.name}.jar"/>
try :
<taskdef name="ntTest" classname="com.yourdomain.AntTest" classpath="${ant.project.name}.jar"/>
I am using the Ant script to compile my source code. previously it was compiling perfectly . recently i added classes which uses javafx specific classes .after this ant is not compiling and it fails to find the javafx classes . i am using jdk 7 update 23 as javafx is inculded in the jdk, i cannot figure out why compilation fails ?.
below is my ant script.
<?xml version="1.0" encoding="UTF-8" ?>
<project name="client" basedir="." default="compile" >
<description>Client</description>
<property file="build.properties" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="*.*"/>
</path>
<!-- Initialization -->
<target name="init" description="Prepare needed directories.">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${jar.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Cleanup -->
<target name="clean" description="Remove all files created by the build/test process.">
<delete dir="${classes.dir}" />
<delete dir="${dist.dir}" />
<delete dir="${build.dir}" />
</target>
<!-- Compile application -->
<target name="compile" depends="init" >
<mkdir dir="${classes.dir}"/>
<javac source="1.7" target="1.7" srcdir="${src.dir}" destdir="${classes.dir}" debug="yes" includeantruntime="false" fork="true" memorymaximumsize="1200m" >
<classpath refid="classpath" />
</javac>
</target>
<path id="lib.lib">
<fileset dir="../lib">
<include name="**/*"/>
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" lib/">
<path refid="lib.lib"/>
<flattenmapper/>
</pathconvert>
<!-- Java Archive -->
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/Client.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Class-Path" value="lib/${mf.classpath}"/>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
</target>
</project>
Suggested Solution
If you want to use ant with JavaFX, you should use Oracle's JavaFX ant tasks.
The JavaFX runtime is included with Java 7 and the Oracle JavaFX ant tasks are aware of it's location, so when you use the Oracle ant tasks, builds of projects referencing JavaFX work.
Why your current build fails
Compilation fails for your script because the JavaFX runtime (jfxrt.jar) is not on the default class path for Java 7.
For Java 8 the JavaFX runtime is on the class path.
You can still use plain ant without the Oracle JavaFX ant tasks to build your application (just by ensuring jfxrt.jar is on the class path for your build step), however use of the Oracle tasks is recommended as they will also appropriately package your application for distribution.
See also: Compile code using JavaFX 2.0 (using command line)
I have a setup of Selenium WebDriver + TestNG + Ant framework in my automation project. Running webdriver + TestNG tests from Ant using build.xml was working absolutely fine a few months ago. TestNG was generating the test-output folder in the project directory as expected. Now when I run my testng tests from ANT it's generating the default report folder test-output on my Desktop (home/user/Desktop). I don't know why it is happening.
This is my build.xml file:
<project name="InitialConfigProject" default="start" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property file="./app.properties"/>
<property name="ws.home" value="${basedir}"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="browser" value="/usr/bin/google-chrome"/>
<property name="mail_body_file" value="${basedir}/email_body.txt"/>
<property name="buildID" value="IND3.2.0"/>
<property name="sendmailscript_path" value="${basedir}/sendmail.sh"/>
<property name="mail_subject" value="Automated_test_execution_of_${buildID}"/>
<!-- ====== Set the classpath ==== -->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.home}/lib" includes="*.jar"/>
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
</target>
<!-- ============ Initializing other stuff =========== -->
<target name="init" depends="setClassPath">
<tstamp>
<format property="timestamp" pattern="dd/MM/yyyy hh:mm aa" />
</tstamp>
<!--
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition> -->
<property name="build.log.dir" location="${basedir}/buildlogs" />
<mkdir dir="${build.log.dir}"/>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
<echo message="build logged to ${build.log.filename}"/>
<echo message="Loading TestNG.." />
<taskdef name="testng" classpath="${test.classpath}" classname="org.testng.TestNGAntTask" />
</target>
<!-- cleaning the destination folders -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compiling files -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<copy file="${ws.home}/app.properties" todir="${ws.home}/build" />
<copy file="${ws.home}/resources/testdata/testDataSet1.properties" todir="${ws.home}/build" />
<echo message="compiling source files..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.6"
classpath="${test.classpath}"
includeantruntime="true"
>
</javac>
</target>
<!-- run -->
<target name="run" depends="compile">
<testng outputdir="${ws.home}/test-output" classpath="${test.classpath}:${test.dest}" suitename="Praxify Sanity Suite">
<xmlfileset dir="${ws.home}" includes="testng.xml"/>
</testng>
</target>
<!-- ========== Generating reports using XSLT utility ============== -->
<target name="testng-xslt-report">
<delete dir="${basedir}/testng-xslt">
</delete>
<mkdir dir="${basedir}/testng-xslt">
</mkdir>
<xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html"
processor="SaxonLiaison">
<param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />
<param expression="true" name="testNgXslt.sortTestCaseLinks" />
<param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />
<param expression="true" name="testNgXslt.showRuntimeTotals" />
<classpath refid="classpath_jars"></classpath>
</xslt>
</target>
<!-- Starting point of the execution, should be dependent on target "run".
Target sequence will be:
start (not_execute) ==> run (not_execute) ==> compile (not_execute) ==> init (execute) ==> clean (execute)
start (execute) <== testng-xslt-report (execute) <== run (execute) <== compile (execute) <==
Suitable for ANT 1.7. Currently using this ====================== -->
<target name="start" depends="run, testng-xslt-report">
<tstamp prefix="getTime">
<format property="TODAY" pattern="MM-dd-yyyyhhmmaa"/>
</tstamp>
<echo message="sending report as mail...."/>
<property name="execution_time" value="${buildID}_${getTime.TODAY}"/>
<property name="dest_file" value="/home/xtremum/Reports/${execution_time}.zip"/>
<zip destfile="/home/xtremum/Reports/${execution_time}.zip" basedir="${basedir}/testng-xslt"/>
<property name="report_attachment_file" value="${dest_file}"/>
<exec executable="${sendmailscript_path}" newenvironment="false">
<arg value="${mail_subject}"/>
<arg value="${mail_recipient}"/>
<arg value="${report_attachment_file}"/>
<arg value="${mail_body_file}"/>
</exec>
</target>
Just for the record:
1. I am using Eclipse Juno.
2. I have installed TestNG plugin on Eclipse so that I can run tests directly from eclipse by right clicking on testng.xml and going for Run.
3. I have installed ANT 1.7 on my Ubuntu machine and have set my ANT_HOME pointing to /usr/share/ant. And I looked up in Windows -> Preferences -> Ant -> Runtime -> Ant Home Entries (Default) and they seem to have references to ant 1.8.3 libraries (JARS) which are inside the Eclipse package (eclipse/plugins/). Is there anything wrong here?
4. I am running the tests via ant from eclipse and not from command line.
I am not getting any build errors. Tests are getting executed but the test-output folder is getting created on Desktop. Any help?
If you are running through the testng plugin option, the output folder would be the one you specify in Project->Properties->TestNG->OutputDirectory
hOW can i add external jars in my build.xml.
i am getting compilation error while running my build.xml.some jars are missing.how can i add them in my build.xml.
my build.xml looks like this
<project name="HUDSONSTATUSWS" default="dist" basedir=".">
<description>
Web Services build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property name="webcontent" location="WebContent"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="war" depends="compile"
description="generate the distribution war" >
<!-- Create the war distribution directory -->
<mkdir dir="${dist}/war"/>
<!-- Follow standard WAR structure -->
<copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" />
<copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" />
<jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
You just need to specify the classpath for javac, using the classpath or classpathref attribute, or a nested classpath element.
See http://ant.apache.org/manual/Tasks/javac.html for details. The ant documentation is very well written, exhaustive, and full of examples. It's there to be read.
I've got an ant file which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project name="p" default="compile" basedir=".">
<path id="compile.cliClasspath">
<fileset dir="./WebContent/WEB-INF/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="init">
<!-- Create the time stamp -->
<tstamp />
</target>
<target name="compile" description="compile the source" >
<javac srcdir="./src/" destdir="C:\TEMP\build">
<classpath refid="compile.cliClasspath" />
</javac>
</target>
<target name="createWar" depends="compile" description="create web archive">
<war destfile="C:\TEMP\client.war"
webxml="./WebContent/WEB-INF/web.xml"
needxmlfile="true"
basedir="./WebContent"
excludesfile="./WebContent/WEB-INF/application.xml">
<lib dir="./WebContent/WEB-INF/lib" />
<classes dir="C:\TEMP\build" />
</war>
</target>
<target name="createEar" depends="compile, createWar" description="create enterprise archive">
<ear destfile="C:\TEMP\Client.ear"
appxml="./WebContent/WEB-INF/application.xml"
includes="C:\TEMP\Client.war" />
</target>
<target name="cleanUp" depends="compile, createWar, createEar" description="clean up">
<delete includeemptydirs="true">
<fileset dir="C:\TEMP\build" includes="**/*" />
</delete>
</target>
</project>
The idea being to create an ear with the content of my web app. When I run this (run as with order as in the file, from galileo) I get this:
Buildfile: C:\Client-was7.xml
<snip>init:
compile:
[javac] Compiling 47 source files to C:\TEMP\build
compile:
[javac] Compiling 47 source files to C:\TEMP\build
createWar:
[war] Building war: C:\TEMP\Client.war
compile:
[javac] Compiling 47 source files to C:\TEMP\build
createWar:
[war] Building war: C:\TEMP\Client.war
createEar:
compile:
[javac] Compiling 47 source files to C:\TEMP\build
createWar:
[war] Building war: C:\TEMP\Client.war
createEar:
cleanUp:
BUILD SUCCESSFUL
Total time: 15 seconds
Why isn't the output:
init:
compile:
createWar:
createEar:
cleanUp:
BUILD SUCCESSFUL
Total time: 15 seconds
?
Thanks
I think it may have to do with the depends targets. when you run create ear, you first run compile, and then create war, which also depends on compile and so on.