当前位置: 首页 > 知识库问答 >
问题:

JaCoCo和sonar是如何工作的?

晋弘义
2023-03-14

我试图通过Sonar ut ant JaCoCo runTests示例了解JaCoCo和Sonar是如何协同工作的:https://github.com/SonarSource/sonar-examples/blob/master/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="UT coverage with Ant and JaCoCo running tests" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">

        <!-- ========= Define the main properties of this project ========= -->
        <property name="src.dir" value="src" />
        <property name="test.dir" value="test" />
        <property name="lib.junit.dir" value="lib" />
        <property name="build.dir" value="target" />
        <property name="classes.dir" value="${build.dir}/classes" />
        <property name="reports.dir" value="${build.dir}/reports" />
        <property name="reports.junit.xml.dir" value="${reports.dir}/junit" />

        <!-- Define the SonarQube properties -->
        <property name="sonar.projectKey" value="org.codehaus.sonar:example-ut-ant-jacoco-runTests" />
        <property name="sonar.projectName" value="UT coverage with Ant and JaCoCo running tests" />
        <property name="sonar.projectVersion" value="1.0" />
        <property name="sonar.language" value="java" />
        <property name="sonar.sources" value="${src.dir}" />
        <property name="sonar.tests" value="${test.dir}" />
        <property name="sonar.binaries" value="${classes.dir}" />
        <property name="sonar.sourceEncoding" value="UTF-8" />
        <property name="sonar.junit.reportsPath" value="${reports.junit.xml.dir}" />   

        <!-- The following properties are required to use JaCoCo: -->
        <property name="sonar.dynamicAnalysis" value="reuseReports" />
        <property name="sonar.java.coveragePlugin" value="jacoco" />
        <property name="sonar.jacoco.reportPath" value="target/jacoco.exec" />

        <!-- Add your basic SonarQube configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
        <!--
          <property name="sonar.jdbc.url" value="jdbc:..." />
          <property name="sonar.jdbc.username" value="..." />
          <property name="sonar.jdbc.password" value="..." />
        -->

        <!-- ========= Define "regular" targets: clean, compile, test, ... ========= -->
        <target name="clean">
                <delete dir=".sonar" />
                <delete dir="${build.dir}" />
                <delete dir="${reports.dir}" />
        </target>

        <target name="init">
                <mkdir dir="${build.dir}" />
                <mkdir dir="${classes.dir}" />
                <mkdir dir="${reports.dir}" />
                <mkdir dir="${reports.junit.xml.dir}" />
        </target>

        <target name="compile" depends="init">
                <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="false" />
                <javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="false" />
        </target>


        <path id="classpath">
                <fileset dir="${lib.junit.dir}" includes="*.jar"/>
        </path>

        <target name="test" depends="compile">

                <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
                        <classpath>
                                <path refid="classpath"/>
                        </classpath>
                </taskdef>

                <!-- Import the JaCoCo Ant Task -->
                <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
                        <!-- Update the following line, or put the "jacocoant.jar" file in your "$HOME/.ant/lib" folder -->
                        <classpath path="path/to/jacoco/ant/task/lib/jacocoant.jar" />
                </taskdef>

                <!-- Run your unit tests, adding the JaCoCo agent -->
                <jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
                  <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
                        <classpath location="${classes.dir}" />
                        <classpath refid="classpath" />

                        <formatter type="xml" />
                        <batchtest todir="${reports.junit.xml.dir}">
                          <fileset dir="${test.dir}">
                                <include name="**/*Test.java" />
                          </fileset>
                        </batchtest>
                  </junit>
                </jacoco:coverage>        

        </target>

        <!-- ========= Define SonarQube target ========= -->
        <target name="sonar" depends="compile">
                <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
                        <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
                        <classpath path="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" />
                </taskdef>

                <!-- Execute SonarQube -->
                <sonar:sonar />
        </target>

        <!-- ========= The main target "all" ========= -->
        <target name="all" depends="clean,compile,test,sonar" />

</project>

我在本地机器上安装声纳,默认配置。然后,我运行以下示例:

$ git clone https://github.com/SonarSource/sonar-examples.git
$ cd sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests

# get the two missing libraries:
$ wget http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-ant-task/2.1/sonar-ant-task-2.1.jar
$ wget -Ojacoco-0.6.4.201312101107.zip http://search.maven.org/remotecontent?filepath=org/jacoco/jacoco/0.6.4.201312101107/jacoco-0.6.4.201312101107.zip
$ unzip -p jacoco-0.6.4.201312101107.zip lib/jacocoant.jar >jacocoanto-0.6.4.201312101107.jar

#edit build.xml and replace:
   <classpath path="path/to/jacoco/ant/task/lib/jacocoant.jar" />
   <classpath path="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" />
by
   <classpath path="./jacocoanto-0.6.4.201312101107.jar" />
   <classpath path="./sonar-ant-task-2.1.jar" />


$ ant clean compile
# this creates:
./target/classes/One.class
./target/classes/OneTest.class


$ ant test
[jacoco:coverage] Enhancing junit with coverage
    [junit] objc[50244]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.

# Why I get this warning? is there something misconfigured?

# this creates:
./target/jacoco.exec
./target/reports/junit/TEST-OneTest.xml


$ ant sonar
# this generates these two files, which are just a list of pmd rules (taken from the local sonar installation I guess). in any case, they are not a computation result.
.sonar//pmd-unit-tests.xml
.sonar//pmd.xml

# and sends the results to sonar.
with Unit tests success: 100% (1 tests), Unit Tests coverage: 60.0%

那么,杰科科和声纳是如何合作的呢?

JaCoCo到底是做什么的?

这个./Target/jacoco.exec文件到底包含什么?

声纳如何使用这个文件,声纳到底是做什么的?

60.00%覆盖率的计算结果在哪里?在蚂蚁测试期间蚂蚁声纳期间

蚂蚁声纳的完整记录:

$ ant sonar
Buildfile: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/build.xml

init:

compile:

sonar:
[sonar:sonar] Apache Ant(TM) version 1.9.3 compiled on December 23 2013
[sonar:sonar] Sonar Ant Task version: 2.1
[sonar:sonar] Loaded from: file:/private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/sonar-ant-task-2.1.jar
[sonar:sonar] INFO: Default locale: "en_US", source code encoding: "UTF-8"
[sonar:sonar] INFO: Work directory: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/.sonar
[sonar:sonar] INFO: Sonar Server 4.0
[sonar:sonar] 15:49:08.182 INFO  - Load batch settings
[sonar:sonar] 15:49:08.259 INFO  - User cache: /Users/david/.sonar/cache
[sonar:sonar] 15:49:08.266 INFO  - Install plugins
[sonar:sonar] 15:49:08.523 INFO  - Install JDBC driver
[sonar:sonar] 15:49:08.529 WARN  - H2 database should be used for evaluation purpose only
[sonar:sonar] 15:49:08.529 INFO  - Create JDBC datasource for jdbc:h2:tcp://localhost/sonar
[sonar:sonar] 15:49:09.365 INFO  - Initializing Hibernate
[sonar:sonar] 15:49:11.269 INFO  - Load project settings
[sonar:sonar] 15:49:11.293 INFO  - Apply project exclusions
[sonar:sonar] 15:49:11.405 INFO  - -------------  Scan UT coverage with Ant and JaCoCo running tests
[sonar:sonar] 15:49:11.408 INFO  - Load module settings
[sonar:sonar] 15:49:11.732 INFO  - Quality profile : [name=Sonar way,language=java]
[sonar:sonar] 15:49:11.744 INFO  - Excluded tests: 
[sonar:sonar] 15:49:11.744 INFO  -   **/package-info.java
[sonar:sonar] 15:49:11.809 INFO  - Index files
[sonar:sonar] 15:49:11.827 INFO  - 2 files indexed
[sonar:sonar] 15:49:11.836 INFO  - Loading technical debt model...
[sonar:sonar] 15:49:12.599 INFO  - Loading technical debt model done: 763 ms
[sonar:sonar] 15:49:12.615 INFO  - Configure Maven plugins
[sonar:sonar] 15:49:12.975 INFO  - Compare to previous analysis (2014-01-20)
[sonar:sonar] 15:49:12.990 INFO  - Compare over 30 days (2013-12-21, analysis of 2014-01-16 12:11:07.227)
[sonar:sonar] 15:49:13.142 INFO  - Base dir: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests
[sonar:sonar] 15:49:13.142 INFO  - Working dir: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/.sonar
[sonar:sonar] 15:49:13.143 INFO  - Source dirs: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/src
[sonar:sonar] 15:49:13.143 INFO  - Test dirs: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/test
[sonar:sonar] 15:49:13.143 INFO  - Binary dirs: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/target/classes
[sonar:sonar] 15:49:13.143 INFO  - Source encoding: UTF-8, default locale: en_US
[sonar:sonar] 15:49:13.152 INFO  - Sensor JavaSourceImporter...
[sonar:sonar] 15:49:13.171 INFO  - Sensor JavaSourceImporter done: 19 ms
[sonar:sonar] 15:49:13.171 INFO  - Sensor JavaSquidSensor...
[sonar:sonar] 15:49:13.237 INFO  - Java AST scan...
[sonar:sonar] 15:49:13.242 INFO  - 1 source files to be analyzed
[sonar:sonar] 15:49:13.347 INFO  - 1/1 source files analyzed
[sonar:sonar] 15:49:13.348 INFO  - Java AST scan done: 111 ms
[sonar:sonar] 15:49:13.350 INFO  - Java bytecode scan...
[sonar:sonar] 15:49:13.374 INFO  - Java bytecode scan done: 24 ms
[sonar:sonar] 15:49:13.383 INFO  - Package design analysis...
[sonar:sonar] 15:49:13.394 INFO  - Package design analysis done: 11 ms
[sonar:sonar] 15:49:13.399 INFO  - Sensor JavaSquidSensor done: 228 ms
[sonar:sonar] 15:49:13.399 INFO  - Sensor SurefireSensor...
[sonar:sonar] 15:49:13.400 INFO  - parsing /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/target/reports/junit
[sonar:sonar] 15:49:13.464 INFO  - Sensor SurefireSensor done: 65 ms
[sonar:sonar] 15:49:13.464 INFO  - Sensor CpdSensor...
[sonar:sonar] 15:49:13.464 INFO  - SonarEngine is used
[sonar:sonar] 15:49:13.465 INFO  - Cross-project analysis disabled
[sonar:sonar] 15:49:13.483 INFO  - Sensor CpdSensor done: 19 ms
[sonar:sonar] 15:49:13.483 INFO  - Sensor PmdSensor...
[sonar:sonar] 15:49:13.484 INFO  - Execute PMD 4.3...
[sonar:sonar] 15:49:13.492 INFO  - Java version: 1.5
[sonar:sonar] 15:49:13.512 INFO  - PMD configuration: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/.sonar/pmd.xml
[sonar:sonar] 15:49:14.061 INFO  - PMD configuration: /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/.sonar/pmd-unit-tests.xml
[sonar:sonar] 15:49:14.062 INFO  - Execute PMD 4.3 done: 578 ms
[sonar:sonar] 15:49:14.062 INFO  - Sensor PmdSensor done: 579 ms
[sonar:sonar] 15:49:14.062 INFO  - Sensor InitialOpenIssuesSensor...
[sonar:sonar] 15:49:14.070 INFO  - Sensor InitialOpenIssuesSensor done: 8 ms
[sonar:sonar] 15:49:14.071 INFO  - Sensor ProfileSensor...
[sonar:sonar] 15:49:14.175 INFO  - Sensor ProfileSensor done: 104 ms
[sonar:sonar] 15:49:14.175 INFO  - Sensor ProfileEventsSensor...
[sonar:sonar] 15:49:14.194 INFO  - Sensor ProfileEventsSensor done: 19 ms
[sonar:sonar] 15:49:14.195 INFO  - Sensor ProjectLinksSensor...
[sonar:sonar] 15:49:14.201 INFO  - Sensor ProjectLinksSensor done: 6 ms
[sonar:sonar] 15:49:14.201 INFO  - Sensor VersionEventsSensor...
[sonar:sonar] 15:49:14.213 INFO  - Sensor VersionEventsSensor done: 12 ms
[sonar:sonar] 15:49:14.214 INFO  - Sensor FileHashSensor...
[sonar:sonar] 15:49:14.215 INFO  - Sensor FileHashSensor done: 1 ms
[sonar:sonar] 15:49:14.215 INFO  - Sensor JaCoCoSensor...
[sonar:sonar] 15:49:14.220 INFO  - Analysing /private/tmp/aa/sonar-examples/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/target/jacoco.exec
[sonar:sonar] 15:49:14.289 INFO  - No information about coverage per test.
[sonar:sonar] 15:49:14.289 INFO  - Sensor JaCoCoSensor done: 74 ms
[sonar:sonar] 15:49:14.794 INFO  - Execute decorators...
[sonar:sonar] 15:49:15.196 INFO  - Store results in database
[sonar:sonar] 15:49:15.238 INFO  - ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/org.codehaus.sonar:example-ut-ant-jacoco-runTests
[sonar:sonar] 15:49:15.263 INFO  - Executing post-job class org.sonar.plugins.core.issue.notification.SendIssueNotificationsPostJob
[sonar:sonar] 15:49:15.264 INFO  - Executing post-job class org.sonar.plugins.core.batch.IndexProjectPostJob
[sonar:sonar] 15:49:15.284 INFO  - Executing post-job class org.sonar.plugins.dbcleaner.ProjectPurgePostJob
[sonar:sonar] 15:49:15.295 INFO  - -> Keep one snapshot per day between 2013-12-23 and 2014-01-19
[sonar:sonar] 15:49:15.296 INFO  - -> Keep one snapshot per week between 2013-01-21 and 2013-12-23
[sonar:sonar] 15:49:15.297 INFO  - -> Keep one snapshot per month between 2009-01-26 and 2013-01-21
[sonar:sonar] 15:49:15.297 INFO  - -> Delete data prior to: 2009-01-26
[sonar:sonar] 15:49:15.299 INFO  - -> Clean UT coverage with Ant and JaCoCo running tests [id=1018]
[sonar:sonar] 15:49:15.303 INFO  - <- Clean snapshot 18068

共有1个答案

农雅畅
2023-03-14

通常有一个

当然,没有看到构建是很困难的。xml记录事物的工作原理。

 类似资料:
  • 我有一个使用maven(scala-maven-plugin)构建的scala项目(有几个java文件)。我们为代码覆盖率插入了jacoco(jacoco-maven-plugin),这会生成良好的scala代码覆盖率。我们可以在/target中的典型位置看到html/csv报告,scala覆盖范围很好。 但是,我们无法使用sonar来处理scala文件。插件运行并发送java报道,所以我知道它从

  • 我知道compare and返回一个值。 例如: 方法调用或方法。但是,当compare或返回值时,方法如何排列。在或返回要排序的int值后运行的后台场景是什么?方法如何使用从和返回的int值(或或)

  • 我使用的是jackson 2.10.0(https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.10.0),下面是一个简单的测试用例 Person类定义如下,对于setter,我使用了@JsonSetter注释,而没有使用@JsonGetter作为getter, 然后,我创建一个Person对象,并将

  • 我将用一个例子来问这个问题。假设我有一个这样的项目目录。 首先我想知道我是否把作曲家放在。项目目录中的json文件是否正确,以便在供应商目录中安装库?第二,假设我有以下几行,作曲家。json 那么作曲家将在哪里配置自动加载来查找关于项目根的类samplevendor\包\sampleclass?我问这个问题是在看了symfony2composer.locked文件中的以下几行之后: 更新:我在自动

  • 应用程序具有上下文路径-->/spring-form-simple-project 因此,为了访问,我使用: 这个控制器又返回student.jsp,当提交这个student.jsp时,它用-->@RequestMapping(value=“/AddStudent”,method=RequestMethod.post)调用controller 任何关于这通常如何工作的指示都将是有帮助的。 谢谢!

  • 我很难让JaCoCo和Maven合作。我一直遇到任何一个。 或者 我似乎也无法让JaCoCo只运行,相反,我必须运行 mvn-clean test-JaCoCo:reportor 我尝试了多种编辑POM文件的方法,例如添加destFile和dataFile的配置,以及此处的POM设置:https://howtodoinjava.com/junit5/jacoco-test-coverage/还有这