How to use SonarCube in your maven projects to analyze the code quality

SonarQube is an open platform to manage code quality. As such, it covers the 7 axes of code quality

You can use it in your maven projects by adding the sonar plugin to pom.xml

 

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>sonar-maven-plugin</artifactId>
   <version>2.3</version>
</plugin>

 

and by adding these properties to pom.xml (an instance of sonar server is already setup)

 

<sonar.jdbc.url>jdbc:mysql://194.219.31.173:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>                       
<sonar.host.url>http://sonar.gnomon.com.gr/sonar/</sonar.host.url>

 

In order not to have in pom.xml these properties you can put them in ~/.m2/settings.xml file in your server in the profiles section

 

<profile>
      <id>openncp</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
            <sonar.jdbc.url>jdbc:mysql://ipaddress:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true</sonar.jdbc.url>
            <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
            <sonar.jdbc.username>sonar</sonar.jdbc.username>
            <sonar.jdbc.password>sonar</sonar.jdbc.password>    
            <sonar.host.url>http://sonar.gnomon.com.gr/sonar/</sonar.host.url>
      </properties>
</profile>

 

Then you can add in your maven commands the mvn sonar:sonar and you will get an analysis of your code in web browser

Easily it can be added to jenkins jobs to run from CI Server when needed