Using dirset with Ant + Jacoco - java

I'm trying to use jacoco to perform unit tests on files but it keeps popping with NoClassDefFoundError and keeps giving new class names every time I sort the previous error out. Till now I've been manually providing the pathelement location to the bin folders which contain the required .class files.
I'd like to fasten the process by using dirset if possible. I've compiled a dirset which contains the bin folders of all the main folders that are required.
Questions:
1. How do I include this dirset in the classpath such that jacoco looks for the .class files in these set of bin directories?
2. Is there any way in which I can echo the dirset to the cmd to check its contents?
3. Why does this not work?
<echo message="Build path is: ${toString:ALL.dirs}"/>
dirset:
<path id="ALL.dirs">
<dirset dir="${classes.dir}">
<include name="projects-framework/**/bin"/>
</dirset>
</path>
jacoco code coverage:
Code I've written till now. The "**" in the code are the full locations to the bin folders which I do not think is relevant here.
<target name="cov-test">
<mkdir dir="${report.dir}"/>
<jacoco:coverage>
<junit fork="true" forkmode="once" showoutput="true" printsummary="on" enabletestlistenerevents="true">
<test name="full.qualified.name.ABC"/>
<classpath>
<path refid="classpath"/>
<path refid="application"/>
<pathelement location="projects-framework/**/bin"/>
<pathelement location="projects-framework/**/bin"/>
<pathelement location="projects-framework/**/bin"/>
<pathelement location="projects-framework/**/bin"/>
</classpath>
<formatter type="plain"/>
<formatter type="plain" usefile="false" />
</junit>
</jacoco:coverage>
</target>

Related

ANT: ignore certain files from javac's sources

Currently, we define a path with
<path id="sources.production">
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
and then compile using
<javac ...>
<src refid="sources.production"/>
<classpath refid="classpath.production">
</javac>
Now we create some modified .java files (original files in src/module2) before this <javac> call and have put them before the other sources:
<path id="sources.production">
<pathelement path="generated-sources"/>
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
Unfortunately, the compile fails now because the original and the modified .java files are both fed to the javac task. How to exclude the original source files easily from the javac's sources without large charges?
you need an intermediate step which you copy all your source files to a new location and override them with generated ones, then use this new location as source folder for javac.
it may look something like this:
<path id="sources.4compile" location="all-sources" />
<target name="prepare-4compile">
<!-- clean -->
<delete dir="all-sources"/>
<mkdir dir="all-sources"/>
<copy todir="all-sources">
<fileset dir="src/module1"/>
<fileset dir="src/module2"/>
<fileset dir="generated-sources"/>
</copy>
</target>
<target name="compile" depends="prepare-4compile">
<javac ...>
<src refid="sources.4compile" />
<classpath refid="classpath.production" />
</javac>
</target>
other way, you may specify a fileset to javac disabling javac's default searching mechanism as suggested here.

Include libs folder containing .jars for compilation

I have the following file structure:
ServerCode <- src , libs, bin
I am trying to compile all the code in src. Src has a couple of .java files at the top level and sub-directories. libs contains all the .jar files which are required to build my project.
I wrote the following build.xml but when I try to compile it, the compiler throws errors cannot find symbol errors for the libraries I am including.
<project default="compile">
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" classpath="libs/*.jar">
</target>
</project>
Define class path to include all jars like this
<target name="compile" depends="" description="compile the java source files">
<javac srcdir="." destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${test_lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
I don't think you can use a pattern in the classpath attribute. I could be wrong about this. You should run ant in verbose mode (the -v option) to see how it's using your classpath attribute. I suspect it's passing it to javac as a literal string.
Here's what I do:
<javac target="${javac.target}" source="${javac.source}" destdir="${validator.output.dir}" debug="on"
nowarn="off"
memorymaximumsize="128m" fork="true">
<classpath refid="validator.module.production.classpath"/>
<src>
<dirset dir="Validator">
<include name="src"/>
</dirset>
</src>
</javac>
...
<path id="validator.module.production.classpath">
<fileset dir="Validator/lib/-validator" includes="**/*.jar"/>
</path>
Some of this code is generated by my IDE so it's a little verbose, but you get the idea.
Try this as mentioned at http://ant.apache.org/manual/using.html instead of giving classPath attribute alongwith javac
<classpath>
<pathelement location="libs/*.jar"/>
</classpath>
There are other ways also which you can glance thru the link mentioned above

Eclipse don't recognize test folder as source folder when importing ant build file

I have an ant build file for a java project the project tree looks like this :
DataBaseFidling.
|-->src (contains production code source)
|-->tests (contains tests code source)
|-->bin (contains .class)
|-->reports(contains junit xml reports)
|-->build.xml
Whenever I import this project with eclipse using "Java Project From Existing Ant Build File", eclipse does not reconize the tests folder as a source folder.
What to do to fix this?
Here is the ant build file :
The DatabaseFidling Project.
<property name="src.dir" location="./src/" />
<property name="tests.dir" location="./tests/" />
<property name="bin.dir" location="./bin/" />
<property name="lib.dir" location="/home/chedy/workspace/lib"/>
<target name="clean">
<delete verbose="true">
<fileset dir="${bin.dir}"/>
</delete>
</target>
<target name="compile">
<javac srcdir="${src.dir}" destdir="${bin.dir}">
</javac>
<javac srcdir="${tests.dir}" destdir="${bin.dir}">
<classpath>
<pathelement location="${lib.dir}/junit4.jar"/>
<pathelement location="${lib.dir}/mockito-all-1.9.5.jar"/>
<pathelement location="${lib.dir}/SQLScriptRunner.jar"/>
</classpath>
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="yes" fork="true" >
<formatter type="xml"/>
<classpath>
<pathelement path="${bin.dir}"/>
<pathelement location="${lib.dir}/junit4.jar"/>
<pathelement location="${lib.dir}/mockito-all-1.9.5.jar"/>
<pathelement location="${lib.dir}/SQLScriptRunner.jar"/>
<pathelement location="${lib.dir}/mysql-connector-java-5.1.23-bin.jar" />
</classpath>
<batchtest todir="./report">
<fileset dir="${bin.dir}">
<include name="**/**Test*.*"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="run" depends="compile">
<java classname="com.esprit.is.Main" fork="true">
<classpath>
<pathelement path="${bin.dir}"/>
<pathelement location="${lib.dir}/mysql-connector-java-5.1.23-bin.jar" />
</classpath>
</java>
</target>
</project>
You can manually add the test folder as a source folder. Right click the project, Build Path -> Configure Build Path -> Java Build Path. In the Source tab, click Link Source then browse to your folder.
The compile target had to contain one javac task which compiles both the src and test folders.

Testing with Ant - Cannot test files in subdirectories

I have an android application in java. I want to test it using a xml script with ant. Now we have several packages and a standard directory structure for our source and test files.
There is a "tests" folder (root of all test code) which has 1 file say A.java and some sub-folders which again in-turn contain some test files or sub-sub-folders. Only file A.java gets tested and rest others are not tested. If i change the directory path in xml script from "tests" to say "tests/ui" then test files in folder "ui" are not able to test and i get some error - Class Not Found Error when i check out the files in "test-result" folder where i store the output of the test.
I have included the parent directory of compiled codes and required external jars in path with id = "test.classpath". I think i should every file of type .class rather then path to parent folder.
The code is :
<target name="test-run" description="Run Test">
<delete dir = "test_result" />
<mkdir dir = "test_result" />
<junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
<classpath refid="test.classpath"/>
<batchtest fork="yes" todir="test_result">
<formatter type="xml"/>
<fileset dir="tests" includes="**/*" />
</batchtest>
</junit>
</target>
Can you please explain me how to achieve my motive assuming a generic file structure.
EDIT :
Following is the path used in above xml where folder out contains .class files of source and test files generated by idea ide :
<path id="test.classpath">
<pathelement path="testlibs/jmockit.jar"/>
<pathelement path="testlibs/jmockit-coverage.jar"/>
<pathelement path="testlibs/jmockit-coverage-htmlfull.jar"/>
<pathelement path="/opt/idea-IU-117.418/lib/junit-4.10.jar"/>
<pathelement path="out"/>
</path>
Try changing the filesetelement to this:
<fileset dir="tests" includes="**/*.java" />

java ant buildfile for project with dependencies

I am new to ant, but am trying to create an ant script that builds my current proeject with another project as a dependency. I have the ant script building my current project, but am unsure how to add the other project to the classpath. Neither project get put into jar files at the moment.
My current portion of the build.xml file is
<target name="run" depends="compile">
<java classname="com.mypackage.Main">
<classpath>
<pathelement location="../project1/out"/>
<pathelement location="${bin}"/>
</classpath>
</java>
</target>
Thanks for your help!
I was able to do it using
<target name="run" depends="compile">
<java classname="com.mypackage.Main" fork="true">
<classpath>
<dirset dir="${other.dir}">
<include name="out/**"/>
</dirset>
<pathelement location="${bin}" />
</classpath>
</java>
</target>
Where ${other.dir} is the relative path to the root of the other project.

Categories