In this session we will look at how to set up a sonarqube on your machine and how to run a simple java program and analyze report.
Let's go ahead and start the first move by downloading the necessary software
I will use Version: 8.0 and we can get this software from the "https:/www.sonarqube.org/downloads/" website.
Second, to run our source code, we need a sonar scanner. There are various ways to set up the scanner with the gradle, and the Jenkins are some other plugins that we can use to run the scanner.
You can download this from the website of sonarqube and view the sonarqube scanner
You can download the software first, then proceed to the session
You can see the Sonarqube file structure like this
You will see the similar structure again in the Sonarscanner case
Next step is to set the sonar scanner environment variable So you go to the sonar scanner and you can take path upto bin and set it as environment variables so you can run the sonar report from anywhere.
To set the environment variables 1. First right click on my computer 2. Go to properties
3. Then select Advance System Settings
4. From System Properties window you have to select environment variables
5. In environment variables window we need to add "C:\Users\Admin\Desktop\sonar\softwares\sonar-scanner-18.104.22.1683-windows\bin" this path in system variables Path
Now the next step is to review the report so to do is you can take any Java project For example, in my case, this is my Java source code
Now I want sonar cube to examine this class file and tell me if it's all good or if there are any problems.
For this we need to configure this project in sonar scanner, to configure you need to go to sonar scanner folder -> go in conf folder
In cong folder you can see sonar-scanner properties file.
Right click on properties file and click edit
Properties file have entry related to sonar qube server
First one is the default url to access the report
We need to make a few entries about our project so that sonarqube can pick up the details of this project and try to run it.
We will include a few entries for the Java project analysis
sonar.projectKey=SimpleJavaProject sonar.projectName=SimpleJavaProject sonar.projectVersion=1.0 sonar.sources=E:/EclipsePractiseWorkspace/SimpleJavaProject/src/com/codeusingjava/sonar
I'm going to give this full path to how I get this source code
Note: In windows you need to change the backword slash '\' to '/' in path of our source file.
Now we will start sonarqube. To start sonarqube you need to go in particular folder for me it will be "bin\windows-x86-64" right click and open comand prompt and run command "StartSonar.bat"
Once sonarqube is started you can see:
You can go to URL:"http://localhost:9000/" and check in browser if sonarqube dashboard is displayed.
It will ask for username and password default username and password is admin:admin
Now we need to start the sonar scanner for that you need to go to project folder and open command prompt and hit "sonar-scanner.bat" command
Once the execution is done it will show
Now refresh the page "http://localhost:9000/" and you could see your project
In the above image you could see two code smells. Those are show below
So it says that code which sonarqube as analysed it has two problems. First is todo and second is about using System.out.printlnstatement It is not a good practise to use sysout. We should always use logger.
After fixing these issues
This is how the sonarqube document can allow the technical teams to optimize or more effectively change their software to ensure highly reliable projects are delivered.