Skip to content

Commit 1761999

Browse files
xzel23svanteschubert
authored andcommitted
add sonarcloud analysis
1 parent 96948c6 commit 1761999

File tree

2 files changed

+144
-11
lines changed

2 files changed

+144
-11
lines changed

.github/workflows/maven.yml

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,59 @@
33

44
name: Java CI with Maven
55

6-
on: [push, pull_request]
6+
on: [ push, pull_request ]
77

88
jobs:
99
build:
10-
1110
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Set up JDK 11
14+
uses: actions/setup-java@v3
15+
with:
16+
java-version: '11'
17+
distribution: 'adopt'
18+
cache: maven
19+
- name: Build with Maven
20+
run: mvn -B package -Ppedantic --file pom.xml
1221

22+
#
23+
# Static analysis with SonarQube
24+
#
25+
sonarcloud:
26+
name: SonarCloud
27+
needs: build
28+
runs-on: ubuntu-latest
29+
# Only run SonarCloud on the main branch
30+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
1331
steps:
14-
- uses: actions/checkout@v3
15-
- name: Set up JDK 11
16-
uses: actions/setup-java@v3
17-
with:
18-
java-version: '11'
19-
distribution: 'adopt'
20-
cache: maven
21-
- name: Build with Maven
22-
run: mvn -B package -Ppedantic --file pom.xml
32+
- uses: actions/checkout@v4
33+
with:
34+
fetch-depth: 0
35+
- name: Set up JDK 11
36+
uses: actions/setup-java@v4
37+
with:
38+
java-version: 11
39+
distribution: 'adopt'
40+
- name: Cache SonarCloud packages
41+
uses: actions/cache@v4
42+
with:
43+
path: ~/.sonar/cache
44+
key: ${{ runner.os }}-sonar
45+
restore-keys: ${{ runner.os }}-sonar
46+
- name: Cache Maven packages
47+
uses: actions/cache@v4
48+
with:
49+
path: ~/.m2
50+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
51+
restore-keys: ${{ runner.os }}-m2
52+
- name: Build and analyze
53+
env:
54+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
55+
run: >
56+
mvn -B verify
57+
-Dsonar.host.url=https://sonarcloud.io
58+
-Dsonar.projectKey=xzel23_odftoolkit
59+
-Dsonar.organization=xzel23
60+
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate/jacoco.xml
61+
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar

pom.xml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
<maven.javadoc.skip>false</maven.javadoc.skip>
5757
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>
5858
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss</maven.build.timestamp.format>
59+
<!-- JaCoCo Properties -->
60+
<jacoco.version>0.8.13</jacoco.version>
61+
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
62+
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
63+
<sonar.coverage.jacoco.xmlReportPaths>${project.reporting.outputDirectory}/jacoco-ut/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
64+
<sonar.language>java</sonar.language>
5965
</properties>
6066
<!-- At root project level defining dependencies useable across all projects.
6167
Making version management easy and consistent! -->
@@ -468,8 +474,96 @@
468474
<artifactId>maven-resources-plugin</artifactId>
469475
<version>3.3.1</version>
470476
</plugin>
477+
<plugin>
478+
<groupId>org.jacoco</groupId>
479+
<artifactId>jacoco-maven-plugin</artifactId>
480+
<version>${jacoco.version}</version>
481+
<executions>
482+
<!--
483+
Prepares the property pointing to the JaCoCo propruntime agent which
484+
is passed as VM argument when Maven the Surefire plugin is executed.
485+
-->
486+
<execution>
487+
<id>pre-unit-test</id>
488+
<goals>
489+
<goal>prepare-agent</goal>
490+
</goals>
491+
<configuration>
492+
<!-- Sets the path to the file which contains the execution data. -->
493+
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
494+
<!--
495+
Sets the name of the property containing the settings
496+
for JaCoCo runtime agent.
497+
-->
498+
<propertyName>surefireArgLine</propertyName>
499+
</configuration>
500+
</execution>
501+
<!--
502+
Ensures that the code coverage report for unit tests is created after
503+
unit tests have been run.
504+
-->
505+
<execution>
506+
<id>post-unit-test</id>
507+
<phase>test</phase>
508+
<goals>
509+
<goal>report</goal>
510+
</goals>
511+
<configuration>
512+
<!-- Sets the path to the file which contains the execution data. -->
513+
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
514+
<!-- Sets the output directory for the code coverage report. -->
515+
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
516+
<formats>
517+
<format>XML</format>
518+
<format>CSV</format>
519+
</formats>
520+
</configuration>
521+
</execution>
522+
</executions>
523+
</plugin>
471524
</plugins>
472525
</pluginManagement>
526+
<plugins>
527+
528+
<plugin>
529+
<groupId>org.jacoco</groupId>
530+
<artifactId>jacoco-maven-plugin</artifactId>
531+
<executions>
532+
<!-- Aggregate reports from all modules -->
533+
<execution>
534+
<id>merge-results</id>
535+
<phase>verify</phase>
536+
<goals>
537+
<goal>merge</goal>
538+
</goals>
539+
<configuration>
540+
<fileSets>
541+
<fileSet>
542+
<directory>${project.basedir}</directory>
543+
<includes>
544+
<include>**/target/coverage-reports/jacoco-ut.exec</include>
545+
</includes>
546+
</fileSet>
547+
</fileSets>
548+
<destFile>${project.build.directory}/coverage-reports/merged-jacoco-ut.exec</destFile>
549+
</configuration>
550+
</execution>
551+
<execution>
552+
<id>aggregate-report</id>
553+
<phase>verify</phase>
554+
<goals>
555+
<goal>report-aggregate</goal>
556+
</goals>
557+
<configuration>
558+
<dataFileIncludes>
559+
<dataFileInclude>**/coverage-reports/merged-jacoco-ut.exec</dataFileInclude>
560+
</dataFileIncludes>
561+
<outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
562+
</configuration>
563+
</execution>
564+
</executions>
565+
</plugin>
566+
</plugins>
473567
</build>
474568
<profiles>
475569
<profile>

0 commit comments

Comments
 (0)