How to use Jacoco to remotely count code coverage for Tomcat services

Source: Internet
Author: User

How to use Jacoco to remotely count the coverage of Tomcat services

When doing software development often encounter do various tests, here is a kind of I encountered: Code coverage test,

This test is very practical and can find many invalid modules and code. HIGHLY RECOMMENDED!!!!!

A lot of information on the Internet is not complete, and there is no detailed configuration process

This article will briefly describe how to use Jacoco to generate coverage reports for remote Tomcat services.

(Note: Using Jacoco to open a remote service port is a security risk.) )


Software Installation

Ant

Jacoco


Remote Tomcat Service configuration

1:sh shutdown.sh shut down the Tomcat service first.

2: Modify the configuration of java_opts in bin/catalina.sh.

#-javaagent: After the installation path with Jacoco # includes= option, select the Service # port= option you want to coverage, select the Port # address= option you want to open, The IP address of the machine where the Tomcat service resides (if you want to perform ant tasks on the same machine as the Tomcat service, you need to change to 127.0.0.1) java_opts= "-javaagent:/path/to/your/jacoco_0.6.4/ lib/jacocoagent.jar=includes=com.baidu.*,output=tcpserver,port=8893,address=10.81.14.77 "

3:sh startup.sh Restart the Tomcat service.

If you find that you started the main program exception, then in Java_opts configuration China add another-xverify:none

Java_opts= "-javaagent:/path/to/your/jacoco_0.6.4/lib/jacocoagent.jar=includes=com.baidu.*,output=tcpserver, Port=8893,address=10.81.14.77-xverify:none "

Local Ant task Configuration

Configure Build.xml.

<?xml version= "1.0"  ?><project name= "Lengyu"  xmlns:jacoco= "Antlib:org.jacoco.ant"  default= "Jacoco" >    <!--Jacoco installation path-->  <property name= " Jacocoantpath " value="/home/work/software/jacoco_0.6.4/lib/jacocoant.jar "/>  <!-- The path to the. exec file is eventually generated, and Jacoco is the-->  <property name= "Jacocoexecpath"  value= "that generates the final report based on this file"/ Home/work/local/hudson_home/workspace/wg_merchant_oc_regression/jacoco.exec "/>    <! --Generate Coverage report path-->  <property name= "Reportfolderpath"  value= "e:/libs/coverage_ant_task/ report/"/>  <!--the IP address of the remote Tomcat service-->  <property name=" Server_ip " value = "10.81.14.77"/>  <!--the port opened by the remote Tomcat service previously configured, as configured above-->  <property name= " Server_port " value=" 8893 "/>  <!--source code path-->  <property name=" Checkordersrcpath " value= "e:/src/ordercenter/ordercenter-biz/src/main/java/"  />  <!--. class file path-->   <property name= "Checkorderclasspath"  value= "e:/src/ordercenter/ordercenter-biz/target/classes/com /baidu/ordercenter/service/impl " />  <!--let Ant know where to find jacoco-->  <taskdef.  uri= "Antlib:org.jacoco.ant"  resource= "Org/jacoco/ant/antlib.xml" >       <classpath path= "${jacocoantpath}"  />  </taskdef>  <!-- The dump task:       access to the target Tomcat service based on the previously configured IP address, and the port number,      . and generate an. exec file. -->  <target name= "Dump" >      <jacoco:dump  address= "${server_ip}"  reset= "false"  destfile= "${jacocoexecpath}"  port= "${server_port}"   Append= "true"/>  </target>    <!--Jacoco task:        according to the formerThe source path of the polygon configuration and the. class file path,       generates the final HTML coverage report based on the. exec file that is generated after the dump. -->  <target name= "Report" >      <delete dir= "${ Reportfolderpath} " />      <mkdir dir=" ${reportfolderPath} "  />            <jacoco:report>           <executiondata>               <file file= "${jacocoexecpath}"  />           </executiondata>                          <structure name= "Jacoco report" >               <group namE= "check order related" >                              < classfiles>                       <fileset dir= "${checkorderclasspath}"  />                   </classfiles>                    <sourcefiles encoding= "GBK" >                       <fileset dir= "${checkOrderSrcpath}"  />                   </sourcefiles>              </group>           </structure>           

Generate Coverage reports

Execute ant dump. If successful, you should have the following output.

[email protected] ant]$ ant dumpbuildfile:/home/work/local/hudson_home/workspace/wg_merchant_oc_regression/ant/ Build.xmldump:[jacoco:dump] connecting To/10.81.14.77:8893[jacoco:dump] dumping execution data to/home/work/local/ Hudson_home/workspace/wg_merchant_oc_regression/jacoco.execbuild successfultotal time:0 seconds

Finally, the ant report is executed. Jacoco will generate the coverage report on the path you specify.

Appendix

More about jacocoagent and the various tasks you can refer to the official documentation

Jenkins ' Jacoco plugin can generate coverage reports directly from the. exec file, generate graphs in Jenkins, and so on. In that case, the ant report won't work.


How to use Jacoco to remotely count code coverage for Tomcat services

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.