9

I am attempting to get Jenkins to run Sonar as a post-build step.

Jenkins runs SonarRunner [1] but this fails with:

ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.projectName, sonar.projectVersion, sonar.sources

If I run mvn sonar:sonar [2] instead it works fine. It almost sounds like SonarRunner is ignoring pom.xml. How do I get SonarRunner to pick up these properties from Maven?


[1] /Users/builds/.jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Default_Sonar_Runner/bin/sonar-runner -Dsonar.jdbc.url=jdbc:h2:tcp://myServer:1009/sonar -Dsonar.host.url=http://myServer/sonar/ -Dsonar.projectBaseDir=/Users/builds/.jenkins/workspace/myProject

[2] mvn sonar:sonar -Dsonar.jdbc.url=jdbc:h2:tcp://myServer:1009/sonar -Dsonar.host.url=http://myServer/sonar/ -Dsonar.projectBaseDir=/Users/builds/.jenkins/workspace/myProject

Gili
  • 86,244
  • 97
  • 390
  • 689

4 Answers4

20

UPDATE: Per https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/ the desired functionality is now available.


What a nice way to waste an evening...

According to this Sonar Runner is not meant for Maven projects. It turns out you're supposed to look under "Post-Build Actions" (not "Post-Build Steps") and you will find "Sonar". See here for more information.

Gili
  • 86,244
  • 97
  • 390
  • 689
  • As of SonarQube 5.x, the SonarQube documentation states using the Jenkins "Sonar" post-build action is deprecated http://docs.sonarqube.org/display/SONARNEXT/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-TriggeringaSonarAnalysiswithMaven – buzz3791 Dec 05 '15 at 00:12
  • Link is broken :( – Farid Movsumov Nov 17 '16 at 05:37
  • My Jenkins tells me it deprecated but it solved my issue – electricalbah Feb 04 '17 at 10:58
  • This is the latest link for here. @Gili would you like to update. https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/ – Vaibhav Jain Nov 03 '22 at 13:29
1

Method 1: If you installed a scanner in Jenkins then just add the following in this location on your job
Post Steps -> Execute SonarQube Scanner -> Analysis properties

sonar.projectKey=myProject1
sonar.sources=.

Method 2: Another alternative is using the deprecated method which still works fine Post-build Actions -> SonarQube analysis with Maven

Method 3: Build -> Goals and options -> "enter the below script" -Dmaven.test.skip=true install sonar:sonar -Dsonar.forceAnalysis=true -Dsonar.host.url=http://localhost:9000/ -Dsonar.branch=my-branch-1

electricalbah
  • 2,227
  • 2
  • 22
  • 36
0

There is some Maven GAV info data mapped into Jenkins variables, look at this thread: Automatically derive mandatory SonarQube properties from pom file in Jenkins

Community
  • 1
  • 1
PRF
  • 821
  • 1
  • 9
  • 16
0

Try below configuration in sonar-scanner.properties:

sonar.host.url=http://localhost:9000
sonar.projectKey=<your project package>
sonar.projectName=<your project name with absolute path>
sonar.projectVersion=1.0
sonar.sources=src/main/java/
sonar.language=java
sonar.java.binaries=target/classes
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263