Jacoco two ways to generate Java coverage reports

Source: Internet
Author: User


dump coverage Data when shutdown the JVM

1. Download jacoco:http://www.eclemma.org/jacoco/

2. For Tomcat, add in the java_opts configuration of catalina.sh:

-javaagent:/home/work/local/jacoco/lib/jacocoagent.jar=includes=com.xxx.*

Where includes: represents coverage data collection for the specified class, other parameters: Append=true/false: Whether the coverage data collected is appended or replaced, and does not set default to True destfile= Jacoco.exec:dump out of coverage data file Output=file/tcpserver/tcpclient: The form of coverage data output, respectively, for File,tcpserver and Tcpclient,file is a simpler way, TCP can support the development of tools for obtaining coverage data on its own. Do not set default is file.

3. Note that the use of Output=file, the JVM is stopped when the coverage data dump came to the file, after shutdown Tomcat can not kill-9 Java, only kill the Tomcat process, otherwise the data collection is invalid

4. Generating coverage data requires ant execution, build.xml instance: After specifying the exec file for step 3rd dump, executing Antjacoco, the HTML format coverage report is output in the specified path

<?xml version= "1.0"?>

<project name= "testexec" xmlns:jacoco= "antlib:org.jacoco.ant" default= "Jacoco" >

<propertyname= "Srcpath" value= ""/>

<propertyname= "ClassPath" value= ""/>

<propertyname= "Jacocoantpath" value= ""/>

<propertyname= "Jacocoexecpath" value= ""/>

<propertyname= "Workspacepath" value= ""/>

<propertyname= "Reportfolderpath" value= ""/>

<propertyname= "Server_ip" value= ""/>

<propertyname= "Server_port" value= ""/>

<taskdefuri= "Antlib:org.jacoco.ant" resource= "Org/jacoco/ant/antlib.xml" >

<classpathpath= "${jacocoantpath}"/>

</taskdef>

<target name= "Merge" >

<jacoco:mergedestfile= "Merged.exec" >

<filesetdir= "${workspacepath}" includes= "**/*.exec"/>

</jacoco:merge>

</target>

<targetname= "Dump" >

<jacoco:dumpaddress= ' ${server_ip} ' reset= ' false ' destfile= ' ${jacocoexecpath} ' port= ' ${server_port} ' append= ' True "/>

</target>

<targetname= "Jacoco" >

<deletedir= "${reportfolderpath}"/>

<mkdirdir= "${reportfolderpath}"/>

<jacoco:report>

<executiondata>

<filefile= "${jacocoexecpath}"/>

</executiondata>

<structurename= "Jacoco" >

<groupname= "AD" >

<classfiles>

<filesetdir= "${classpath}"/>

</classfiles>

<sourcefilesencoding= "GBK" >

<filesetdir= "${srcpath}"/>

</sourcefiles>

</group>

</structure>

</jacoco:report>

</target>

</project>

Keep JVM dump out coverage Data

When configuring java_opts parameters, modify the following:

-javaagent:/home/work/local/jacoco/jacocoagent.jar=includes=com.xx.*,output=tcpserver,port=10001,address= 100.100.100.100

In this way, after you start Tomcat, Jacoco will provide client access on one port and dump the coverage data file at this time, and the way to dump is still ant execution, which is in Build.xml:

<target name= "Dump" >

<jacoco:dumpaddress= ' ${server_ip} ' reset= ' false ' destfile= ' ${jacocoexecpath} ' port= ' ${server_port} ' append= ' True "/>

</target>

Need to configure server IP and port, execute ant dump, output or exec file, and then execute ant Jacoco will generate HTML report

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.