简体   繁体   中英

Integrating SonarQube with Maven reusing Findbugs, PMD, Checkstyle, Surefire and Jacoco reports

I have a multi-module Java Maven project that already uses Findbugs, Checkstyle, PMD and Jacoco to check the code. Tests are also run during these analyses. I would now like to set up SonarQube to gather the reports generated from the Maven run and display them in SonarQube's dashboard. So far I have these properties in my parent pom.xml that I found in various postings around the internet:

<properties>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.surefire.reportsPath>${project.parent.build.directory}/surefire-reports</sonar.surefire.reportsPath>
    <sonar.jacoco.reportPath>${project.parent.build.directory}/coverage-reports/${project.artifactId}-jacoco.exec</sonar.jacoco.reportPath>
    <sonar.findbugs.reportPath>${project.parent.build.directory}/findbugs/findbugs.xml</sonar.findbugs.reportPath>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
</properties>

I didn't change the Checkstyle, Findbugs, Jacoco or PMD plugin configurations. I added some lines to the Surefire plugin configuration:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <skip>${skip.test}</skip>
                    <testFailureIgnore>${testFailureIgnore}</testFailureIgnore>
                    <argLine>${surefireArgLine}</argLine>
                    <failIfNoTests>${failIfNoTests}</failIfNoTests>
                    <test>AllTests</test>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>org.sonar.java.jacoco.JUnitListener</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>

And these are the Sonar plugins:

            <plugin>
                <groupId>org.codehaus.sonar-plugins.java</groupId>
                <artifactId>sonar-jacoco-listeners</artifactId>
                <version>${sonar-jacoco-listeners.version}</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>${sonar-maven-plugin.version}</version>
            </plugin>

When running this, I get the message "'sonar.dynamicAnalysis' is deprecated since version 4.3 and should no longer be used." But I'm not sure what else to use. The relevant JIRA issue doesn't say so either ( https://jira.codehaus.org/browse/SONAR-5185 ) And besides, it doesn't look as if Sonar is picking up anything much from my Maven run. The dashboard only shows values for "Lines of Code", "Duplications", "Complexity", "Directory Tangle Index" and "Unit Test Success". Judging from the last entry at least the results from the unit tests are picked up.

I am using Maven 3.2.2, sonar-jacoco-listeners 2.9.1, sonar-maven-plugin 2.5 and SonarQube 4.5.1. The tests are run from within a Jenkins job using "mvn -e test".

Any pointers or help would be greatly appreciated. Maybe what I'm trying to achieve simply isn't possible and/or feasible.

(In answer to user944849's comment) This is the sonar output for one module build.

[INFO] [13:39:34.821] -------------  Scan Administration
[INFO] [13:39:34.822] Load module settings
[INFO] [13:39:34.869] Configure Maven plugins
[INFO] [13:39:34.871] Compare to previous analysis (2015-02-25)
[INFO] [13:39:34.872] Compare over 30 days (2015-01-26, analysis of 2015-01-26 05:14:41.328)
[INFO] [13:39:34.873] Compare to previous version (2015-01-16)
[INFO] [13:39:34.874] No quality gate is configured.
[INFO] [13:39:34.889] Initializer FindbugsMavenInitializer...
[INFO] [13:39:34.889] Initializer FindbugsMavenInitializer done: 0 ms
[INFO] [13:39:34.889] Base dir: /mnt/jenkins/jobs/nightlybuild_UVDS_BTH/workspace/trunk/admin
[INFO] [13:39:34.889] Working dir: /mnt/jenkins/jobs/nightlybuild_UVDS_BTH/workspace/trunk/admin/target/sonar
[INFO] [13:39:34.889] Source paths: pom.xml, src/main/java
[INFO] [13:39:34.890] Test paths: src/test/java
[INFO] [13:39:34.890] Binary dirs: target/classes
[INFO] [13:39:34.890] Source encoding: UTF-8, default locale: de_DE
[INFO] [13:39:34.890] Index files
[INFO] [13:39:35.112] 544 files indexed
[INFO] [13:39:42.216] Quality profile for java: DEFAULT
[INFO] [13:39:42.276] Sensor JavaSquidSensor...
[INFO] [13:39:42.303] Java Main Files AST scan...
[INFO] [13:39:42.303] 407 source files to be analyzed
[INFO] [13:39:52.304] 245/407 files analyzed
[INFO] [13:39:58.548] 407/407 source files analyzed
[INFO] [13:39:58.549] Java Main Files AST scan done: 16246 ms
[INFO] [13:39:58.566] Java bytecode scan...
[INFO] [13:39:58.895] Java bytecode scan done: 329 ms
[INFO] [13:39:58.896] Java Test Files AST scan...
[INFO] [13:39:58.896] 137 source files to be analyzed
[INFO] [13:39:59.676] Java Test Files AST scan done: 780 ms
[INFO] [13:39:59.676] 137/137 source files analyzed
[INFO] [13:39:59.677] Package design analysis...
[INFO] [13:40:00.767] Package design analysis done: 1090 ms
[INFO] [13:40:00.788] Sensor JavaSquidSensor done: 18512 ms
[INFO] [13:40:00.796] Sensor QProfileSensor...
[INFO] [13:40:00.797] Sensor QProfileSensor done: 1 ms
[INFO] [13:40:00.797] Sensor InitialOpenIssuesSensor...
[INFO] [13:40:00.839] Sensor InitialOpenIssuesSensor done: 42 ms
[INFO] [13:40:00.839] Sensor ProjectLinksSensor...
[INFO] [13:40:00.843] Sensor ProjectLinksSensor done: 4 ms
[INFO] [13:40:00.843] Sensor VersionEventsSensor...
[INFO] [13:40:00.850] Sensor VersionEventsSensor done: 7 ms
[INFO] [13:40:00.850] Sensor FileHashSensor...
[INFO] [13:40:00.863] Sensor FileHashSensor done: 13 ms
[INFO] [13:40:00.863] Sensor SurefireSensor...
[INFO] [13:40:00.863] parsing /mnt/jenkins/jobs/nightlybuild_UVDS_BTH/workspace/trunk/uvds-admin/target/surefire-reports
[INFO] [13:40:00.920] Sensor SurefireSensor done: 57 ms
[INFO] [13:40:00.921] Sensor Maven dependencies...
[INFO] [13:40:01.086] Sensor Maven dependencies done: 165 ms
[INFO] [13:40:01.090] Sensor CPD Sensor (wrapped)...
[INFO] [13:40:01.090] JavaCpdEngine is used for java
[INFO] [13:40:01.101] Cross-project analysis disabled
[INFO] [13:40:02.295] Sensor CPD Sensor (wrapped) done: 1205 ms
[INFO] [13:40:02.432] Execute decorators...

Here is a POM with a configuration for SonarQube 4.5.1 that works for me, at least for Surefire and JaCoCo (see the results ).

The project is run with mvn clean install -Dsonar .

I don't use FindBugs in my project..

Thank you for your answers. Sonar now picks up the code coverage. The problem was the path I provided for the Jacoco exec files. I used the variable parent.project.build.directory. This didn't work. I'm now storing the exec files in the projects' build directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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