I have adjusted Sonar part to show project fake test coverage - giving mock "jacoco.exec" file, so I need to have real file as a result of project analyse with JaCoCo.
I have Ant "build.xml" and try to implement two approaches for getting goal (both are described here http://eclemma.org/jacoco/trunk/doc/ant.html):
Using Ant's Java agent I have adjusted JVM option
-agentvmparam:${somepath}\\lib\\jacocoagent.jar=destfile=D:\\jacoco.exec
and try to execute in build.xml
<project name="project" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:agent property="agentvmparam"/>
Unfortunately, nothing happens.
Using JaCoCo coverage I try to run the following - but java tag is not specified and build failed:
<project name="project" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
<target name="jacoco" description="Code coverage reporting" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage>
<java classname="org.jacoco.examples.HelloJaCoCo" fork="true">
<classpath>
<pathelement location="./bin"/>
</classpath>
</java>
</jacoco:coverage>
<jacoco:coverage>
<junit fork="true" forkmode="once">
<test name="org.jacoco.examples.HelloJaCoCoTest"/>
<classpath>
<pathelement location="./bin"/>
</classpath>
</junit>
</jacoco:coverage>
</target>
Related
I am new to jacoco and trying to add jacoco coverage for a web application. i saw some links and came to know that we have to add javaagent below are the things that i did.
build.xml
<?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="./src"/>
<property name="result.dir" location="./target"/>
<property name="result.classes.dir" location="${result.dir}/classes"/>
<property name="result.report.dir" location="${result.dir}/site/jacoco"/>
<property name="result.exec.file" location="${result.dir}/jacoco.exec"/>
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="../../../lib/jacocoant.jar"/>
</taskdef>
<target name="clean">
<delete dir="${result.dir}"/>
</target>
<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}" includes="*">
<java classname="com.antspringmvc.exp.Math" fork="true" >
<classpath path="${result.classes.dir}"/>
</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="clean,compile,test,report"/>
</project>
I have added the below code to my tomcat:
if ""%1"" == ""stop"" goto skip_agent
set "JAVA_OPTS=%JAVA_OPTS% -javaagent: C:\Users\kk\Desktop\Personal\jars\jacocoagent.jar=destfile=C:\Users\kk\Desktop\Personal\WorkSpace\JacocoExample\target\jacoco.exec,append=true,output=tcpserver,address=localhost,port=8080,includes=*"
i thought after stopping the server the report will be generated but that has not happened.
am i missing something in the configuration
From your build.xml seems that you expect JaCoCo agent to generate jacoco.exec file, however according to
if ""%1"" == ""stop"" goto skip_agent
set "JAVA_OPTS=%JAVA_OPTS% -javaagent: C:\Users\kk\Desktop\Personal\jars\jacocoagent.jar=destfile=C:\Users\kk\Desktop\Personal\WorkSpace\JacocoExample\target\jacoco.exec,append=true,output=tcpserver,address=localhost,port=8080,includes=*"
you configure agent with output=tcpserver, which means quoting documentation at http://www.jacoco.org/jacoco/trunk/doc/agent.html :
The agent listens for incoming connections on the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
I'm solving task related with code coverage analyse using JaCoCo, and I need to describe Ant target to start scanning. Inspite of examples "java" and "junit" tasks highlighted with red color in the following XML:
<target name="jacoco" description="Code coverage reporting" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage destfile="jacoco.exe">
<java classname="org.jacoco.examples.HelloJaCoCo" fork="true">
<classpath>
<pathelement location="./bin"/>
</classpath>
</java>
</jacoco:coverage>
<jacoco:coverage>
<junit fork="true" forkmode="once">
<test name="org.jacoco.examples.HelloJaCoCoTest"/>
<classpath>
<pathelement location="./bin"/>
</classpath>
</junit>
</jacoco:coverage>
But at the same time "java" and "junit" are recognized in any other task. I'm lack of experience with Ant, so what should I adjust to make these tasks work?
You are probably not properly defining the tasks prior to trying to running them.
<project name="Example" xmlns:jacoco="antlib:org.jacoco.ant">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="path_to_jacoco/lib/jacocoant.jar"/>
</taskdef>
...
</project>
I found this on javacoco's Ant task directory. make sure you are performing this prior to attempting to run those tasks
I'm in the process of setting up a project which uses TeamCity to build the project and run the unit tests.
The first has been done successfully and the project builds. However I can't figure out how to set up TeamCity or my build.xml such that I can view the success or failure of each individual test. Although I'm quite sure I'm attacking this from the wrong angle.
Below is my build.xml:
<project>
<property name="test" location="tests"/>
<target name="test">
<junit printsummary="yes" fork="yes" haltonfailure="yes">
<formatter type="plain"/>
<batchtest fork="true">
<fileset dir="${test}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
This post on stack overflow answered my main problem. So I'll close this.
Simply generating the ANT build file from within eclipse did the trick.
Now for the reporting...
I am using the following demonstration script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
When I run this build script (with target default), the error is
build.xml:9: Problem: failed to create task or type antlib:net.sf.antcontrib:if
The pathelement lib/ant-contrib-1.0b3.jar exists, and ant is picking it up. I'm thinking the problem is how I'm using the xmlns. I'm taking this from another example that I have that also doesn't work for me (it works on a particular server, though!), and trying to figure out what the magic sauce is.
Your taskdef where you're adding ant-contrib needs to declare a URI the same as the namespace you're defining and prefixing in the project. Similar to how the taskdef over here works.
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
Well, the error has if on the end, and it's talking about line 9. I think it's a problem with the syntax of this tag:
<deploy:if>
I can't find any documentation on a "deploy:if" tag, or even a "deploy" tag. I think there is no 'deploy' task in Ant - you need to make a 'deploy' target.
How about trying this:
<if>
<isset property="defaultprop"/>
<then>
<antcall target="deploy" />
</then>
</if>
As I read it, this will check the isset, then call the "deploy" target if it's set.
Of course, you need to make the 'deploy' target now :)
I'm completely new to Ant and need to add a couple jars to my classpath, compile a couple .java files, and run a junit test, all in Ant. I've been looking at a few online tutorials and manuals, but can't seem to wrap my head around the entire xml writing process.
All the previously-written code resides in a single directory called XXX.
In XXX there are two jars I need to add to my classpath with export CLASSPATH=$CLASSPATH:jar1:jar2, two java files I compile with javac *.java, one of which contains several junit tests that I run with java org.junit.runner.JUnitCore Tests. The build.xml would reside in XXX as well (I believe).
So far I have the following for just compiling, although I think there's a lot missing.
<?xml version="1.0"?>
<project name="EtlAutomation" default="compile" basedir=".">
<property name="src" value="${basedir}"/>
<target name="compile">
<!-- Compile .java files -->
<javac srcdir="${src}" destdir="${src}"/>
</target>
</project>
What else do I need to add to compile *.java in the current directory? How can I run the export CLASSPATH command, and finally the junit commend?
I'm not asking for anyone to write my code, but it would be appreciated. If anyone knows a good beginner tutorial for a unix environment, that would be awesome. I'm a total beginner with ant so I'll take what I can get.
Here is a previous question addressing this. And this may work for you:
<project name="EtlAutomation" default="compile" basedir=".">
<property name="src" value="${basedir}"/>
<path id="compile.classpath">
<fileset dir="./">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile" >
<javac destdir="${src}" srcdir="${src}">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<junit>
<classpath refid="compile.classpath" />
<test name="TestExample" />
</junit>
</target>
</project>