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

SonarQube 7.0中Sonar Scanner未分析src/test/java文件夹求助

Fix: Sonar Scanner Not Analyzing src/test/java in Maven Project

Hey there, let’s get your test code showing up in SonarQube analysis! I’ve tackled this exact issue before, so here are practical steps to resolve it:

1. Add Sonar Test Configuration to pom.xml

SonarQube needs explicit guidance to find your test sources and compiled test classes. Add these properties to your pom.xml inside the <project> tag:

<properties>
    <!-- Specify the test source directory -->
    <sonar.java.test.files>src/test/java</sonar.java.test.files>
    <!-- Point to the compiled test classes (critical for analysis) -->
    <sonar.java.test.binaries>target/test-classes</sonar.java.test.binaries>
</properties>

This tells the Sonar Maven plugin exactly where to look for your test code and its compiled output.

2. Ensure Test Code is Compiled

Sonar Scanner can’t analyze test files that haven’t been compiled first. Run one of these commands to generate the target/test-classes directory:

  • Compile only test code: mvn test-compile
  • Full build (skip tests if needed): mvn clean install -DskipTests
    Double-check that target/test-classes exists and contains your compiled test classes before running Sonar analysis.

3. Verify SonarQube Project Scope Settings

Sometimes the issue is on the SonarQube server side:

  1. Log into your SonarQube 7.0 web interface
  2. Navigate to your project > Project Settings > General Settings > Analysis Scope
  3. Check if sonar.exclusions or sonar.test.exclusions has a rule that blocks src/test/java
    • If you see an entry like src/test/java/**/*, remove it or adjust the pattern to include the test files you want analyzed.

4. Use the Correct Sonar Scanner Command

Make sure you’re running the full Sonar analysis command with necessary server details. For example:

mvn sonar:sonar -Dsonar.host.url=http://your-sonarqube-server:9000 -Dsonar.login=your-authentication-token

Replace your-sonarqube-server and your-authentication-token with your actual server URL and token (if your SonarQube instance requires login).

5. Debug with Verbose Logs

If the above steps don’t work, enable debug logging to pinpoint the issue. Run:

mvn sonar:sonar -X

Look for lines related to test file scanning—you might see messages like "0 test files analyzed" or errors about missing test binaries. These logs will help you narrow down whether the plugin can’t find the files, or if there’s a configuration conflict.

Give these steps a go, and your src/test/java files should start appearing in SonarQube’s analysis results. If you hit specific errors in the logs, share those details and we can dig deeper!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:40:53