Running SonarQube Analysis for Angular and Java Applications

Subash Pradhan
4 min readSep 16, 2020

--

Step 1: ( Install Sonarqube )

For installing Sonarqube first download the Sonarqube from https://www.sonarqube.org/downloads/.

There will be multiple editions available.For free use, you just click on the Community Edition.

1)Make sure you have JVM installed and running in your machine. For some time you may get JVM timeout or loading error, no worries just rerun the StartSonar.bat file.

2) Post download extracts the ZIP file to any folder and then run the StartSonar.bat from sonarqube\bin\windows-x86–64. Once the batch file runs you see the sonarqube instance is running if there are no errors.

3) If the 9000 port is already been used then the StartSonar.bat file execution will fail. To overcome this issue you can use any free available port in sonar.web.port present in sonarqube\conf\sonar.properties and uncomment or enable it. Rerun the StartSonar.bat file.

4) Now you can access the Sonarqube from your browser using the default port http://localhost:9000. You can use the default credentials i.e admin and admin as the Login name and Password respectively. Once you login to the Sonarqube you see the below window

Step 2: ( Configure Sonar with Angular )

  1. Configuring the Sonar with Angular application and to do this we need sonar-scanner node package in the Angular application. And to include this, please follow any one of below methods,Include sonar-scanner as devDependencies in the package.json and then run

npm install or npm i OR

npm install sonar-scanner — save-dev

2. Install the Karma JUnit reporter as a build dependency.

npm i — save-dev karma-junit-reporter

3. Configure Karma to output unit test and code coverage output using Cobertura and LCOV by editing the karma.config.js file.

4. Modify your test NPM script to include code coverage in your package.json file. If you configured your project to PhantomJs, you’ll need to make that switch in the browsers argument.

“test”: “ng test — code-coverage — single-run — browsers=Chrome — reporters=progress,junit”

5. Create a file called sonar-project.properties in your Angular root directory. This is the standard naming for this file, but if you need to support multiple build configurations you can have more than one of these files.

It’s important to note the exclusions, you may need to add additional files to expand exclusions from code coverage results.

6. Run the test cases and validate if the following files are generating by below commands.

npm test

Files will be generated as:

./Tests-{Browser}_{OperatingSystem}.xml
./coverage/cobertura-coverage.xml
./coverage/lcov.info

7. We have both Karma code coverage and Sonar server ready . We will push the result to local sonar server by using the below commands.

npm run sonar

8. Verify that you can see the projects analysis including code coverage using URL http://localhost:9000/projects

Step 3: ( Configure Sonar with JAVA )

  1. Follow the sonarqube installation part if you have not done it earlier . Those steps will be similar irrespective of any particular language.
  2. Will setup JaCoCo — a tool, which will help us generate coverage report for the codebase. This report will contain detailed information as to which classes/paths have been covered by tests, and which are not covered.

Add a dependency plugin in build.gradle file and specify the path where the Jacoco html report will reside.

3. Create a file called sonar-project.properties in your Java project root directory. This is the standard naming for this file, but if you need to support multiple build configurations you can have more than one of these files.

It’s important to note the exclusions, you may need to add additional files to expand exclusions from code coverage results.

Include java source to analyze the code.

sonar.java.binaries=build/libs

4. Add a sonar plugin in build.gradle file to run and analyze the application on local workspace. Any one of the below plugin command should work .

id “org.sonarqube” version “3.0”

5. Run the gradlew clean build command which will build the application .

6. Run gradlew sonarqube command which willpublish the result to local sonarqube server.

--

--