How to integrate pmd and checkstyle using sonar-project.properties

Dear Sonar team,

We are using Sonar to do Java static code check in our jenkins. As you know, there are two ways to implement it, one is using SonarScanner with configuration file sonar-project.properties, the other is way is using Maven(need to do some configuration with pom.xml).

Now we would like to integrate pmd and checkstyle. When using the latter way (Maven), we can refer link [External Analyzer Reports] Report generation for Java / Maven. After running mvn command, file checkstyle-result.xml and pmd.xml can be found under target folder.

However, currently we would like to prefer the former way (with sonar-project.properties) because it will decouple from java project, below is the configuration for pmd and checkstyle.

sonar.projectKey=my-sonar-test
sonar.projectVersion=dev
sonar.sources=src
sonar.java.checkstyle.reportPaths=target/checkstyle-result.xml
sonar.java.pmd.reportPaths=target/pmd.xml

But when running command sonar-scanner, there will be some errors, see below:

ERROR: Failed to import external issues report:C:\..\target\checkstyle-result.xml
java.io.FileNotFoundException: C:\..\target\checkstyle-result.xml (The system cannot find the path specified)

So I guess it can not run pmd and checkstyle by sonar-project.properties, is that right?

Below is the system environment.
SonarQube version 7.6
SonarScanner version 4.0

Thanks a lot for your support!

Best Regards,
Stanley

Stanley,

SonarQube, natively, cannot run PMD and Checkstyle, only consume the reports those tools generate. You’ll need to run those tools to generate the reports prior to your SonarQube analysis. Adding the analysis parameters won’t do that for you.

Colin

1 Like

Hi Colin,

Got it. Thanks!

Best Regards,
Stanley

My project is generating pmd.xml file, but when I analyze the project in SonarQube… the violations are not shown anywhere?
where can i check the violations?

1 Like

Setting sonar.java.checkstyle.reportPaths to the location of the checkstyle result, gives “file not found” - why?

SonarQube 8.9
sonar-maven-plugin:3.6.0.1398:sonar

I run a maven build like this in Jenkins:

/usr/lib/jvm/java-11/bin/java -classpath /opt/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven/boot/* -Dmaven.home=/opt/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven -Dmaven.conf=/opt/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven/conf -DbuildInfoConfig.propertiesFile=/opt/jenkins/workspace/test_pl_adrian-sonar@tmp/artifactory/buildInfo6151924388434472961.properties -DbuildInfoConfig.artifactoryResolutionEnabled=true -Dm3plugin.lib=/opt/jenkins/workspace/test_pl_adrian-sonar@tmp/artifactory/cache/artifactory-plugin/3.4.1 -Dclassworlds.conf=/opt/jenkins/workspace/test_pl_adrian-sonar@tmp/artifactory/classworlds6765446697029947892conf -Dmaven.multiModuleProjectDirectory=/opt/jenkins/workspace/test_pl_adrian-sonar -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -Dsonar.java.checkstyle.reportPaths=**/checkstyle-result.xml org.codehaus.plexus.classworlds.launcher.Launcher -f pom.xml -B clean verify jxr:jxr taglist:taglist checkstyle:checkstyle pmd:pmd sonar:sonar --no-transfer-progress

My intent is to run checkstyle and write a report to its default output ./target/checkstyle-result.xml

The build runs checkstyle before sonar - this message appears in the log:

**07:29:55** [main] INFO org.apache.maven.plugins.checkstyle.CheckstyleViolationCheckMojo - You have 0 Checkstyle violations.

Then later - this:

**07:31:15** [main] WARNING org.sonarsource.scanner.maven.SonarQubeMojo - Checkstyle report not found: /opt/jenkins/workspace/test_pl_adrian-sonar/**/checkstyle-result.xml
**07:31:15** [main] INFO org.sonarsource.scanner.maven.SonarQubeMojo - Sensor Import of Checkstyle issues [java] (done) | time=2ms

I have the checkstyple plugin installed in SQ - so later on …

**07:31:15** [main] INFO org.sonarsource.scanner.maven.SonarQubeMojo - Sensor CheckstyleSensor [checkstyle] 
**07:31:15** [main] INFO org.sonarsource.scanner.maven.SonarQubeMojo - Checkstyle output report: /opt/jenkins/workspace/test_pl_adrian-sonar/target/sonar/checkstyle-result.xml 
**07:31:15** [main] INFO org.sonarsource.scanner.maven.SonarQubeMojo - Checkstyle configuration: /opt/jenkins/workspace/test_pl_adrian-sonar/target/sonar/checkstyle.xml 
**07:31:15** [main] INFO org.sonarsource.scanner.maven.SonarQubeMojo - Checkstyle charset: UTF-8 
**07:31:15** [main] INFO org.sonarsource.scanner.maven.SonarQubeMojo - Sensor CheckstyleSensor [checkstyle] (done) | time=327ms

The maven-executed cs uses it’s own rules - I can find no way to add the local rules to SQ (another issue).