Loading data into Sonar

Let the world know ...Tweet about this on TwitterShare on Google+Share on FacebookEmail this to someoneShare on LinkedIn

The following blog was meant to be part of my Sonar with SAP Java series originally published at SCN, but somehow it was never published. So, here we go, some more information on Sonar and SAP Java, including findbug for code quality checks.

Sonar needs to be fed with data. From inside Sonar you cannot define a path with files to be analyzed. The task to send files is up to the developer (or a kind of software). Yes, in that case the developer can be replaced by a script. The process for loading the data to be analyzed is described in great detail at the Sonar homepage. I’m using ant for this. I created a stub build.xml file for my needs that does the compilation of SAP Portal PAR and EAR archives.

<property
name=“sonar.jdbc.url”
value=“jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8”
/>

<property
name=“sonar.jdbc.driverClassName”
value=“com.mysql.jdbc.Driver”
/>

<property
name=“sonar.jdbc.username”
value=“sonar”
/>

<property
name=“sonar.jdbc.password”
value=“sonar”
/>    

<property
name=“sonar.host.url”
value=“http://localhost /sonar”
/>

<target name=”sonar”>

<property name=”sonar.binaries” value=”path to jar files” />

    <sonar:sonar workDir=”.” key=”com.tobias.km.par:KmListFiles” version=”1.0″ xmlns:sonar=”antlib:org.sonar.ant”>

        <sources>

            <path location=”dist” />

            <path location=”src.core” />

        </sources>

        <property key=”sonar.projectName” value=”KM List Files PAR” />

        <property key=”sonar.dynamicAnalysis” value=”false”/>

    </sonar:sonar>

</target>

Complete ant file

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?>

<project
basedir=“.”
default=“par”
name=“UserList”>


<property
environment=“env”/>


<property
name=“JRE_LIB”
value=“../../../../../../Java14/jre/lib/rt.jar”/>


<property
name=“ECLIPSE_HOME”
value=“../../../eclipse”/>


<property
name=“debuglevel”
value=“source,lines,vars”/>


<property
name=“target”
value=“1.4”/>


<property
name=“source”
value=“1.4”/>

    

    <property
name=“name.par”
value=“com.tobias.km.KmListFiles.par”/>

    <property
name=“sonar.jdbc.url”
value=“jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8”
/>

    <property
name=“sonar.jdbc.driverClassName”
value=“com.mysql.jdbc.Driver”
/>

    <property
name=“sonar.jdbc.username”
value=“sonar”
/>

    <property
name=“sonar.jdbc.password”
value=“sonar”
/>    

    <property
name=“sonar.host.url”
value=“http://localhost:2080/sonar”
/>


<path
id=“project.classpath”>


<pathelement
location=“classes.api”/>


<pathelement
location=“${JRE_LIB}”/>


<pathelement
location=“C:/Dev/jar/servlet.jar”/>


<pathelement
location=“C:/Dev/jar/bc.uwl.service.api_api.jar”/>


<pathelement
location=“C:/Dev/jar/bc.rf.framework_api.jar”/>


<pathelement
location=“C:/Dev/jar/bc.util.public_api.jar”/>


<pathelement
location=“C:/Dev/jar/com.sap.portal.ivs.connectorservice_api.jar”/>


<pathelement
location=“C:/Dev/jar/com.sap.portal.usermanagementapi.jar”/>


<pathelement
location=“C:/Dev/jar/com.sap.security.api.ep5.jar”/>


<pathelement
location=“C:/Dev/jar/ConnectorHelper.jar”/>


<pathelement
location=“C:/Dev/jar/ExtendedConnector.jar”/>


<pathelement
location=“C:/Dev/jar/GenericConnector.jar”/>


<pathelement
location=“C:/Dev/jar/lafapi.jar”/>


<pathelement
location=“C:/Dev/jar/portal_services_api_lib.jar”/>


<pathelement
location=“C:/Dev/jar/prtapi.jar”/>


<pathelement
location=“C:/Dev/jar/prtconnection.jar”/>


<pathelement
location=“C:/Dev/jar/prtcoreservice.jar”/>


<pathelement
location=“C:/Dev/jar/prtdeploymentapi.jar”/>


<pathelement
location=“C:/Dev/jar/prtjsp_api.jar”/>


<pathelement
location=“C:/Dev/jar/prttest.jar”/>


<pathelement
location=“C:/Dev/IDE702/eclipse/plugins/com.sap.security_2.0.0/lib/com.sap.security.api.jar”/>


</path>

    <taskdef
uri=“antlib:org.sonar.ant”
resource=“org/sonar/ant/antlib.xml”></taskdef>

    <target
name=“sonar”>

     <!–<property name=”sonar.libraries” value=”C:\Dev\jar\SAP Portal JARs\*.jar” />–>

        <!–<property key=”sonar.libraries” value=”project.classpath” />–>

     <property
name=“sonar.tests”
value=“”
/>

        <property
name=“sonar.binaries”
value=“build/classes”
/>            

        <!– findbugs –>

        <path
id=“sap.jars”>

         <fileset
dir=“C:\Dev\jar\SAP Portal JARs”
includes=“*.jar”
/>

        </path>

        <pathconvert
property=“sonar.libraries”
refid=“sap.jars”
pathsep=“,”
/>

        <sonar:sonar
workDir=“.”
key=“com.tobias.km.par:KmListFiles”
version=“1.0”
xmlns:sonar=“antlib:org.sonar.ant”>

            <sources>

                <path
location=“dist”
/>

                <path
location=“src.core”
/>

            </sources>

            <property
key=“sonar.projectName”
value=“KM List Files PAR”
/>

            <property
key=“sonar.dynamicAnalysis”
value=“false”/>

        </sonar:sonar>

    </target>

    <path
id=“compile.classpath”>

        <pathelement
location=“${JRE_LIB}”/>

         <pathelement
location=“C:/Dev/IDE702/eclipse/plugins/com.sap.security_2.0.0/lib/com.sap.security.api.jar”/>

        <fileset
dir=“C:\Dev\jar”>

            <include
name=“*.jar”/>          

        </fileset>

    </path>

    <target
name=“init”>

     <mkdir
dir=“build/classes”/>

     <mkdir
dir=“dist_temp”
/>

    </target>

    <target
name=“compile”
depends=“init”
>

     <javac
destdir=“build/classes”
debug=“true”
srcdir=“src.core”>

     <classpath
refid=“compile.classpath”/>

     </javac>

    </target>

    <target
name=“par”
depends=“compile”>

     <war
destfile=“dist_temp/${name.par}”
webxml=“dist/PORTAL-INF/portalapp.xml”>

     <fileset
dir=“dist”/>

     <lib
dir=“dist/PORTAL-INF/private/lib”/>

     <classes
dir=“build/classes”/>

     </war>

        <copy
includeemptydirs=“false”
todir=“./”>

         <fileset
dir=“dist_temp”></fileset>

        </copy>

    </target>     

</project>

Load all necessary JARs == make them available to findbugs.

<property name=”sonar.binaries” value=”build/classes” />

<path
id=“sap.jars”>

         <fileset
dir=“C:\Dev\jar\SAP Portal JARs”
includes=“*.jar”
/>

        </path>

        <pathconvert
property=“sonar.libraries”
refid=“sap.jars”
pathsep=“,”
/>

Activate findbugs in Sonar:

  • Sonar way with Findbugs

Example code that triggers a blocker in findbugs:

Object o = null;

public String foo(Object o) {        

    if (Integer.class.isInstance(o)) {

        return (String) o;

    }

    return
“”;

}

String a = foo(o);

The code won’t work as o is null, but will compile. Instead of deploying that code and then be surprised why an exception occurred during runtime, let Sonar and findbugs let do this.

Nice thing from findbug is that it tracks the values of variables and objects.

Object o = null;

if (o instanceof IPortalComponentRequest) {}

Findbug identifies that we are going to check a null value.

Findbug even analyzes the code further and gives for the same line of code more information:

And without findbugs? What does sonar find from just looking at the source code?

No more blockers and the rules compliance went up from 35% to 63%. From the source code analysis Sonar is able to identify serious problems too, but to get the most out of the analysis findbug should also be activated.

The critical error shows only the empty if statement:

So no value tracking and identifying of possible NullPointerExceptions. And these are the ones that are really annoying as they can be predicted but finding them can be a tedious task.

Let the world know ...Tweet about this on TwitterShare on Google+Share on FacebookEmail this to someoneShare on LinkedIn