简体   繁体   English

在Tomcat上使用JaCoCo Java代理获取我的应用程序的代码覆盖率

[英]Getting code coverage of my application using JaCoCo Java agent on Tomcat

I want to measure the code coverage of integration tests using the JaCoCo and Sonar tools. 我想使用JaCoCo和Sonar工具测量集成测试的代码覆盖率。

For that, I start my Tomcat 5.5 configured with the JaCoCo agent in order to get the dump file from JaCoCo. 为此,我启动了使用JaCoCo代理配置的Tomcat 5.5,以便从JaCoCo获取转储文件。

Thus, I set the JAVA_OPTS for that: 因此,我为此设置了JAVA_OPTS

set JAVA_OPTS=-Xrs -XX:MaxPermSize=256m -XX:PermSize=256m -XX:NewRatio=3 -Xms512m -Xmx1024m -XX:+UseParallelGC -javaagent:C:\dev\servers\jacoco-agent.jar=destfile=C:\dev\servers\jacoco.exec,append=true,includes=my.application.*

When I start Tomcat, the C:\\dev\\servers\\jacoco.exec file is generated, but no data is filled. 当我启动Tomcat时,会生成C:\\dev\\servers\\jacoco.exec文件,但不会填充任何数据。

Is there something I forgot in the configuration of my server? 我的服务器配置中有没有忘记的东西?

Regards. 问候。

I realize this may not have been an option 2 years ago when this question was asked, but presently you have some other options available to fetch the JaCoCo execution data without shutting down Tomcat (or any JVM instrumented with the JaCoCo java agent). 我意识到这可能不是2年前问这个问题时的一个选项,但是目前你还有一些其他选项可以获取JaCoCo执行数据而无需关闭Tomcat(或任何使用JaCoCo java代理程序的JVM)。

First take a look at the current documentation for the JaCoCo Java Agent: http://www.eclemma.org/jacoco/trunk/doc/agent.html 首先看一下JaCoCo Java Agent的当前文档: http//www.eclemma.org/jacoco/trunk/doc/agent.html

You can use the output=tcpserver option on the JaCoCo agent to have the Java agent listen for commands. 您可以使用JaCoCo代理上的output = tcpserver选项让Java代理侦听命令。 You can set address= * to have the tcpserver listen on all interfaces, and you can set the port=6300 argument to choose the port where the tcpserver should listen. 你可以设置address = *让tcpserver监听所有接口,你可以设置port = 6300参数来选择tcpserver应该监听的端口。

Through the tcpserver the JaCoCo java agent can be instructed to send you the data whenever you ask for it. 通过tcpserver,可以指示JaCoCo java代理在您请求时向您发送数据。

If your JVM is currently exposing JMX you have another option which you can utilize without opening additional ports. 如果您的JVM当前正在公开JMX,那么您可以使用另一个选项,而无需打开其他端口。 By setting the jmx=true option the JaCoCo java agent exposes an MBean which you can interact with. 通过设置jmx = true选项,JaCoCo java代理公开了一个可以与之交互的MBean。

If you are using maven you can take a look at the plugin I recently wrote in order to gather JaCoCo data from remote JVM's while running. 如果您正在使用maven,您可以查看我最近编写的插件,以便在运行时从远程JVM收集JaCoCo数据。 The project for the plugin is located at: 该插件的项目位于:
https://github.com/mattcj/jacocotogo https://github.com/mattcj/jacocotogo

据我记忆 - 在关闭Tomcat期间会填充文件。

Besides the maven solution, you can also consider https://www.eclemma.org/jacoco/trunk/doc/cli.html 除了maven解决方案,您还可以考虑https://www.eclemma.org/jacoco/trunk/doc/cli.html

Basically, you start your service on the remote machine with the javaagent option like (you can change the port number and omit includes if you want to have coverage for all of the classes): 基本上,您可以使用javaagent选项在远程计算机上启动服务(您可以更改端口号,如果要覆盖所有类,则省略包含):

-javaagent:/tmp/jacocoagent.jar=port=36320,destfile=jacoco-it.exec,output=tcpserver,includes=abcd*”

Then connect to the remote machine by providing remote host address or open a tunnel to the remote machine. 然后通过提供远程主机地址或打开到远程计算机的隧道连接到远程计算机。 The following example assumes I have set up a port forwarding between local host's 36320 and remote host's 36320 以下示例假设我已在本地主机36320和远程主机36320之间设置了端口转发

java -jar jacococli.jar dump --port 36320 --destfile /tmp/jacoco-it.exec

If you have multiple .exec files, you need to merge them: 如果您有多个.exec文件,则需要合并它们:

java -jar jacococli.jar merge /tmp/jacoco-it-1.exec /tmp/jacoco-it-2.exec --destfile /tmp/merge

Then generate the html report (path1 can be a path to the jar file or the class files folder) 然后生成html报告(path1可以是jar文件或类文件文件夹的路径)

java -jar jacococli.jar report /tmp/jacoco-it.exec --classfiles path1 --sourcefiles path2 --html /tmp/report

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM