Tuesday, April 28, 2015

Analyzing the application code by using the sonarqube ANT/MAVEN


SonarQube™ software (previously known as “Sonar”) is an open source project hosted at Codehaus. By using this we can analyze the source code, its very easy to configure and use.


1. Download and unzip the SonarQube distribution ("C:\sonarqube" or "/etc/sonarqube")

2. Start the SonarQube server: under bin folder run the executable file according to respective OS.

sonarqube/bin/[OS]

3.Browse the results at http://localhost:9000

we will use Embedded database for learning.

under sonarqube/conf/sonar.properties will have the db base configuration, default it uses embeded db H2 which is in build in java.

Application level ANT configuration :


Download the sonar-ant-task jar file download

copy the jar file to /lib folder

add following to existing build.xml file of the application.

<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
    <classpath path="path/to/sonar-ant-task-*.jar" />
</taskdef>

if you don't want to modify the existing build.xml file then use below xml file and run "ant -f analyze-code.xml"

<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple Java Project analyzed with the Sonar Ant Task" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">
<!-- ========= Define the main properties of this project ========= -->
<property name="src.dir" value="src" />
<property name="build.dir" value="target" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="extlib.dir" value="ext-lib"/>
<!-- Define the Sonar properties -->
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the Sonar Ant Task" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="src" />
<property name="sonar.binaries" value="target" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<property name="sonar.scm.disabled" value="True" />
<!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
<!--
<property name="sonar.jdbc.url" value="jdbc:..." />
<property name="sonar.jdbc.username" value="..." />
<property name="sonar.jdbc.password" value="..." />
-->
<!-- ========= Define "regular" targets: clean, compile, ... ========= -->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<path id="local.classpath">
<fileset dir="${extlib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" fork="true" debug="true" includeAntRuntime="false">
<classpath refid="local.classpath"/>
</javac>
</target>
<!-- ========= Define Sonar target ========= -->
<target name="sonar" depends="compile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
<classpath path="E:/rivetsys/tools/ant/apache-ant-1.8.3/lib/sonar-ant-task-*.jar" />
</taskdef>
<!-- Execute Sonar -->
<sonar:sonar />
</target>
<!-- ========= The main target "all" ========= -->
<target name="all" depends="clean,compile,sonar" />
</project>


after the successful execution it will provide the url to access the result.


for maven application it simple run the following command

mvn clean install sonar:sonar


sample result page :





Related Posts:

0 comments:

Post a Comment