LoginSignup
0
1

More than 5 years have passed since last update.

SonarQubeでテストコードも静的解析するには?

Posted at

金曜夜の小ネタ。

起こったこと

あるモジュールでSonarQubeの静的解析ができない。
下で触れるようにmvn sonar:sonarを使っている。

環境

SonarQube 5.4

調べたこと

ログ

"mvn sonar:sonar"を使うシェルスクリプトをJenkins経由で実行していたので、JenkinsのConsole Outputを確認(一部伏せ字)。

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.2:sonar (default-cli) on project XXXXXX: File [moduleKey=XXXXXX, relative=src/test/java/...../XXXXXXTest.java, basedir=XXXXXX] can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

公式ドキュメントやらネットの情報やら

Narrowing the Focus

  • sonar.sourcesで "解析の範囲を特定のディレクトリに制限" (="limit the scope of the analysis to certain directories")できる
  • sonar.exclusions, sonar.test.exclusions で、解析から除外するソースコード、単体テストコードを指定できる

Need clarification on the Inclusion\exclusion pattern

By default Maven plugin will set:
sonar.sources=src/main/java
sonar.tests=src/test/java

Since you have overriden sonar.sources to be "." then indeed test files are indexed twice (once as "main" code and once as "test").

  • デフォルトはsrc/main/java, src/test/java
  • sonar.sourcesを.に指定したことで、テストコードが2回インデックスされてしまう

Analysis Parameters

"the default Maven source code location" とか "the default location for Java Maven projects" とかは以下参照でいいはず。
Introduction to the Standard Directory Layout

直したこと

スクリプトの中で"-Dsonar.sources=."を指定していた箇所があったので、それを取り除いた。

mvn sonar:sonar -Dsonar.projectKey=XXXXXX -Dsonar.projectName=XXXXXX -Dsonar.projectVersion=XXXXXX -Dsonar.sources=.
↓
mvn sonar:sonar -Dsonar.projectKey=XXXXXX -Dsonar.projectName=XXXXXX -Dsonar.projectVersion=XXXXXX

ちなみに

そもそもなんで -Dsonar.sources=. 付けてたのか。
Analyzing with SonarQube Scannerに従っていた。SonarQube導入時に、テストコード書いていない小さいモジュールで試したから、そもそも上記の2回インデックスされる問題が起こらなかった。

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=.
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1