Tuesday, August 6, 2019

Installing SonarQube - 4.5.7 and Generating PDF Reports of the Analysis For an Ant Based Project in Ubuntu

Hi,

In this post, I am going to talk about how to perform static code analysis of any java project based on ANT build.

The Prerequisites section lists the items that we should have in place to preform the source code analysis and sonarqube is depending on.

Prerequisites
Name Version Download Page Direct Link
SonarQube 4.5.7 SonarQube Download Page Click to Download Directly
SonarQube Ant Task 2.5 SonarQube Ant Task Download Page Click to Download Directly
SonarQube Scanner API 2.8 SonarQube Scanner API Download Page Click to Download Directly
jsr305 3.0.0 jsr305-3.0.0.jar Download Page Click to Download Directly
Sonar PDF Report Plugin 1.4 Sonar PDF Report Plugin Download Page Click to Download Directly
Apache Ant 1.9.14 Apache Ant Click to Download Directly
MySql 5.7.26 MySql N/A

From the prerequisites section... I will be discussing the installation of first three items and for the last two, you can refer online for the installation of same.

1. Installation of MySql Server
If you have one already you can skip this section else you can check an other post of mine, the link to the post is given below. .

Installation of MySql Server on Ubutu

2. Creating Database for SonarQube
Login into mysql server, make sure you have the administrator privileges, In my case I am going to use the root account. Open up a Terminal and fire fire the command below
> mysql -u root -p
the above command would prompt you for password to key in, provide the password associated with the account, if the login is successful, then the mysql prompt would appear on the terminal.

At the mysql prompt, create a database using any name of your choice....
mysql> create database sonarqube;
create a user to access the database that we just created and grant full access to the user, the root account is sufficient to access the database, but it is not advisable to share or use the administrator account....

command to create a user
mysql> create user sonarqube identified by 'password';

command to give full access on the database 'sonarqube'
mysql> grant all privileges on sonarqube.* to 'sonarqube' identified by 'password'

2. Installation of SonarQube
Download SonarQube using the direct link given at the prerequisite section, and extract it to one of your favourite locations, make sure the location where you extract sonarqube has write permissions...

2.1 Updating SonarQube's Configuration File
Inside the extracted folder, you would find a folder named 'conf', navigate to that sub-directory and open the file 'sonar.properties' in your favourite text editor....

In my case I was using VI
> vi sonar.properties
In this file you would find three properties namely
1. sonar.jdbc.username
2. sonar.jdbc.password
3. sonar.jdbc.url

Un comment the above mentioned properties and update these as dictated below....

FROM:
sonar.jdbc.username=sonar

TO:
sonar.jdbc.username=sonarqube
FROM:
sonar.jdbc.password=sonar

TO:
sonar.jdbc.password=password
FROM:
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

TO:
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

3. Installing SonarQube Ant Task
Download the SonarQube Ant Task from the location given in the prerequisite section and extract it to your favourite location.

4. Installing SonarPDF Report Plugin
Download Sonar PDF Report Plugin from the location given in the prerequisites section and drop in the sonarqube server at the location given below....

<INSTALLATION-DIRECTORY-OF-SONARQUBE>/extensions/plugins

5. Start SonarQube Server
Open a terminal and navigate to the bin directory of sonarqube i.e <INSTALLATION-DIRECTORY-OF-SONARQUBE>/bin and fire the command below at the prompt...
> sonarqube/linux-x86-64/sonar.sh start
Pick the appropriate directory according to the Operation System you are working with.

After you start your sonarqube server... you could see the server writing tables required to generate the reports on the database... when you open your database and list the tables you should see similar to the image given below...


This concludes the sonarqube server setup...

6.Updating Ant's build.xml of the Project
Let us start updating the Ant's build.xml file of the project, assuming the build.xml file is like the one given below....
<project name="hello-world" basedir="." default="deploy">
<import file="build-common.xml" />
</project>
This build.xml file explains, the name of the project is 'hello-world' and the default task is deploy... along with that, the file imports other tasks from the file named 'build-common.xml'...

Now let us update the file build.xml in order to have the source code analysed for the project...

As a first step let us introduce "xmlns:sonar", this is one of the attributes of xml element <project>, after you introduce this attribute the build.xml file should like the one given below...
<project name="hello-world" basedir="." default="deploy" xmlns:sonar="antlib:org.sonar.ant">
<import file="build-common.xml" />
</project>

Next we are going to introduce new task definition to the project

<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="<SONAR-ANT-JARS-INSTALLATION-PATH>/jsr305-3.0.0.jar" />
<classpath path="<SONAR-ANT-JARS-INSTALLATION-PATH>/sonarqube-ant-task-2.5.jar" />
<classpath path="<SONAR-ANT-JARS-INSTALLATION-PATH>/sonar-scanner-api-2.8.jar" />
</taskdef>

Now the build.xml file should appear something like the one given below....

<project name="fiera-messaging-portlet" basedir="." default="deploy" xmlns:sonar="antlib:org.sonar.ant">
 
    <import file="../build-common.xml" />
 
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="/home/ravaneswaran/sonar-ant-jars/jsr305-3.0.0.jar" />
        <classpath path="/home/ravaneswaran/sonar-ant-jars/sonarqube-ant-task-2.5.jar" />
        <classpath path="/home/ravaneswaran/sonar-ant-jars/sonar-scanner-api-2.8.jar" />
    </taskdef>
 
</project>

The next job is to introduce the sonar task, in order to do that update the build.xml file with the following lines

<target name="sonar" depends="compile">
     
        <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="sonar.jdbc.username" value="sonarqube" />
        <property name="sonar.jdbc.password" value="password" />
     
        <property name="sonar.projectKey" value="hello-world" />
        <property name="sonar.projectName" value="Hello World" />
        <property name="sonar.projectVersion" value="1.0" />
        <property name="sonar.language" value="java" />
        <property name="sonar.sources" value="docroot" />
        <property name="sonar.binaries" value="docroot/WEB-INF/classes" />
        <property name="sonar.sourceEncoding" value="UTF-8" />
     
     
        <property name="sonar.exclusions" value="**/com/test/base/**/*,**/com/test/persistence/**/*,**/com/test/impl/**/*" />
        <echo>Sonar Report is Executing</echo>
        <echo>Sonar Report is Executing on Source Directory docroot/WEB-INF/src,docroot/html</echo>
     
        <!-- Execute SonarQube Scanner for Ant Analysis -->
        <sonar:sonar/>
    </target>

To brief some about the above target, this is nothing but the configurations that sonar ant would be looking for

if you look closely you can see  properties holding the values required to connect to database and to communicate to sonarqube server....

The final build.xml file would be like the one given below....

<project name="fiera-messaging-portlet" basedir="." default="deploy" xmlns:sonar="antlib:org.sonar.ant">
 
    <import file="../build-common.xml" />
 
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <classpath path="/home/fiera-mm/Downloads/sonar-ant-jars/jsr305-3.0.0.jar" />
        <classpath path="/home/fiera-mm/Downloads/sonar-ant-jars/sonarqube-ant-task-2.5.jar" />
        <classpath path="/home/fiera-mm/Downloads/sonar-ant-jars/sonar-scanner-api-2.8.jar" />
    </taskdef>
 
    <target name="sonar" depends="compile">
     
        <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="sonar.jdbc.username" value="sonarqube" />
        <property name="sonar.jdbc.password" value="password" />
     
        <property name="sonar.projectKey" value="hello-world" />
        <property name="sonar.projectName" value="Hello World" />
        <property name="sonar.projectVersion" value="1.0" />
        <property name="sonar.language" value="java" />
        <property name="sonar.sources" value="docroot" />
        <property name="sonar.binaries" value="docroot/WEB-INF/classes" />
        <property name="sonar.sourceEncoding" value="UTF-8" />
     
     
        <property name="sonar.exclusions" value="**/com/test/base/**/*,**/com/test/persistence/**/*,**/com/test/impl/**/*" />
        <echo>Sonar Report is Executing</echo>
        <echo>Sonar Report is Executing on Source Directory docroot/WEB-INF/src,docroot/html</echo>
     
        <!-- Execute SonarQube Scanner for Ant Analysis -->
        <sonar:sonar/>
    </target>
 
    <target name="all" depends="clean,compile,sonar"/>
 
</project>


7 Generating the Reports
Now we are all set to generate the sonarqube analysis for our project "hello-world"

Make sure the sonarqube server is up and running.

Open up a terminal and navigate to the project directory and fire the command below to generate the analysis...
> ant sonar

8 Viewing the Reports
Now open up a browser and hit the following address on the address bar

http://localhost:9000

You can see the result of the analysis of sonarqube as the one given below...



Well this concludes this post, hope you have enjoyed it

Thanks.

1 comment:

How to change the root password in linux when it is forgotten/to change

This blog is all about changing the root password of the Linux system when it is forgotten or to reset the password...   Let's get it ...