0

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

Godin
  • 9,801
  • 2
  • 39
  • 76
user3927150
  • 61
  • 1
  • 2
  • 9
  • Advice: instead of trying random things and asking tons of slightly different questions here under different usernames (I refer to https://stackoverflow.com/questions/46276784/how-does-jacoco-work-with-ant that contains pretty similar `build.xml`), please take time to carefully study documentation and principles of work of a tools that you're trying to use. – Godin Sep 18 '17 at 20:42
  • If the problem continues despite of applying the workarounds, you may have a look at my answer on [maven jacoco: not generating code coverage report](https://stackoverflow.com/questions/25395255/maven-jacoco-not-generating-code-coverage-report/71661614#71661614). – Murat Yıldız Mar 29 '22 at 12:04

1 Answers1

0

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.

Godin
  • 9,801
  • 2
  • 39
  • 76