Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions dev/code-coverage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# Clover code analysis

The `run-coverage.sh` script runs maven with the code-coverage profile which generates
the clover code analysis data.
If the necessary parameters are given it also uploads the results to SonarQube.

## Running code coverage

The coverage results can be found under `target/clover/index.html` and here is how you can run the clover code analysis:

```./dev/code-coverage/run-coverage.sh```

## Publishing coverage results to SonarQube

The required parameters for publishing to SonarQube are:

- host URL,
- login credentials,
- project key

The project name is an optional parameter.

Here is an example command for running and publishing the coverage data:

`./dev/code-coverage/run-coverage.sh -l ProjectCredentials -u https://exampleserver.com
-k Project_Key -n Project_Name`
72 changes: 72 additions & 0 deletions dev/code-coverage/run-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


usage() {
echo
echo "options:"
echo " -h Display help"
echo " -u SonarQube Host URL"
echo " -l SonarQube Login Credentials"
echo " -k SonarQube Project Key"
echo " -n SonarQube Project Name"
echo " -t Number of threads (example: 1 or 2C)."
echo
echo "Important:"
echo " The required parameters for publishing the coverage results to SonarQube:"
echo " - Host URL"
echo " - Login Credentials"
echo " - Project Key"
echo
}

execute() {
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
MAIN_POM="${SCRIPT_DIR}/../../pom.xml"
# Check the syntax for the THREAD_COUNT variable
if [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)+[C]?$ ]]; then
THREADS="${THREAD_COUNT}"
else
THREADS=1
fi

mvn -B -e -Pcode-coverage -f "$MAIN_POM" clean install -Dparallel-tests -Dscale -fn -T "$THREADS"
mvn -B -e -Pcode-coverage -f "$MAIN_POM" clover:aggregate clover:clover -T "$THREADS"

# If the required parameters are given, the code coverage results are uploaded to the SonarQube Server
if [ -n "$SONAR_LOGIN" ] && [ -n "$SONAR_PROJECT_KEY" ] && [ -n "$SONAR_URL" ]; then
mvn -B -e -Pcode-coverage -f "$MAIN_POM" sonar:sonar -Dsonar.projectName="$SONAR_PROJECT_NAME" \
-Dsonar.host.url="$SONAR_URL" -Dsonar.login="$SONAR_LOGIN" -Dsonar.projectKey="$SONAR_PROJECT_KEY" -T "$THREADS"
fi
}

while getopts ":u:l:k:n:t:h" option; do
case $option in
u) SONAR_URL=${OPTARG:-} ;;
l) SONAR_LOGIN=${OPTARG:-} ;;
k) SONAR_PROJECT_KEY=${OPTARG:-} ;;
n) SONAR_PROJECT_NAME=${OPTARG:-} ;;
t) THREAD_COUNT=${OPTARG:-} ;;
h) # Display usage
usage
exit
;;
\?) # Invalid option
echo "Error: Invalid option"
exit
;;
esac
done

# Start code analysis
execute
55 changes: 55 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
<maven-jxr-plugin.version>2.3</maven-jxr-plugin.version>
<maven-findbugs-maven-plugin.version>3.0.1</maven-findbugs-maven-plugin.version>
<maven-owasp-plugin.version>6.5.3</maven-owasp-plugin.version>
<!-- Code coverage properties -->
<maven-clover-plugin.version>4.4.1</maven-clover-plugin.version>
<maven-sonar-plugin.version>3.9.1.2184</maven-sonar-plugin.version>

<!-- Licensing properties (for license-maven-plugins) -->
<license.header>misc/header.txt</license.header>
Expand Down Expand Up @@ -635,6 +638,58 @@
<protoc.version>2.5.0.2</protoc.version>
</properties>
</profile>

<profile>
<id>code-coverage</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<cloverDatabase>${project.build.directory}/clover/code-coverage.db</cloverDatabase>
<sonar.core.codeCoveragePlugin>clover</sonar.core.codeCoveragePlugin>
<sonar.clover.version>${maven-clover-plugin.version}</sonar.clover.version>
<sonar.clover.reportPath>${project.build.directory}/clover/clover.xml</sonar.clover.reportPath>
<sonar.surefire.reportsPath>${project.build.directory}/surefire-reports</sonar.surefire.reportsPath>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.openclover</groupId>
<artifactId>clover-maven-plugin</artifactId>
<version>${maven-clover-plugin.version}</version>
<configuration>
<cloverDatabase>${cloverDatabase}</cloverDatabase>
<cloverMergeDatabase>${cloverDatabase}</cloverMergeDatabase>
<outputDirectory>${project.build.directory}/clover</outputDirectory>
<alwaysReport>true</alwaysReport>
<generateHistorical>false</generateHistorical>
<generateHtml>true</generateHtml>
<generateXml>true</generateXml>
<includesTestSourceRoots>true</includesTestSourceRoots>
<includesAllSourceRoots>true</includesAllSourceRoots>
<includes>
<include>**/org/apache/**/*.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>clover-setup</id>
<goals>
<goal>setup</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${maven-sonar-plugin.version}</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencyManagement>
Expand Down