Skip to content

Instantly share code, notes, and snippets.

@Badbond
Created March 16, 2023 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Badbond/0777680409ce28349c792416535940e0 to your computer and use it in GitHub Desktop.
Save Badbond/0777680409ce28349c792416535940e0 to your computer and use it in GitHub Desktop.
JaCoCo -- Generate HTML report from coverage data and (compiled) source code
#!/usr/bin/env bash
set -e -u -o pipefail
if [[ "${#}" -lt 2 ]]; then
echo "Usage: ${0} <path-to-jacococli.jar> [--sourcefiles-matcher=<string>] [ --classfiles-miles-matcher<string>] <source-roots>..."
exit 1
fi
JACOCO_CLI_LOCAL="$(realpath "${1}")"
shift
SOURCEFILES_MATCHER='*/src/main/java'
CLASSFILES_MATCHER='*/target/classes'
while [ "${#}" -gt 0 ]; do
case "${1}" in
--sourcefiles-matcher)
shift
SOURCEFILES_MATCHER="${1}"
;;
--classfiles-matcher)
shift
CLASSFILES_MATCHER="${1}"
;;
*)
break
;;
esac
done
if [[ "${#}" -ne 1 ]]; then
echo "No source roots passed."
exit 1
fi
pushd /tmp/jacoco-export
# Generate an HTML report based on the collected data. Note that this command
# assumes that the associated source code has been compiled (i.e. that the
# `target/classes` directories are present and populated).
java -jar "${JACOCO_CLI_LOCAL}" report jacoco.exec --html report \
$(find "${@:1}" -path "${CLASSFILES_MATCHER}" | sed 's/^/--classfiles /') \
$(find "${@:1}" -path "${SOURCEFILES_MATCHER}" | sed 's/^/--sourcefiles /')
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment