You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins中SonarQube Scanner扫描项目报错求助

Fixing SonarQube Scanner Errors in Jenkins for GitLab Java Projects

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/java and test code in src/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:

  1. Add a compile step to your Jenkins job: Before running the SonarQube Scanner, execute your project's build command:

    • For Maven: Run mvn compile (or mvn test-compile to include test classes)
    • For Gradle: Run gradle compileJava (or gradle compileTestJava)
    • For plain Java: Run your javac command to output classes to a dedicated folder (e.g., bin/)
  2. 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.binaries pointing to your compiled test classes (e.g., target/test-classes for Maven).

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.sources and sonar.java.binaries—absolute paths will break between environments.

Follow these steps, and your SonarQube scan should run successfully!

内容的提问来源于stack exchange,提问作者Ver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:17:34