Jenkins中SonarQube Scanner扫描项目报错求助
Let's walk through resolving your two key errors—this is a super common setup issue when getting SonarQube hooked up to Jenkins for Java projects.
First Error: Invalid sonar.sources Path
Your first error clearly points to a path mismatch relative to Jenkins' workspace:
ERROR: The folder 'polacpat/Documents/Jenkins2/workspace/SonarScanningTestProject/src/test' does not exist for 'test' (base directory = /var/jenkins_home/workspace/SonarScanningTestProject)
What's wrong:
You used an absolute path from your local machine (polacpat/Documents/...) instead of a path relative to Jenkins' workspace root. SonarQube Scanner resolves sonar.sources based on the Jenkins job's workspace, not your local filesystem.
Fix:
Set sonar.sources to the relative path of your code within the Jenkins workspace. For example:
- If your main code lives in
src/main/javaand test code insrc/test/java, use:sonar.sources=src/main/java,src/test/java - If you only want to scan production code, use
sonar.sources=src/main/java
Second Error: Missing sonar.java.binaries
After fixing the path, you hit this Java-specific roadblock:
org.sonar.squidbridge.api.AnalysisException: Please provide compiled classes of your project with sonar.java.binaries property
What's wrong:
SonarQube needs your compiled .class files to run deep analysis (like dependency checks, bytecode rule enforcement, etc.). If you skip compiling before the scanner runs, or don't point to the correct compiled classes directory, this error will pop up.
Fix:
Add a compile step to your Jenkins job: Before running the SonarQube Scanner, execute your project's build command:
- For Maven: Run
mvn compile(ormvn test-compileto include test classes) - For Gradle: Run
gradle compileJava(orgradle compileTestJava) - For plain Java: Run your
javaccommand to output classes to a dedicated folder (e.g.,bin/)
- For Maven: Run
Configure
sonar.java.binaries: Point this property to your compiled main classes directory. Examples:- Maven projects:
sonar.java.binaries=target/classes - Gradle projects:
sonar.java.binaries=build/classes/java/main - Plain Java:
sonar.java.binaries=bin
If you want to analyze test code too, add
sonar.java.test.binariespointing to your compiled test classes (e.g.,target/test-classesfor Maven).- Maven projects:
Full Working Configuration Example
Here's a complete set of properties you can use in your Jenkins SonarQube Scanner build step, tailored to a Maven project:
# Core project identification (match what you created in SonarQube) sonar.projectKey=SonarScanningTestProject sonar.projectName=SonarScanningTestProject # SonarQube server details sonar.host.url=http://your-sonarqube-server-address:9000 sonar.login=your-sonarqube-authentication-token # Use a token instead of password for security # Source and compiled code paths sonar.sources=src/main/java,src/test/java sonar.java.binaries=target/classes sonar.java.test.binaries=target/test-classes # Java version matching your project sonar.java.source=8 sonar.java.target=8
Quick Checks to Avoid Future Issues
- Verify Jenkins workspace structure: Use the Jenkins UI to navigate to your job's workspace and confirm your source and compiled class paths are correct.
- Scanner-Server compatibility: Your SonarScanner 3.0.3 works perfectly with SonarQube 6.7.1 (they’re in compatible version ranges), so no issues there.
- Stick to relative paths: Always use paths relative to the Jenkins workspace for
sonar.sourcesandsonar.java.binaries—absolute paths will break between environments.
Follow these steps, and your SonarQube scan should run successfully!
内容的提问来源于stack exchange,提问作者Ver

