Introduction:
Sonar is an open-source platform used for continuous inspection of code quality. It helps developers and development teams manage and improve the quality of their codebase to identify bugs, vulnerabilities, and code smells.Prerequisites:
- Liferay
- SonarQube 8.9
- Java 11.0.18
- Gradle
To integrate Sonar with Liferay workspace, follow the below steps:
- Setup the Sonar
- Integrating sonar with Liferay Workspace
- Download the sonar zip file from the sonar Download page.
- Extract the zip file once the download is completed in your preferred location or drive.
- Go to the bin folder and then based on the operating system go to the respective folder.
![sonar server path | Integrate Sonar With Liferay Workspace sonar server path](https://www.ignek.com/wp-content/uploads/2024/05/sonar-server-path-1024x139.webp)
- To start the Sonar application, click on StartSonar.
![sonar startup path | Integrate Sonar With Liferay Workspace sonar startup path](https://www.ignek.com/wp-content/uploads/2024/05/sonar-startup-path-1024x179.webp)
In the case of Linux, use sonar.sh to start the server through the command prompt.
- Once the “Sonar is up” message comes to the console or log file. It means the SonarQube server is up now. To check the log, Go to the logs folder. Open the sonar.log file.
- To test whether the sonar is installed correctly or not access the sonar server in the browser http://localhost:9000/ (Default port).
![sonar login page | Integrate Sonar With Liferay Workspace sonar login page](https://www.ignek.com/wp-content/uploads/2024/05/sonar-login-page-1024x458.webp)
To Log in to Sonar,Login: adminPassword: adminTo check the system information, click on the Administration > System > System Info.
2. Integrating sonar with Liferay Workspace:
- We add sonar properties to our liferay workspace project.
- Create sonar.gradle file in the same location as the liferay build.gradle file and add the following properties:
apply plugin: "org.sonarqube"
sonar {
properties {
// Insert other SonarQube properties here
property 'sonar.projectName', 'Proect Name'
property 'sonar.projectKey', 'project-name'
property 'sonar.host.url', 'http://localhost:9000'
property 'sonar.login', 'admin'
property 'sonar.password', 'admin'
}
}
- Add sonar-related properties in the build.gradle file.
// build.gradle
plugins {
id 'java'
id "jacoco"
id "org.sonarqube" version "4.0.0.2929"
}
jacoco {
toolVersion = "0.8.7"
}
dependencies {
// Use JUnit test framework
testCompile group: "junit", name: "junit", version: "4.12"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testIntegrationImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
testIntegrationRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.2.0'
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '5.2.0'
}
apply from: "$project.rootDir/sonar.gradle"
subprojects {
sonar {
properties {
property 'sonar.coverage.jacoco.xmlReportPaths', "${rootProject.projectDir}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
property 'sonar.cpd.exclusions','**/com/portal/**'
property 'sonar.coverage.exclusions','**/com/portal/**’
}
}
tasks.withType(Test).configureEach {
useJUnitPlatform()
}
}
jacocoTestReport {
reports {
xml.required = true
html.required = true
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}
}
- Save the file.
- Open the liferay workspace path in the command prompt.
- Run the command “.\gradlew.bat test codeCoverageReport sonar” .
- It will take some time to execute.
- Once you see “Build Successful”.
You can see the project result in Sonar sever in the path “http://localhost:9000/projects” with the Liferay workspace name.
Discover our Liferay Architecture Design service! We create tailored architectural plans for your portal, ensuring scalability, security, and efficiency. From initial concept to implementation, we’ll build a solid foundation for your Liferay platform. Let’s design your success together! Check out our “Hire Us” services to get started.