How To Setup Sonarqube With Angular Application (5 Minute Guide)

A simple guide to setup Sonarqube for your Angular project6 min


12
12 points
Setup Sonarqube for Angular application in 5 minutes

If you are trying to set up Sonarqube for your Angular codebase for code coverage analysis. You have to follow up on the below steps. Learn how to setup sonarqube with angular project quickly.

In this article, you will learn how to setup sonarqube for your Angular application easily.

Step 1: Install Sonarqube

For installing Sonarqube first download the Sonarqube from the official website

How To Setup Sonarqube With Angular Application (5 Minute Guide)
How To Setup Sonarqube With Angular Application (5 Minute Guide)

Step 2: Configure Sonarqube with your Angular Project

Run the following command

npm install sonar-scanner --save-dev

Step 3: Configure the SonarQube properties file on the Angular project

Create a file called sonar-project.properties in your Angular root directory and add the below attributes in the same file.

sonar.host.url=http://localhost:9000
sonar.login=admin
sonar.password=admin
sonar.projectKey=demo-app
sonar.projectName=demo-app
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.sources=src
sonar.exclusions=**/node_modules/**
sonar.tests=src
sonar.test.inclusions=**/*.spec.ts
sonar.typescript.lcov.reportPaths=coverage/lcov.info

Now you want to run code coverage on the Angular project, so Karma will do the same work for you.

Step 4: Integrate Angular project’s Karma code coverage with Sonarqube

Add a script called sonar to your package.json, You can give any key name!

"scripts": {
    "sonar": "sonar-scanner"
}

Finally, run the below command to integrate the Karma coverage with the Sonar server,

npm run sonar

Step 5: Configure the project from the SonarQube server and start an analysis

Once your sonar is started using the above command, hit this URL in your browser:

http://localhost:9000/

Enter your credentials (admin/admin in our case) and configure your project.

Give them a couple of minutes to index all the files on SonarQube.

Bingo You did it in 4 Minutes 51 seconds! Congrats! 👏👏👏

Cover Image Credits: Prepared by Author in MS PowerPoint


Here are the explanation of all key values explained in Step 3.

  • sonar.host.url=http://localhost:9000: Specifies the URL of the SonarQube server.
  • sonar.login=admin: Specifies the login username for connecting to the SonarQube server.
  • sonar.password=admin: Specifies the login password for connecting to the SonarQube server.
  • sonar.projectKey=demo-app: Specifies the project key for identifying the project in SonarQube.
  • sonar.projectName=demo-app: Specifies the project name in SonarQube.
  • sonar.projectVersion=1.0: Specifies the version of the project.
  • sonar.sourceEncoding=UTF-8: Specifies the encoding for the source files.
  • sonar.sources=src: Specifies the source directory of the project.
  • sonar.exclusions=**/node_modules/**: Specifies the directories to be excluded from analysis (e.g., node_modules).
  • sonar.tests=src: Specifies the directory containing the test files.
  • sonar.test.inclusions=**/*.spec.ts: Specifies the patterns for including test files.
  • sonar.typescript.lcov.reportPaths=coverage/lcov.info: Specifies the path to the TypeScript coverage report file.

Usage of Sonarqube in Angular Application.

SonarQube is a tool used to analyze the quality of code in an Angular application. It helps developers by providing insights into potential bugs, code smells, and security vulnerabilities in their code. By running the SonarQube analysis, developers can ensure that their Angular application meets the highest coding standards and best practices. SonarQube also provides valuable metrics and visualizations, making it easier to understand and improve the overall code quality. Overall, SonarQube is a valuable tool for maintaining the health and quality of an Angular application’s codebase.

You may also like,

If you are not sure how to install and run sonar

I can help you with it. The below steps will work for you!

  1. Download JDK 11 (Minimum Requirement to run SonarQube) from the official oracle website
  2. Download SonarQube, community edition, from this link
  3. Unzip SonarQube in C drive (You can choose any directory), let's say C:sonarqube
  4. Move to unzipped SonarQube binwindows-x86–64 folder lets say, C:sonarqubebinwindows-x86–64, after that find file RunSonarServer.bat, open it and change the java-11 path as per your system
  5. If SonarServer still gives you an error on “SonarQube requires Java 11 to start” after executing RunSonarServer.bat, even the Java-11 path is set up correctly, you need to find the wrapper.conf in the conf folder of SonarQube. Then change wrapper.java.command into the location where your JDK 11 is. e.g. wrapper.java.command=C:Program FilesJavajdk-11.0.8binjava
  6. Now open terminal, change the directory to unzipped SonarQube folderbinwindows-x86–64, let's say, C:sonarqubebinwindows-x86–64 and execute RunSonarServer.bat file.
  7. Once it has started, open the page in a browser http://localhost:9000
  8. Login using username=admin and password=admin
  9. After login, find the button Create New Project and click on it.
  10. Type project key and display name; let's say, demo-app, and click on Continue.
  11. On the next screen generate a token by typing any string in the field and clicking on the generate button.
  12. Once a token is generated, copy and keep that token handy.
  13. Click continue, and select Java as the main language and Maven as a builder of the project.
  14. Next, you will see the command to run an analysis on the project.
  15. Open a command prompt, go to the root folder of your project demo-app and execute that command.
  16. Once SonarQube has analyzed your project the browser page will be refreshed automatically to show you the report of analysis.

Wrapping up...

I saved you a lot of time, isn’t it? — Please comment your thoughts for the same!


I obtain an immense amount of satisfaction from helping others attain their goals and reach their potential through technology. Even if you wish to reach out and say "Hello", I sincerely appreciate all of the wonderful correspondence I receive each day from readers. You all enrich my life, and I hope I am able to do the same for you all as well.

If you find joy and value in what I do, please consider supporting my work with a donation — however much you can afford, it means and helps more than you can imagine.

You can give tips too using Buy me a coffee.


adsense


Discover more from 9Mood

Subscribe to get the latest posts sent to your email.


Like it? Share with your friends!

12
12 points

What's Your Reaction?

Lol Lol
6
Lol
WTF WTF
2
WTF
Cute Cute
3
Cute
Love Love
12
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
11
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Rakshit Shah

Legend

Hey Moodies, Kem chho ? - Majama? (Yeah, You guessed Right! I am from Gujarat, India) 25, Computer Engineer, Foodie, Gamer, Coder and may be a Traveller . > If I can’t, who else will? < You can reach out me by “Rakshitshah94” on 9MOodQuoraMediumGithubInstagramsnapchattwitter, Even you can also google it to see me. I am everywhere, But I am not God. Feel free to text me.

0 Comments

Leave a Reply

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment