Maven + Sonar + Jacoco扫描代码覆盖率

I've recently setup and successfully got Sonar and Jacoco running together. Since I'm recent with the topic, I figured I'd check on stackoverflow for similar issues and help out. I am getting results from Jacoco, but found you had to explicitly set the following parameters in addition to the properties you've listed in your post:

sonar.core.codeCoveragePlugin=jacoco
sonar.jacoco.reportPath=tests/jacoco-exec/jacoco.exec
sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=tests/test-reports

You have to set sonar.core.codeCoveragePlugin=jacoco if you want to be able to use the sonar.jacoco.reportPath property. Otherwise, you will have to use the sonar.jacoco.itReportPath property. However, I recommend just setting the codeCoveragePlugin and reportPath properties. Otherwise, it won't display under the default coverage widget in sonar. Please make note, you cannot use the default coverage tool and jacoco together. It has to be one or the other. I decided to use Jacoco.

Your ant target must be configured to generate the jacoco.exec results prior to running the sonar tasks:

<jacoco:coverage enabled="${tests.code.coverage}" destfile="${jacoco.exec.dest}">
  <junit fork="yes" printsummary="withOutAndErr" dir="${tests.working.dir}">
  ...

Be sure to tell sonar to reuse reports and any sunfire reports if you're running junit before sonar, that is if you're running junit outside of sonar:

sonar.dynamicAnalysis=reuseReports
sonar.jacoco.reportPath=tests/jacoco-exec/jacoco.exec
sonar.surefire.reportsPath=tests/test-reports

For whatever reason, if you need more verbose debugging, use the following property:

sonar.verbose=true


Step1 配置settings.xml,加入profile:

<settings>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- EXAMPLE FOR MYSQL -->
                <sonar.jdbc.url>
                  jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
                </sonar.jdbc.url>
                <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
                <sonar.jdbc.username>sonar</sonar.jdbc.username>
                <sonar.jdbc.password>sonar</sonar.jdbc.password>
 
                <!-- optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                  http://myserver:9000
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>
 
Step2. 运行Maven命令:
mvn clean install sonar:sonar -Dmaven.test.failure.ignore=true -Dsonar.branch=@FP-Dev -Dsonar.core.codeCoveragePlugin=jacoco -Dsonar.jacoco.reportPath=tests/jacoco-exec/jacoco.exec -Dsonar.dynamicAnalysis=reuseReports -Dsonar.surefire.reportsPath=tests/test-reports > log.log
原文地址:https://www.cnblogs.com/zhangqingsh/p/2944719.html