Jacoco: unable to read execution data file, ant task - java

Info:
java version: 1.8.0_66
ant version: 1.9.6
What I want to do:
Provide a code coverage report for the server's code that is running on AWS windows 2k12 server.
What I did:
Stop the server completely.
Put jacocoagent.jar into server's bin folder. Note: this is inside Program Files folder
Append -javaagent settings to JAVA_OPTS that is used during server start up.
Start the server.
Run my sample test from my local laptop.
Stop the server completely. This produced a 184kb jacoco.exec.
Copied my build.xml to the same directory where the jacoco.exec is located. C:/path/to/exec/jacoco.exec
Copied jacocoant.jar to C:/path/to/jacocoant.jar
cd into C:/path/to/exec/ and run command "ant"
Result:
Got error unable to read execution data file C:/path/to/exec/jacoco.exec
Build.xml:
<project name="Example" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant">
<description>
Example Ant build file that demonstrates how a JaCoCo coverage report
can be itegrated into an existing build in three simple steps.
</description>
<property name="result.dir" location="." />
<property name="result.classes.dir" location="${result.dir}/path/to/classes" />
<property name="result.report.dir" location="${result.dir}/report" />
<property name="result.exec.file" location="${result.dir}/jacoco.exec" />
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="../jacocoant.jar" />
</taskdef>
<target name="clean">
<delete dir="${result.report.dir}" />
</target>
<target name="report">
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!-- 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 Code Coverage Report">
<classfiles>
<fileset dir="${result.classes.dir}" >
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</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>
I am not sure if the problem is with exec file (it is corrupted maybe) or is with my entire setup.
Any help to identify and help solving the problem is appreciated!!!
Thanks!

I got this when using gradle and jaCoCo.
I deleted the build/ directory and reran ./gradlew jacocoTestReport, this time passing.

Similar to #sofia's solution but for gradle:
I removed the version after tool version. Instead of
allprojects {
jacoco {
toolVersion = '0.7.1.201405082137'
}
}
I used the following
allprojects {
jacoco {
toolVersion = '0.7.1'
}
}

I have the same problem recently, it took me long time to figure this out. I hope how I fix this will help you or someone else.
Use ant -verbose report to see the detailed information. I used "ant -verbose report" and got this message: "java.io.IOException: Incompatible version 1007".
As I am using maven, I added the following lines to my pom.xml. Note: version is the same as your javaagent's version.
<dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.ant</artifactId> <version>0.7.4.201502262128</version> </dependency>
In the end, the report is successfully generated.

Related

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.

java.lang.NoClassDefFoundError, Ant task could not see classes in jar inside jar

I wrote Java tool (in this case is iOffloadMaker) which also contains own-defined Ant Task as the main launcher to launch the tool with Ant. I bundled all external jar libraries into delivered my tool's jar. I also provide a simple Ant build.xml file to launch my tool:
<?xml version="1.0"?>
<project name="TestBound" default="main" basedir=".">
<!-- Sets varables which can later be used. -->
<property name="src.dir" value="src" />
<property name="build.dir" value="bin" />
<property name="dist.dir" value="dist" />
<property name="libs.dir" value ="libs" />
<path id="build.classpath">
<fileset dir="${libs.dir}">
<include name="**/*.jar"/>
</fileset>
<pathelement location=".\iOffloadMaker.jar"/>
</path>
<!-- define offload maker task -->
<taskdef name="iOffloadMaker" classname="com.richardle.ioffload.OffloadMakerTask" classpathref= "build.classpath"/>
...
<!-- Creates the build, docs and dist directory-->
<target name="modify" description="modify the source code" >
<iOffloadMaker projectFolder="${basedir}">
</iOffloadMaker>
</target>
...
<target name="main" depends="compile">
<description>Main target</description>
</target>
</project>
The thing is that, Ant Task could not refer to classes in jar libraries inside my tool jar file. Hence, when I run ant, it throw exception as
D:\SOFTWARE\Android\TestBound>ant modify
Buildfile: D:\SOFTWARE\Android\TestBound\build.xml
modify:
[iOffloadMaker] Offload Maker is executing...
BUILD FAILED
D:\SOFTWARE\Android\TestBound\build.xml:40: java.lang.NoClassDefFoundError: org/
xmlpull/v1/XmlPullParserException
at com.richardle.ioffload.offloadmaker.ApplicationProject.<init>(Applica
tionProject.java:30)
at com.richardle.ioffload.offloadmaker.OffloadMaker.execute(OffloadMaker
.java:121)
at com.richardle.ioffload.OffloadMakerTask.execute(OffloadMakerTask.java
:26)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
I am sure that all necessary libs are bundled into iOffloadMaker.jar. The thing is that Ant Task loader could not see classes of dependency jars inside my jar file.
If I do not bundled all dependencies into jar, but deliver them in dependency folder along with iOFfloadMaker.jar, it work as I expected. But I want to bundle all dependencies and my tool source code into one delivery jar file.
Is there a solution for this problem?
The standard Java classloader isn't able to handle recursive JARs (i.e. JARs that are bundled inside of other JARs).
This page lists a couple of solutions for that: http://www.jdotsoft.com/JarClassLoader.php
Please let me know which one worked for you.

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.

How to Run TestNG Tests on Jenkins

I'm trying to run TestNG tests (in a contained Java project) from Jenkins but having no luck.
It appears as though the TestNG plugin for Jenkins (https://wiki.jenkins-ci.org/display/JENKINS/testng-plugin) only publishes the results of TestNG tests, but doesn't actually run test classes... or am I wrong?
In any case, how do I actually run TestNG tests in a TestNG project with Jenkins, or is that even possible? Do I have to use a command line statement or batch file (on Windows Server 2008), for example?
Any help much appreciated.
Note I tried entering a post-build command line in Jenkins for the project to run TestNG tests but had a hard time with class paths not being found for TestNG. I posted an earlier question about running TestNG from the command line which I couldn't get working, so I've given up on that route:
How to run TestNG from command line
There are two steps to accomplished this task:-
Step 1:-
Go to localhost:8080/configure (Jenkins configure section)
Now go to JDK section and uncheck Install automatically (If you don't uncheck that then it will download latest java every time whenever it is available, and can cause for build failed)
put JAVA_HOME in name section and jdk home path in JAVA_HOME section
Apply and save
Step 2:-
Go to Jenkins and add new Item, also select "Free Style Project" and click on Ok.
Click on "Advanced in "Advanced Project Options"
Now check option: - "Use custom workspace" and specify your project absolute path in Directory section
Apply
Now to go "Build" and select "Execute windows batch command"
Here in command column give the file name of your batch file
Apply and save
Now go to the Jenkins and select your Jenkins project and click on Build :)
I use gradle to run my testNG tests from Jenkins.
Take a look at the gradle docs.
I run the testNG tests using configuration xml files.
Take a look at the testNG docs.
There is quite a lot to cover so I suggest reading these sources but I'll provide some relevant pieces from one of my configurations.
The relevant parts from my build.gradle
tasks.withType(Test) {
useTestNG {
useDefaultListeners = true
}
options {
outputDirectory = file('test-report')
listeners << 'org.uncommons.reportng.JUnitXMLReporter'
}
testLogging.showStandardStreams = true
systemProperties System.getProperties()
systemProperty "org.uncommons.reportng.escape-output", "false"
systemProperty "org.uncommons.reportng.title", "Test Report"
ignoreFailures = true
}
task Smoke_Test(type: Test) {
description "SmokeTest"
options.suites("resources/testng-smoketest.xml")
ignoreFailures = false
}
My testNG xml as referenced above 'testng-smoketest.xml'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Tests" >
<test name="BootCheck" parallel="false" thread-count="1">
<classes>
<class name="com.x.automation.y.tests.smoke.BootCheck" />
</classes>
</test>
</suite>
And from Jenkins, as an 'execute shell' build step run the gradle task, I use gradle wrapper for convenience.
./gradlew clean Smoke_Test
Ensure you're in the correct directory, 'Smoke_Test' is the name specified in the build.gradle.
You can use the testNG Jenkins plugin for saving your results.
I also recommend using reportng for nice formatting of your test reports which can also be shown and saved in Jenkins using the HTML Publisher plugin.
Try getting this to run from a CLI on your local machine first, trying to debug when running from Jenkins will drive you crazy.
As commented above, please use the following ant script to run TestNG unit tests. Please tweak the below code to meet your requirements.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="Ant Play">
<property name="classes.dir" value="bin" />
<property name="report.dir" value="test-output" />
<path id="classpath">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${basedir}\${classes.dir}"/>
</path>
<target name="init">
<mkdir dir="${classes.dir}"/>
<copy includeemptydirs="false" todir="${classes.dir}">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${classes.dir}"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-project" name="build"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" includeantruntime="false" destdir="${classes.dir}">
<src path="src"/>
<classpath refid="classpath"/>
</javac>
</target>
<target depends="build" name="runTests" description="Running tests" >
<echo>Running Tests...</echo>
<taskdef resource="testngtasks" classpathref="classpath"/>
<testng outputDir="${report.dir}"
haltonfailure="true"
useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter"
classpathref="classpath">
<xmlfileset dir="${basedir}" includes="testng.xml"/>
<!--<classfileset dir="${classes.dir}" includes="**/*.class" />-->
</testng>
</target>
</project>
Let me know if you encounter any issues. BTW, please use the Jenkins ant plugin/task to run this script

is there a way to set ant -verbose inside build.xml?

I would like to get verbose console output while building from eclipse and hudson.
There seems to be no verbose property for <target> and <project> and it seems very wrong to call <exec> on ant from inside the script just to pass the verbose prop.
Is there a better way?
You could use Ant's <record> task (http://ant.apache.org/manual/Tasks/recorder.html) to get verbose logging to a file. If this task is defined early in the build file, you should get logging for all build tasks. You could also start and stop the recorder anywhere in your build file. This could, for example, allow you to not log the output of some task that you do not want to see in the log file.
Here's an example of a simple build file that uses the <record> task:
<?xml version="1.0" encoding="UTF-8"?>
<project default="all" basedir=".">
<record name="build.log" loglevel="verbose" action="start" />
<target name="all">
<path id="all.files">
<fileset dir="." includes="**/*" />
</path>
<property name="files" refid="all.files" />
<echo level="verbose">files=${files}</echo>
</target>
</project>
It will be an eclipse External Tools Configuration parameter (under Run -> External Tools). Please see the screenshot below:

Categories

Resources