Introduction:

In this blog, i am presenting you the integration of Sonar code quality tool with Maven.  Sonar is a powerful code quality tool Which leads the control of quality of code in different phases of the project life cycle.
Sonar has got a very efficient way of navigating, a balance between high-level view, dashboard, TimeMachine and defect hunting tools. This enables to quickly uncover projects and / or components that are in Technical Debt to establish action plans.Sonar is a web-based application. Rules, alerts, thresholds, exclusions, settings… can be configured online. By leveraging its database, Sonar not only allows to combine metrics altogether but also to mix them with historical measures.

Languages covered
Java is built in. Open Source and commercial plugins enable to cover C, C#, Flex, Natural, PHP, PL/SQL, Cobol and Visual Basic 6.

For more details about Sonar http://www.sonarsource.org/

Install Sonar:

Sonar is a open source code quality tool which can be downloaded here http://www.sonarsource.org/downloads/

Follow the below steps once the download is completed.

1. UnZip the sonar-2.13.1.zip in to your local dick for example C:\\Installable\Sonar.

2. Modify the below properties for sonar.properties which is available in the conf folder of your installed location.

sonar.web.host:                             localhost
 sonar.web.port:                             9000
 sonar.web.context:                       /MyprojectCodeQuality

3. By default derby database is embedded with sonar. you can can easily configure your database in to saonar by specifying the database properties in to sonar.properties fille.

For Oracle database:

As per the sonar specification,

It's recommended to use the latest version of the JDBC driver (either ojdbc6.jar for Java 6 or ojdbc5.jar for Java 5). Copy the driver to the directory extensions/jdbc-driver/oracle/.  Comment the embedded database and uncomment the following line and provide the required database parameters.

sonar.jdbc.username:                       schema userName
 sonar.jdbc.password:                       schema password

sonar.jdbc.url:                                            jdbc:oracle:thin:@DB Host name:Port number:DB Instance
 # Optional properties
 sonar.jdbc.driverClassName:                oracle.jdbc.OracleDriver
 sonar.jdbc.validationQuery:                select 1 from dual

4. Start sonar:

For windows 32 bit, execute the startsonar.bat which is available in \\ sonar-2.13.1\bin\windows-x86-32

That's all. Sonar is configured with Oracle database and this can be accessible in the below url.

http://localhost:9000/MyprojectCodeQuality

Configure with Maven:

To configure the Sonar with Maven, the below plugin to be included in to to setting.xml of maven


Maven: Setting.xml
<profile>
<id>sonar</id>
 <activation>
 <activeByDefault>true</activeByDefault>
 </activation>
 <properties>
 <!-- EXAMPLE FOR ORACLE-->

<sonar.jdbc.url>jdbc:oracle:thin:@ jdbc:oracle:thin:@DB Host name:Port number:DB Instance</sonar.jdbc.url>
 <sonar.jdbc.driverClassName>oracle.jdbc.OracleDriver</sonar.jdbc.driverClassName>
 <sonar.jdbc.username> schema userName</sonar.jdbc.username>
 <sonar.jdbc.password> schema password</sonar.jdbc.password>

<!-- optional URL to server. Default value is http://localhost:9000 -->
 <sonar.host.url>http://localhost:9000/MyprojectCodeQuality</sonar.host.url>

</properties>
 </profile>

Invoke the below mvn command to analyze your project from windows command line.

set PATH=%JAVA_HOME%\bin;%MAVEN_HOME%\bin;%PATH%

mvn clean install

mvn sonar:sonar

Also the sonar properties can be pass as a parameters to mvn command.

mvn sonar:sonar -Dsonar.host.url=http://localhost:9000/MyprojectCodeQuality -Dsonar.jdbc.url=jdbc:oracle:thin:@ jdbc:oracle:thin:@DB Host name:Port number:DB Instance-Dsonar.jdbc.driverClassName=oracle.jdbc.OracleDriver -Dsonar.jdbc.username= schema userName -Dsonar.jdbc.password=schema password

About Suyambu

Ask my friends :-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s