JaCoCo coverage report setups(exclude test classes) - java

Im generating a jacoco coverage report using the following target :
<target name="report" depends="test">
<!-- This task needs the collected execution data and ... -->
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" >
<exclude name="**/*Test*.class"/>
</fileset>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
The problem is that the report takes into account the code from unit tests and I consider this fact a mistake. This way, your line coverage percent and instruction coverage will be artificially increased(because test lines are considered 100% covered) and report correctness is pretty affected. I tried to add this tag
<exclude name="**/*Test*.class"/
under fileset tag, hoping that testClasses will be excluded, but it doesnt works. Do you have any ideas for my problem? I want to avoid programmatically
report modification.
Thanks!

You need to exclude the Test class files from the classfiles fileset:
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}">
<exclude name="**/*Test*.class"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
See documentation:
"Note that the classfiles and sourcefiles elements accept any Ant resource collection. Therefore also filtering the class file set is possible and allows to narrow the scope of the report, for example:
<classfiles>
<fileset dir="classes">
<include name="org/jacoco/examples/important/**/*.class"/>
</fileset>
</classfiles>
This is because the actual report is done from the classfiles. The sourcefiles are there to include highlighted source code in the report - as of course the human eye can't read compiled code.
Again from the documentation:
classfiles: Container element for Ant resources and resource
collections that can specify Java class files, archive files (jar,
war, ear etc. or Pack200) or folders containing class files. Archives
and folders are searched recursively for class files.
sourcefiles:
Optional container element for Ant resources and resource collections
that specify corresponding source files. If source files are
specified, some report formats include highlighted source code.
Source files can be specified as individual files or as source
directories.

Related

Jacoco: Add coverage for failed test cases

I am using Jacoco with Ant on a Java project which has a combination of unit tests as well as integration tests. We also have tests for REST APIs. In case there is a failure in any of the test cases, the entire class is not getting covered as part of Jacoco code coverage. Is there any configuration that I need to set to ensure that at least a part of code that has been executed as part of tests is covered? Here is my jacoco report XML
<jacoco:report>
<structure name="Jacoco Report">
<classfiles>
<fileset dir="${build.classes.dir}">
<exclude name="ut/**"/>
<exclude name="com/**/test/**"/>
</fileset>
</classfiles>
<sourcefiles>
<fileset dir="${build.source.dir}">
<exclude name="ut/**"/>
<exclude name="com/**/test/**"/>
</fileset>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${build.root.dir}/jacoco"/>
<csv destfile="${build.root.dir}/jacoco/report.csv"/>
<xml destfile="${build.root.dir}/jacoco/report.xml"/>
</jacoco:report>

ANT xmlproperty: demux properties with multiple values

I'm using cobertura to calculate test coverage. I want my ant script to echo coverage information about specific packages.
So far, I have:
<target name="coverage-check">
<loadfile property="coveragexml" srcFile="${coverage.report.dir}/coverage.xml">
<filterchain>
<linecontains negate="true">
<contains value="!DOCTYPE"/>
</linecontains>
</filterchain>
</loadfile>
<xmlproperty validate="false">
<string value="${coveragexml}"/>
</xmlproperty>
</target>
This works to load the various cobertura information into ant variables like: coverage.packages.package(name)=lots,of,package,names.
I'd like to find a way to appropriate a specific package name (from one variable) to coverage metrics stored in other variables. If I were using python, lisp, or the like, I'd zip them together, then search. I don't know how to do zipping or searching in ant.
I made an example with use of xmltask
<target name="xml-test">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpathref="extension.classpath"/>
<property name="xml.file" location="coverage.xml"></property>
<!-- package to search for -->
<property name="packageName" value="foo"></property>
<!-- extract for example line-rate and echo it -->
<xmltask source="${xml.file}">
<copy path="/coverage/packages/package[#name='${packageName}']/#line-rate" property="line-rate" />
</xmltask>
<echo>
Line Rate: ${line-rate}
</echo>
<!-- extract complete xml-block for package ${packageName}
and write it to other file
-->
<xmltask source="${xml.file}">
<copy path="/coverage/packages/package[#name='${packageName}']" buffer="foo-buffer" append="true" />
</xmltask>
<!-- write cut out to file -->
<xmltask dest="foo-coverage.xml">
<insert path="/" buffer="foo-buffer"/>
</xmltask>
</target>
The snipped copied from the source xml can unfortunately not echoed by default, but written to another file.
It's not a solution but an example that may be helps.
I think it would be less work to write a custom ant task your self.

How do I create a Jar file from my program

I am using eclipse, and I am having difficulty in creating jar files.
So I have codes like getClass().getResource("/imagesfolder/dog.jpg").
How would I create Jar files such that the folder containing my images will also be included. Because error occurs if my Jar file is not in my bin folder with the class files and the imagesfolder.
I tried File>Export>Java>Executable Jar>Save in desktop but when I double click it, it does not start. I tried cmd and it worked but with errors that it can't find imagesfolder.
How will I do a jar file in a separate directory that executes with a double click
I have a class TreeIcon; it uses two images, and I store them in a folder 'images' which is within the package of TreeIcon. For whatever reason, I made the package of TreeIcon spacecheck.images (it could just as easily have been com.mycompany.images). Therefore I used following code to access my images:
expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.GIF"));
where the 'images' here is the name of the folder containing just the images, not the one that is part of the package. I.E., in my tree structure for the program source, the images are in a folder named spacecheck.images.images.
Note that there's no slash at the start of my string; this means it references a path relative to that of the class. Putting the slash in front of the spec causes getResource to regard the path as absolute within your jar, so I could also have used the string "/spacecheck/images/images/Expanded.GIF".
ANT is The Way
In eclipse you can use Ant to build your .jar file.
From ant.apache.org
Apache Ant is a Java library and command-line tool whose mission is to
drive processes described in build files as targets and extension
points dependent upon each other. The main known usage of Ant is the
build of Java applications. Ant supplies a number of built-in tasks
allowing to compile, assemble, test and run Java applications. Ant can
also be used effectively to build non Java applications, for instance
C or C++ applications. More generally, Ant can be used to pilot any
type of process which can be described in terms of targets and tasks.
Ant is written in Java. Users of Ant can develop their own "antlibs"
containing Ant tasks and types, and are offered a large number of
ready-made commercial or open-source "antlibs".
Ant is extremely flexible and does not impose coding conventions or
directory layouts to the Java projects which adopt it as a build tool.
Software development projects looking for a solution combining build
tool and dependency management can use Ant in combination with Apache
Ivy.
The Apache Ant project is part of the Apache Software Foundation.
Search with google and you will find many documentation, I will show the basic way to do it.
The Build.xml file
First of all create a new file xml, for example "Build.xml" this will be the file that Ant will read.
The you start writing inside it this:
<?xml version="1.0" encoding="UTF-8"?>
This is the basic line you have always to include.
<project name="NameOfYourProject" default="try_jar" basedir=".">
This (with its closing tag </project> at the end of the file, is the main tag, declaring the name of the project and the first task (default) that will be executed, each task is something Ant will do, and is called "Target", you can create a single target that do everything or various target that do few things each, in this case you can create different "flow-chart" that ant will follow. For example I usually create 3 route for Ant: try_jar that is used just to try if all is working in the jar without doing many things, new_version_jar that is the same of try_jar but will update version number, will sign the jar and some other stuff, and javadoc that creates the javadoc for the project. Il will show you the basic try_jar.
<description>
This buildfile is used to build the jar of the game.
</description>
No need to explanation.
<!-- ================= File and Directory Names ==================== -->
<property name="src" location="${basedir}/src" />
<property name="conf" location="${basedir}/conf" />
<property name="build" location="${basedir}/build" />
<property name="dist" location="${basedir}/dist" />
<property name="app.name" value="MyAppName" />
<property name="dist.jarHome" value="${user.home}/MyApplicationMainFolder" />
<property name="app.version" value="0.2" />
<tstamp />
<property name="jar.name" value="${app.name}_${app.version}.${DSTAMP}.jar" />
<property name="jar.completePath" value="${dist.jarHome}/${jar.name}" />
Here we declare the base properties of the jar, we tell it where the source code is, where the build folder should be and so on. We also choose to put all the app in a folder in the base user home (in mac this is /user/UserName/) and create the name for the file that will include the name (obviously) the version and the time when this jar is created. This avoid duplicated or overriding of files that we may want to keep.
<property name="shared.lib" value="${basedir}/lib" />
Here we must specify the directory in which jar files needed by this plugin to run are stored
<!-- =============== Custom Ant Task Definitions =================== -->
<property name="compile.debug" value="true" />
<property name="compile.deprecation" value="false" />
<property name="compile.optimize" value="true" />
This are configuration params for ant
<!-- ================== External Dependencies ======================= -->
<property name="LWJGL" value="lwjgl.jar" />
<property name="Timer" value="timer.jar" />
<property name="Database" value="hsqldb.jar" />
<property name="Splice" value="jarsplice-0.25.jar" />
Here you must specify your external dependencies (something like easymock or powermock if you want to create a test target.
<!-- ================== Compilation Classpath ======================= -->
<path id="compile.classpath">
<fileset dir="${src}">
<include name="**/*.java" />
<exclude name="**/server/*.java"/>
</fileset>
<fileset dir="${shared.lib}">
<include name="**/*.jar" />
</fileset>
</path>
This is what And (with javac command) will build, you have to specify all the folders you want to build and to add (with <fileset>) any jar that is in the buildpath
<!-- =================== All Target ================================ -->
<!-- ================== Try_jar Target ============================ -->
<target name="try_jar" depends="compile, dist, clean_class_files, run" description="Clean build and dist directories, then compile, create jar and finally run" />
This is our target, as specified in "default" the first line, and will run this. Depends tells Ant what it should do before this target. A you can read it will compile, create the jar (dist), remove the class files, and run it.
<!-- ================== Clean Target ============================== -->
<target name="clean" description="Delete old build and dist directories">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
This is very clear, before to compile a new version we want to remove any old class file to avoid problems. You may think that this is never called, but pay attention to the dependencies of each target.
<!-- ================== Prepare Target ============================= -->
<target name="prepare" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${build}/classes" />
<mkdir dir="${build}/lib" />
<copy todir="${build}/lib">
<fileset dir="${shared.lib}" includes="${Timer}, ${LWJGL}, ${Database}" />
</copy>
</target>
This prepare the path, creating new needed folders (like build and build/classes) and adding the external dependencies jars.
<!-- ================== Compile Target =========================== -->
<target name="compile" depends="prepare" description="Compile Java sources">
<mkdir dir="${build}/classes" />
<javac srcdir="${src}" destdir="${build}/classes" encoding="8859_1" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" source="1.6" target="1.6">
<classpath refid="compile.classpath" />
</javac>
</target>
This is the main compiling target, as you can see it depends on prepare (that depends on clean) so until now we are using all <target> tags.
Ant compile .java files using <javac> tag, that needs to know where the source files are, where to put .class files, the encoding, and the three params we specified earlier.
<!-- =================== Dist Target ================================ -->
<target name="dist" description="Creates Jar archive">
<!-- Create the time stamp -->
<tstamp>
<format property="compile.timestamp" pattern="yyyyMMddHHmm" />
</tstamp>
<!-- update version in manifest -->
<replaceregexp file="${basedir}/manifestClient" match="Implementation-Version: .*" replace="Implementation-Version: ${app.version}.${compile.timestamp}" />
<!-- Create Jar file -->
<jar destfile="${jar.completePath}" manifest="${basedir}/manifest">
<fileset dir="${build}/classes" excludes="**/*.bak" />
<fileset dir="${src}/" excludes="mh/" />
<fileset dir="${shared.lib}/native/macosx" />
<zipfileset src="${shared.lib}/${Timer}" />
<zipfileset src="${shared.lib}/${LWJGL}" />
<zipfileset src="${shared.lib}/${Database}" />
</jar>
</target>
this creates the real jar. <tstamp> and <replaceregexp> are used to update the version in the manifest, you can remove them.
Jar tag will create the .jar file, we specified what files to add in the jar that will be avaible to my classes inside. We have also to specify a manifest that will discuss later.
<!-- =================== Delete .class Target===================== -->
<target name="clean_class_files" description="Delete .class files stored inside build directory and dist folder">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
This target deletes the two folder used to store .class files (and obviously all the files inside).
<!-- ================== Run Target =============================== -->
<target name="run" description="Run MagicHogwarts">
<java jar="${jar.completePath}" fork="true">
</java>
</target>
The end of our build.xml file, that is the run target that runs the jar.
This is almost what you need to compile and and the correct resources to a jar, if something is not like you are expecting, simply try few times and all will go right.
This is the manifest:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: package.to.class.with.main
Built-by: Gianmarco
Implementation-Vendor: Gianmarco
Implementation-Title: Title
I hope this will be useful to you.
I am editing few things to make the post better, but no contents will be different.

Ant File, include another file

Let me describe the scenario first. I have a main build file called main_build.xml. This file calls 4 other build files defined for sub projects, copies the Jars generated by sub project builds and then creates a WAR and finally EAR.
I have my classpath dependencies defined in a XML file named my_clspath.xml.
Now I have two questions:
1. How do I include my_clspath.xml file within the main_build.xml?
2. After including the my_clspath.xml in main_build, how do I make the classpath available for all 4 sub builds which are called from this main_build?.
my_clspath.xml content:
<fileset dir="../myApp_Ear/build/">
<include name="**/*.jar" />
</fileset>
<fileset dir="../myApp_Ear/lib/">
<include name="**/*.jar" />
</fileset>
Try
<project name="Your Project">
<include file="my_clspath.xml" />
<ant antfile="sub1.xml" inheritAll="true" inheritRefs="true" />
</project>
Also, you should set an id on the Filesets. Just having them in your Ant file does not do anything.
<fileset dir="../myApp_Ear/build/" id="my.EAR.fileset">
<include name="**/*.jar" />
</fileset>
Then, in sub1.xml, you can reference the filesets with <fileset refid="my.EAR.fileset" />
Hope this helps.
You should really take a look at the <subant> task. This does exactly what you're asking: You have a master build.xml file that's calling some sub-ant build files to build basic components.

How to perform Ant path mapping in a 'war' task?

I have several JAR file pattern sets, like
<patternset id="common.jars">
<include name="external/castor-1.1.jar" />
<include name="external/commons-logging-1.2.6.jar" />
<include name="external/itext-2.0.4.jar" />
...
</patternset>
I also have a 'war' task containing a lib element:
<lib dir="${src.dir}/jars">
<patternset refid="common.jars"/>
<patternset refid="web.jars"/>
...
</lib>
Like this however, I end up with WEB-INF/lib containing the subdirectories from my patterns:
WEB-INF/lib/external/castor-1.1.jar
WEB-INF/lib/external/...
Is there any way to flatten this, so the JAR files appear at the top-level under WEB-INF/lib, regardless of the directories specified in the patterns? I looked at mapper but it seems you cannot use them inside lib.
I was dissatisfied with having to manually generate a WAR directory or declare library subdirectories, so I came up with this. Seems to work, obviously you'll need to adjust it to your own needs:
<war destfile="build/output.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}" />
<mappedresources>
<fileset dir="${lib.dir}" includes="**/*.jar" />
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="WEB-INF/lib/*.jar" />
</chainedmapper>
</mappedresources>
</war>
You can try to use mappedresources element (Ant 1.8+)
<mappedresources>
<!-- Fileset here -->
<globmapper from="external/*.jar" to="*.jar"/>
</mappedresources>
http://ant.apache.org/manual/Types/resources.html#mappedresources
If you have a typical web project it is better to split the web-app libraries and general-purpose libraries in different folders and leave WEB-INF/lib to have only those needed at runtime. This way you'll avoid collisions and also your project will look much clearer to other developers.

Categories

Resources