|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +usage() { |
| 15 | + echo |
| 16 | + echo "options:" |
| 17 | + echo " -h Display help" |
| 18 | + echo " -u SonarQube Host URL" |
| 19 | + echo " -l SonarQube Login Credentials" |
| 20 | + echo " -k SonarQube Project Key" |
| 21 | + echo " -n SonarQube Project Name" |
| 22 | + echo " -t Number of threads (example: 1 or 2C)." |
| 23 | + echo |
| 24 | + echo "Important:" |
| 25 | + echo " The required parameters for publishing the coverage results to SonarQube:" |
| 26 | + echo " - Host URL" |
| 27 | + echo " - Login Credentials" |
| 28 | + echo " - Project Key" |
| 29 | + echo |
| 30 | +} |
| 31 | + |
| 32 | +execute() { |
| 33 | + SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" |
| 34 | + MAIN_POM="${SCRIPT_DIR}/../../pom.xml" |
| 35 | + # Check the syntax for the THREAD_COUNT variable |
| 36 | + if [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ "$THREAD_COUNT" =~ ^[0-9]+([.][0-9]+)+[C]?$ ]]; then |
| 37 | + THREADS="${THREAD_COUNT}" |
| 38 | + else |
| 39 | + THREADS=1 |
| 40 | + fi |
| 41 | + |
| 42 | + mvn -B -e -f "$MAIN_POM" clean verify -Pcodecoverage -fn -Dmaven.javadoc.skip=true -DskipShade -T "$THREADS" |
| 43 | + |
| 44 | + # If the required parameters are given, the code coverage results are uploaded to the SonarQube Server |
| 45 | + if [ -n "$SONAR_LOGIN" ] && [ -n "$SONAR_PROJECT_KEY" ] && [ -n "$SONAR_URL" ]; then |
| 46 | + mvn -B -e -Pcodecoverage -f "$MAIN_POM" sonar:sonar -Dsonar.projectName="$SONAR_PROJECT_NAME" \ |
| 47 | + -Dsonar.host.url="$SONAR_URL" -Dsonar.login="$SONAR_LOGIN" -Dsonar.projectKey="$SONAR_PROJECT_KEY" -T "$THREADS" |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +while getopts ":u:l:k:n:t:h" option; do |
| 52 | + case $option in |
| 53 | + u) SONAR_URL=${OPTARG:-} ;; |
| 54 | + l) SONAR_LOGIN=${OPTARG:-} ;; |
| 55 | + k) SONAR_PROJECT_KEY=${OPTARG:-} ;; |
| 56 | + n) SONAR_PROJECT_NAME=${OPTARG:-} ;; |
| 57 | + t) THREAD_COUNT=${OPTARG:-} ;; |
| 58 | + h) # Display usage |
| 59 | + usage |
| 60 | + exit |
| 61 | + ;; |
| 62 | + \?) # Invalid option |
| 63 | + echo "Error: Invalid option" |
| 64 | + exit |
| 65 | + ;; |
| 66 | + esac |
| 67 | +done |
| 68 | + |
| 69 | +# Start code analysis |
| 70 | +execute |
0 commit comments