Skip to content

Instantly share code, notes, and snippets.

@n-shinya
Created March 27, 2012 09:42
Show Gist options
  • Save n-shinya/2214427 to your computer and use it in GitHub Desktop.
Save n-shinya/2214427 to your computer and use it in GitHub Desktop.
How to add custom checks of Findbugs to Sonar

FindbugsのカスタムチェックをSonarに適用する方法

$SONAR_HOME/extensions/rules/findbugsに

  • rules.xml
  • カスタムチェックをまとめたプラグインjar

それぞれ配置して再起動すればよい。
手順はここに書いてある。
http://jira.codehaus.org/browse/SONAR-1481

動作の確認

sonar-findbugs-pluginが実行された時に、 配置したカスタムプラグインのログが出力されていればOK。

[INFO]  Found findbugs plugin: file:/tmp/sonar-batch200788980331407878716358278886859969/findbugs/findbugs_xxplugin-1.0-SNAPSHOT.jar ←カスタムjar
[INFO]  Found findbugs plugin: file:/tmp/sonar-batch200788980331407878716358278886859969/findbugs/findbugs-1.3.9.jar ← デフォルトjar
[INFO]  Loaded plugin edu.umd.cs.findbugs.plugins.core ←デフォルトプラグイン
[INFO]  Loaded plugin xx.tools.findbugs ← カスタムプラグイン

はまった

$SONAR_HOME/extensions/rules/findbugs/に配置するプラグインjarは 直下にfindbugs.xmlがある形でアーカイブされていないといけない。

これはsonar-findbugs-plugin(org.sonar.plugins.findbugs.FindbugsExecutor)が以下のように プラグイン登録を行っているからである。

private DetectorFactoryCollection loadFindbugsPlugins() {
  List plugins = Lists.newArrayList();
  try {
    Enumeration urls = Thread.currentThread().getContextClassLoader().getResources("findbugs.xml");
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      String path = StringUtils.removeStart(StringUtils.substringBefore(url.toString(), "!"), "jar:");
      Logs.INFO.info("Found findbugs plugin: " + path);
      plugins.add(new URL(path));
    }
  }
@n-shinya
Copy link
Author

今気づいた。
jar作るときにめんどくさがってzipで固めて、
その時にディレクトリ含める形でアーカイブした(直下がディレクトリの状態)からはまったんだ!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment