friends welcome back to devopsint in this video we are going to learn how to integrate sonar cube with java maven project using hello friends welcome back to devopsint in this video we are going to learn how to integrate sonar cube with java maven project using gitlab ci so firstly you can see here this is my sonar cube dashboard and now we are going to integrate sonar cube with java map and project using gitlab ci so here you can see my project with the name sonar cube and my repository i have few files yes rc readme.md and pom.xml and here i'm using droplet of digital ocean and now here i am using mobile extreme to connect my ubuntu terminal and now we are going to integrate sonar cube with java maven project using gitlab ci so first of all we need to install maven maven and java on our ubuntu so first of all you can see here this is my article so in my description box i will give this link so you can check my description box so now firstly we are going to install firstly we are going to update the system package then install maven so copy the command so i can say yes now clear the screen and the next is you can check maven version okay now clear the screen and now then if you want to remove maven then you can use this command then now install java so first of all you need to update your system package yeah it's done then we are going to install openjdk 11 or on our ubuntu 20.04 lts So yes, it's done. So let's check the version of our Java. So firstly clear the screen and run the command java fnfnversion.
So you can see here the version 11.0.13. Now clear the screen and the next is now downloading latest app.hm.avn for that firstly you need to inside that's the tmp directory. Then for download you can use this link now we need to extract this file for that we need to for that we are going to use this command user using tar Okay, now the next is for creating link So we are going to use this command, okay Now the next is setup apache maven environment variables. So for that firstly we need to run this command and then in this we need to add these lines in this particular directory to set environment.
Now save the file and exit. you need to give this permission to this particular directory then second for load the environment variable using source command and now let's check the version of our maven so you can see so So clear the screen and let's check the next step. Now it's done.
Now the next step is we need to install GitLab runner. So you can go to the official page of GitLab runner. Here you can see this is the official page of GitLab runner.
So for now we are going to install GitLab runner. So you can see the command for ubuntu. Firstly we need to add the official gitlab repository. So copy the command. So it's done.
Now install latest version of GitLab runner. Now it's done now clear the screen and now the next step is we need to register GitLab runner so for that you can see a register runner so click on register a runner and here if you scroll down for Linux you can use this command to register GitLab runner. Now it will ask enter the gitlab instance URL for example like this.
So firstly you need to go to your repository then settings then CI CD and here you can see a runner so click on expand. and here you can see the url so copy the url and paste it then registration token now enter a description for the runner so here my runner then tags devops so here i will choose shell so let's check our register successfully sorry our runner register successfully or not so you can see my runner So, sometime they give us error. So, the solution is click on edit and check indicates whether this runner can pick jobs without tax then save changes. So, now it's done.
So, the next step is now we are going to run our pipeline. So, firstly, we are going to create our pipeline. So, click on repository then files. then click on this plus icon then new file dot gitlab iphone c dot yml and here we need to add this code firstly before we are going to create a gitlab pipeline we need to add some variables here So for that firstly you need to go to settings then CI CD then here you can see your variables so click on expand and here you need to add three variables so the first variable is sonar URL so here you need to add these three variables so the first variable is sonar URL So here you need to copy your URL and paste here.
Remove this protect variable flag and click on add variable. Then second variable is your sonar login. that means your username then the third variable is sonar password then remove protected variable flag and then add variable okay Now it's done. Now we are going to run our pipeline. So for that copy this pipeline and paste here.
And here you can see we have two stages here. Firstly build and then test. See here in build we have script that maven package in artifacts the path then the second job you can see here if you add only so only main that means that the pipeline run for only main branch if you don't want then you can remove it and now we have already installed maven on our ubuntu instance so you can remove this also and now you can see in script maven verify sonar sonar so here the host url sonar url in sonar url your url we have added in our variable then sooner login and then sooner password now commit changes now let's check the pipeline so you can see our first job build is running so it's passed let's check the second job You can see a sonar master job is also running.
So you can see a job succeed. So let's check. So firstly go to your sonar cube dashboard and here click on projects. So here you can see last analysis 30 seconds ago.
So here you can see our project. So in this video we have learned how to integrate sonar cube with java maven project using gitlab CR. So thank you for watching our video.
I hope you like the video and it's helpful to you. So please like, share and subscribe our DevOpsTrend channel.