14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HomeBrewでSonarQubeを設定

Last updated at Posted at 2014-03-31

MacのHomeBrewでSonarQube™(旧名:Sonar)をDBをPostgreSQLにして取り敢えず動かすまでのメモ。

環境

sonar 4.1.2
postgresql 9.3.4
maven 2.2.1

インストール

brew install sonar

必要に応じて

brew install postgresql
brew install maven2

などとpostgresqlやmaven2も同様にインストール。
(postgresql自体の詳細設定は割愛)

設定

1. 動作確認

sonar console

してから
http://localhost:9000/
にアクセスして動くことを確認。
Embedded database should be used for evaluation purpose only
と怒られるので、分析データ格納用DBをデフォルトのH2からpostgresに変更する。

2. SonarQubeサーバ側のDB設定

SonarQubeのインストールディレクトリに行き、設定を弄る

cd /usr/local/Cellar/sonar/4.1.2/libexec/conf
cp sonar.properties sonar.properties.org
vim sonar.properties

変更箇所は以下の通り

diff sonar.properties.org sonar.properties

30c30
< sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
---
> #sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
62c62
< #sonar.jdbc.url=jdbc:postgresql://localhost/sonar
---
> sonar.jdbc.url=jdbc:postgresql://localhost/sonar

3. postgresサーバー側設定

postgresなどのsuperuser権限でログインし、

psql -U postgres

以下のSQLを実行。

CREATE USER sonar WITH PASSWORD 'sonar';
CREATE DATABASE sonar WITH OWNER sonar ENCODING 'UTF8';

実行結果は

select usename, usesysid from pg_user;
select datname, datdba, encoding from pg_database;

などでそれぞれ確認出来る。

4. SonarQubeを再起動

Ctrl-Cなどで抜けてからsonar consoleを再度実行。
http://localhost:9000
で再度アクセスし、警告メッセージが消えていることを確認。

5. プラグインの追加や各種設定

右上のloginリンクから
ログイン: admin
パスワード: admin
でログインし、アップデートセンターからプラグインのインストールをしたり、各種設定を行う。

おすすめプラグイン

  • JavaScript
  • Japanese Pack
  • Web

適用するにはsonar consoleの再起動が必要。

6. クライアント側設定

個人カスタマイズ用に設定ファイルをコピーして編集。

mkdir -p ~/.m2
cp /usr/local/Cellar/maven2/2.2.1/libexec/conf/settings.xml ~/.m2
vim ~/.m2/settings.xml

以下のように<settings>-><profiles>配下にエントリを追加。

diff /usr/local/Cellar/maven2/2.2.1/libexec/conf/settings.xml ~/.m2/settings.xml

246a247,259
>     <profile>
>         <id>sonar</id>
>         <activation>
>             <activeByDefault>true</activeByDefault>
>         </activation>
>         <properties>
>             <sonar.jdbc.url>jdbc:postgresql://localhost/sonar</sonar.jdbc.url>
>             <sonar.jdbc.username>sonar</sonar.jdbc.username>
>             <sonar.jdbc.password>sonar</sonar.jdbc.password>
>             <!-- Optional URL to server. Default value is http://localhost:9000 -->
>             <sonar.host.url>http://localhost:9000</sonar.host.url>
>         </properties>
>     </profile>

7. 動作確認用サンプルプロジェクト作成

SeasarのSAStrutsとかのpom.xmlがすぐに使える適当なサンプルプロジェクトを用意。

cd /tmp
mvn archetype:generate -DarchetypeRepository=http://maven.seasar.org/maven2/ \
    -DarchetypeGroupId=org.seasar.sastruts \
    -DarchetypeArtifactId=sa-struts-archetype -DarchetypeVersion=1.0.4-sp9.1 \
    -DgroupId=hoge -DartifactId=Hoge -Dversion=1

→プロンプトが止まるのでy押下

8. SonarQube解析実行

cd Hoge
mvn sonar:sonar

BUILD SUCCESSFUL
が出て、解析結果が
http://localhost:9000/
に出ていればOK。

補足

プロジェクト名

上記サンプルだとプロジェクト名Unnamed - hoge:Hoge:war:1などと正しく出ていませんが
pom.xmlの<project>配下に

<name>Hoge</name>

などでプロジェクト名が指定可能です。

web, jsプラグイン

webプラグインやjsプラグインを動かすにはpom.xmlに小細工が必要です。

SonarQube Users - Setting maven sourceDirectory for javascript breaks build

設定例

pom.xml
   <packaging>war</packaging>
   <description/>
+  <properties>
+    <sourceDirectory>src/main/java</sourceDirectory>
+  </properties>
   <build>
+    <sourceDirectory>${sourceDirectory}</sourceDirectory>
     <finalName>Hoge</finalName>
     <plugins>
       <plugin>
@@ -253,5 +257,27 @@
       <scope>provided</scope>
     </dependency>
   </dependencies>
+  <profiles>
+    <!-- mvn -P sonar-web sonar:sonar で実行可能 -->
+    <profile>
+      <id>sonar-web</id>
+      <properties>
+        <sonar.language>web</sonar.language>
+        <sonar.branch>Web</sonar.branch>
+        <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
+        <sourceDirectory>src/main/webapp</sourceDirectory>
+      </properties>
+    </profile>
+    <!-- mvn -P sonar-js sonar:sonar で実行可能 -->
+    <profile>
+      <id>sonar-js</id>
+      <properties>
+        <sonar.language>js</sonar.language>
+        <sonar.branch>JS</sonar.branch>
+        <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
+        <sourceDirectory>src/main/webapp/js</sourceDirectory>
+      </properties>
+    </profile>
</project>

参考

14
14
0

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
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?