We have previously installed the SonarQube portal with Tomcat, and the SonarQube Runner, which will enable us to achieve our first analysis today.
In the installation folder of the SonarQube Runner, we cand find three directories:
- A directory ‘..\lib’ dedicated to.a jar file necessary for the execution of the SonarQube-Runner.
- A directory ‘..\conf’ with the file ‘sonar–runner.properties‘ dedicated to the connectiong to SonarQube and our database.
- A directory ‘..\bin’ where is located a file ‘sonar–runner.bat` which allows us to run an analysis.
Before configuring it, let‘s pause a moment to think about the organization of our analysis environment.
Analysis environment
When you install a server dedicated to code analysis, it is important to differentiate between different spaces according to their purpose:
- The space for the different software and their versions: Oracle, Java, Tomcat, SonarQube, etc.
- The space for to the delivery and installation of the source code to be analyzed.
- The space dedicated to the implementation of analysis: configuration, backups, customizations, reporting, analysis and documentation of your (useful when you begin to have multiple analysis or you must work an old analysis or your analysis server is used by different people), etc..
SonarQube allows us to specify this space in the file ‘sonar–runner.bat‘, with the variable ‘PROJECT HOME’.
We will look into this file for the following line:
set PROJECT_HOME= %CD%
and change it to indicate a subdirectory ‘\Projects’ in which we will place the configuration files of our analyzes. In reality, I would not locate this space in the SonarQube Runner directory, it is only for demonstration purposes.
I modified the file ‘sonar-runner.bat’ to insert the following three lines:
- Display the ‘SONAR RUNNER HOME’ folder:
echo ”SONAR_RUNNER_HOME = %SONAR_RUNNER_HOME%”
- Define the analysis directory ‘PROJECT HOME’ in a ‘\Projects’ directory that we create under the previous folder, and display it:
set PROJECT_HOME=%SONAR_RUNNER_HOME%\Projects
echo ”PROJECT_HOME = %PROJECT_HOME%”
Configuring our initial analysis
To perform an analysis, the SonarQube Runner uses the file ‘sonar-project.properties’ located in our analysis environment: the directory ‘Projects’ created previously. Our goal is to analyze a Java application localized in this directory: ‘C:\SRC\Demo\J2EE\Source\Extranet\’.
If you do not have source code, you can retrieve an example of code and configuration analysis in this page: http://docs.codehaus.org/display/SONAR/Sonar+Project+Examples.
Without going into the details of the parameters for an analysis of Java code, we will create the file ‘sonar-project.properties’ with the following attributes:
- First, some mandatory data: a name / a key for this application, and a version number:
# required metadata
sonar.projectKey=EXT
sonar.projectName=Extranet
sonar.projectVersion=1.0
- The location of the files to analyze:
sonar.sources=C:/SRC/Demo/J2EE/Source/Extranet/WEB-INF/src/
- Last parameter: the programming language that will decide wich parser SonarQube will use to analyze this application:
#The value of the property must be the key of the language.
sonar.language=java
Packages and Java classes
One point to clarify if the Java language is unfamiliar to you. Java classes are organized into packages corresponding to the directories where the Java files are located. This package is shown in the first line of the java program.
In this example, the file ‘J2EEConnection.java’ is located into the directory ‘C:\SRC\Demo\J2EE\Source\Extranet\WEB-INF\src\com\extranet\common’. The root folder that we must precise in our configuration file, is the one immediately abote the directories / packages.
If you have any doubt, open a java file and look at the first line. Then navigate to the directory ‘father’ of this package: this is the one you must specify in the configuration file.
Normally, it is located in a ‘src’ directory, but sometimes this standard is not respected, and the packages are organized in different folders. In this case, you must specify them in the ‘sonar-project.properties’, separated by a comma, as in the following example:
sources=../src1/,../src2/
Running our first analysis
In a DOS command window, we run the ‘sonar-runner.bat’ file.
Then, we can see the two variables pointing to the directory SonarQube Runner and our space ‘Projects’, and the configuration file of our analysis:
Then, we can see the mention of the name of the project described in the ‘sonar-project.properties’ file and the directory corresponding to the parameter ‘sonar.sources’.
When you will see in this log the message ‘ANALYSIS SUCCESFULL’:
then you can visit the portal and see if SonarQube display the results of your first analysis.
We can see the project name and its version as defined in our configuration file ‘sonar-project.properties’.
We have seen here only the most simple parameters necessary to perform an analysis. There are many others you can look at on this page Analyzing with SonarQube Runner.
We were able to carry out our first analysis in the simplest possible way, by setting some parameters required in the ‘sonar-project.properties’ file, without the need to manage Maven xml files with a syntax sometimes complex.
However, this approach has a drawback: looking at errors in a DOS window is not very user-friendly, especially for long analysis and / or when having many errors.
Fortunately, we will see how to fix it, thanks to our friend Jenkins. This will be in our next post. See you soon.
This post is also available in Leer este articulo en castellano and Lire cet article en français.