Thursday, November 6, 2014

SonarQube : Using SonarQube with Maven and Mac OSX







SonarQube (formally known as just sonar) is a server based open source platform for inspecting code quality of a project. It's mainly designed to inspect continuous code quality of projects with multi developer environment. But it can be use to inspect code quality in individual projects as well.


In this short tutorial I'll explain how to use sonar in single developer environment to inspect code quality of a java project.

I have used following configurations to up and running SonarQube. 
Mac OS X (10.9.4)
Mysql 
Maven 3.0
JDK 1.6 or Above
Sonar 4.4.1


1. Download, Install and run SonarQube

It's open source..!! you can download it free from sonar website. 
SonarQube download [1]. Unzip somewhere you can reach.
Set the SONAR_HOME path to sonar qube base directory using .profile or .bashrc scripts.


2. Create Mysql database for SonarQube

Source following script on mysql to create new database for sonar sonar database script [2]. This is essential as it keeps all data related to projects on this database.

Make sure it's created properly by login to Mysql server.




3. Set up SonarQube

Goto sonarQube conf directory using "cd $SONAR_HOME"/conf and open sonar.properties file using vim.

# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- Embedded database H2
# Note: it does not accept connections from remote hosts, so the
# SonarQube server and the maven plugin must be executed on the same host.

# Comment the following line to deactivate the default embedded database.
sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar

# directory containing H2 database files. By default it's the /data directory in the SonarQube installation.
#sonar.embeddedDatabase.dataDir=
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092


#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

Note down sonar .jdbc username and password this is the username and password.
Uncomment sonar.jdbc.url to enable jdbc driver for mysql database.


4. Set up maven to run sonar upon build.

Move to .m2 directory using following command.
cd ~/.m2/

Create a new file settings.xml.
vim settings.xml 

Save following xml as settings.xml using vim.
<settings>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Example for MySQL-->
                <sonar.jdbc.url>
                  jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
                </sonar.jdbc.url>
                <sonar.jdbc.username>root</sonar.jdbc.username>
                <sonar.jdbc.password>{your_mysql_root_password}</sonar.jdbc.password>
 
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                  http://localhost:9000
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>
Change your mysql root username and password accordingly and add localhost to sonar.host.url.

5. Run sonar server.

Move to sonar bin directory.
cd $SONAR_HOME/bin/

Run the sonar sever as follows.
./macosx-universal-64/sonar.sh start

Now using a web browser goto following url http://localhost:9000/ . Hopefully now you should see the sonar dashboard.


6. Inspect a project using SonarQube.

From CLI move to root directory of your project (where the main pom file is) and run following command.
 mvn clean install sonar:sonar

After build success using a web browser open sonar dashboard and you will see your newly build project under "Projects" tab.




Congratulations.. now you can use sonar for inspect quality of your code every time you build it.



References :




No comments:

Post a Comment