Skip to content

Instantly share code, notes, and snippets.

@andrioli
Created November 22, 2014 22:28
Show Gist options
  • Save andrioli/d7faee5cef5776abe367 to your computer and use it in GitHub Desktop.
Save andrioli/d7faee5cef5776abe367 to your computer and use it in GitHub Desktop.
JaCoCo offline instrumentation command-line support

This is a general purpose Ant build file that execute JaCoCo offline instrumentation.

Requirement: Just create a environment variable JACOCO_LIBS that point to a directory with the JaCoCo jar files.

There is also two BASH scripts to help setting the necessary build variables. Execute the BASH script with -h option for more information.

<?xml version="1.0" encoding="UTF-8"?>
<project name="JaCoCo Offline Instrumentation" basedir="${basedir}" xmlns:jacoco="antlib:org.jacoco.ant">
<property environment="env"/>
<property name="encoding" value="UTF-8" />
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="${env.JACOCO_LIBS}/jacocoant.jar" />
</taskdef>
<target name="instrument">
<echo message="classes.dir=${classes.dir}"/>
<echo message="dest.dir=${dest.dir}"/>
<jacoco:instrument destdir="${dest.dir}">
<fileset dir="${classes.dir}" includes="**/*.class" />
</jacoco:instrument>
</target>
<target name="report">
<echo message="classes.dir=${classes.dir}"/>
<echo message="dest.dir=${dest.dir}"/>
<echo message="sources.dir=${sources.dir}"/>
<echo message="exec.file=${exec.file}"/>
<echo message="encoding=${encoding}"/>
<jacoco:report>
<executiondata>
<file file="${exec.file}" />
</executiondata>
<structure name="JaCoCo Report">
<classfiles>
<fileset dir="${classes.dir}" />
</classfiles>
<sourcefiles encoding="${encoding}">
<fileset dir="${sources.dir}" />
</sourcefiles>
</structure>
<csv destfile="${dest.dir}/report.csv" />
</jacoco:report>
</target>
</project>
#!/bin/bash
usage()
{
cat << EOF
usage: $0 options
This script instrument classes with JaCoCo
OPTIONS:
-h Show this message
-c Directory with classes to be instrumented (required)
-d Destination directory to put instrumented classes (required)
EOF
}
CLASSES=
DEST=
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
while getopts “hc:d:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
c)
CLASSES=$OPTARG
;;
d)
DEST=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $CLASSES ]] || [[ -z $DEST ]]
then
usage
exit 1
fi
if [ ! -d "$CLASSES" ]; then
echo "$CLASSES is not a directory"
exit 1
fi
if [ ! -d "$DEST" ]; then
echo "$DEST is not a directory"
exit 1
fi
ant -f ${DIR}/ant.xml instrument -Dclasses.dir=$CLASSES -Ddest.dir=$DEST -Dbasedir=${PWD}
#!/bin/bash
usage()
{
cat << EOF
usage: $0 options
This script generate a coverage report with JaCoCo
OPTIONS:
-h Show this message
-c Directory with classes (required)
-d Destination directory to put the generated report
-s Directory with sources
-f execution file (required)
-z define encoding
EOF
}
CLASSES=
DEST=
SOURCES=
FILE=
ENCODING=
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
while getopts “hc:d:s:f:z:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
c)
CLASSES=$OPTARG
;;
d)
DEST=$OPTARG
;;
s)
SOURCES=$OPTARG
;;
f)
FILE=$OPTARG
;;
z)
ENCODING=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $CLASSES ]] || [[ -z $FILE ]]
then
usage
exit 1
fi
if [ "$DEST" == "" ]; then
DEST=`pwd`
fi
if [ ! -d "$CLASSES" ]; then
echo "$CLASSES is not a directory"
exit 1
fi
if [ ! -d "$DEST" ]; then
echo "$DEST is not a directory"
exit 1
fi
if [ ! -f "$FILE" ]
then
echo "$FILE is not a file"
exit 1
fi
if [[ -z $ENCODING ]]
then
ant -f ${DIR}/ant.xml report -Dclasses.dir=$CLASSES -Ddest.dir=$DEST -Dsources.dir=$SOURCES -Dexec.file=$FILE -Dbasedir=${PWD}
else
ant -f ${DIR}/ant.xml report -Dclasses.dir=$CLASSES -Ddest.dir=$DEST -Dsources.dir=$SOURCES -Dexec.file=$FILE -Dbasedir=${PWD} -Dencoding=$ENCODING
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment