SonarQube 7.0中Sonar Scanner未分析src/test/java文件夹求助
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 thattarget/test-classesexists 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:
- Log into your SonarQube 7.0 web interface
- Navigate to your project > Project Settings > General Settings > Analysis Scope
- Check if
sonar.exclusionsorsonar.test.exclusionshas a rule that blockssrc/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.
- If you see an entry like
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

