Skip to content

Commit 7ddbeda

Browse files
authored
Merge pull request #27 from conorheffron/feat/sonar
Create sonarcloud.yml
2 parents 355b88d + 6e389eb commit 7ddbeda

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

.github/workflows/sonarcloud.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: SonarQube
2+
permissions:
3+
contents: read
4+
on:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
types: [opened, synchronize, reopened]
10+
jobs:
11+
build:
12+
name: Build and analyze
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
18+
- name: Set up JDK 21
19+
uses: actions/setup-java@v4
20+
with:
21+
java-version: 21
22+
distribution: 'zulu' # Alternative distribution options are available
23+
- name: Cache SonarQube packages
24+
uses: actions/cache@v4
25+
with:
26+
path: ~/.sonar/cache
27+
key: ${{ runner.os }}-sonar
28+
restore-keys: ${{ runner.os }}-sonar
29+
- name: Cache Gradle packages
30+
uses: actions/cache@v4
31+
with:
32+
path: ~/.gradle/caches
33+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
34+
restore-keys: ${{ runner.os }}-gradle
35+
- name: Build and analyze
36+
env:
37+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
38+
run: ./gradlew build sonar --info

build.gradle.kts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ plugins {
88
java
99
id("io.freefair.lombok") version "8.0.1" // Lombok plugin for Gradle
1010
id("com.adarshr.test-logger") version "4.0.0"
11+
id("jacoco")
12+
id("org.sonarqube") version "6.3.1.5724" // Use the latest version
13+
}
14+
15+
sonar {
16+
properties {
17+
property("sonar.projectKey", "conorheffron_shoopingcart-java")
18+
property("sonar.organization", "conorheffron")
19+
}
1120
}
1221

1322
repositories {
@@ -80,7 +89,7 @@ dependencies {
8089
}
8190

8291
group = "com.siriusxm.example.cart"
83-
version = "1.0.7-RELEASE"
92+
version = "1.0.8-RELEASE"
8493
description = "shoppingcart"
8594
java.sourceCompatibility = JavaVersion.VERSION_21
8695

@@ -103,3 +112,42 @@ tasks {
103112
options.compilerArgs.add("-parameters") // Optional: Enables parameter names in reflection
104113
}
105114
}
115+
116+
tasks.withType<JacocoReport> {
117+
dependsOn(tasks.withType<Test>()) // Ensure tests run before generating the report
118+
119+
reports {
120+
xml.required.set(true) // Enable XML report generation
121+
html.required.set(true) // Enable HTML report generation
122+
csv.required.set(false) // Disable CSV report generation
123+
}
124+
125+
// Configure the source sets for coverage
126+
val sourceSets = project.extensions.getByType<org.gradle.api.tasks.SourceSetContainer>()
127+
sourceDirectories.setFrom(sourceSets["main"].allSource.srcDirs)
128+
classDirectories.setFrom(
129+
fileTree("${buildDir}/classes/java/main") {
130+
exclude("**/generated/**") // Exclude generated code if necessary
131+
}
132+
)
133+
executionData.setFrom(fileTree(buildDir) {
134+
include("**/jacoco/test.exec") // Include JaCoCo execution data
135+
})
136+
}
137+
138+
// Optional: Add a custom task to aggregate coverage reports for multi-module projects
139+
tasks.register<JacocoReport>("jacocoAggregateReport") {
140+
dependsOn(subprojects.map { it.tasks.withType<Test>() })
141+
142+
reports {
143+
xml.required.set(true)
144+
html.required.set(true)
145+
}
146+
147+
val executionDataFiles = subprojects.flatMap { subproject ->
148+
subproject.fileTree(subproject.buildDir) {
149+
include("**/jacoco/test.exec")
150+
}
151+
}
152+
executionData.setFrom(executionDataFiles)
153+
}

0 commit comments

Comments
 (0)