Ant javadoc generation of tests code cannot find symbol from src code - ant

In my project, I have a src folder with code source of the application and test folder with code source of the application tests.
In my Ant build, I would like to separate javadoc generation of these source codes. For the src code javadoc generation, there is no problem but for the tests code javadoc generation, I've got a problem because test code uses src code.
My Ant task to generate javadoc is like that :
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${testclasses.home}" />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project in ${classes.home}
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="generate-javadoc-tests" depends="compile-tests" >
<javadoc sourcepath="${test.home}" packagenames="*"
destdir="${test-javadoc.home}" verbose="false"
linksource="true" encoding="${encoding}">
<classpath refid="classpath-test" />
</javadoc>
</target>
The ${test.home} variable is test folder. In the classpath-test, I put jar from junit to avoid error about annotation specifics to junit during javadoc generation. This jar is contained in ${libtest.home}.
When I generate javadoc, I have several warnings about code from test folder that using code from src folder which is normal. The errors are like that :
[javadoc] E:\workspace\app\test\com\app\MyClass.java:9: package com.app.SrcClass does not exist
[javadoc] symbol : class MyClass
[javadoc] location: class com.app.MyClass
So, someone knows a way to includes src classes in classpath to avoid these warnings but without having source code javadoc included in test code javadoc.
Or may be a way to disable these warnings because the verbose option of javadoc task to false doesn't disable these warnings.

So, someone knows a way to includes src classes in classpath to avoid
these warnings but without having source code javadoc included in test
code javadoc.
Make sure the classes on which your tests depend are on the classpath. You might want to make your javadoc generation target dependent on the target which compiles code from src and builds a jar file. Then make sure that the classpath, referenced by refid classpath-test includes that jar.

Related

is there a way to make a jar with the Eclipse generated ant script?

To my surprise the build.xml file generated by Eclipse (Neon) for Java has no element containing an invocation of a jar task. As often is the case with code generation I think you have to use it and make no edits so that you can regenerate - or - avoid code generation completely. A comment in the generated file suggests it might be possible to avoid edits by extending the capabilities by importing.
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
I thought I would be able to use the <?eclipse.ant.import?> element in an second file called export.xml. In ant scripting there is supposed to be one project per buildfile so now there is a second project with a dependency on a target in the first project.
Regenerating build.xml reveals that it contains an "import" as expected.
<import file="export.xml"/>
Unfortunately this does not work. Running ant, which I do from the command line, just seems to result in the export/jar project being ignored.
The generated script with the import element (nested on the 7th line)...
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="ohana1">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<import file="export.xml"/>
<path id="ohana1.classpath">
<pathelement location="bin"/>
<pathelement location="../export/ohana1/commons-collections-3.2.1.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="ohana1.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
</project>
The export.xml file meant to make a jar...
<?eclipse.ant.import?>
<project basedir="." default="export" name="ohana1Export">
<target depends="build,make-jar" name="export"/>
<target name="make-jar">
<jar destfile="../export/ohana1/${ant.project.name}.jar" basedir="bin"/>
</target>
</project>
Note that the Eclipse Ant editor complains about this export.xml file because the target named build, which is a dependency, does not exist in this project/buildfile. The build target is in the generated build.xml. That error might be coming from a "dumb" editor so I went ahead to do a run of ant. Invoking ant from the command line I find that there is no jar file made.
Should I conclude that Eclipse's ant script generator is useless if you need to export a .jar file and that a human should maintain the ant script that meets all the requirements?
Yes, in my opinion the exported build.xml is useless, as of Eclipse Neon, if the intention is to make a .jar.
Specifically do the following.
Manually write the trivial ant script that exports a .jar. The link at the bottom of this post has verbatim text on what the script might look like. You can use the built-in Xml Editor via New > Other > XML > XML File to create this new file which might be called makeJar.xml and save it. If the icon shown in the Package Explorer is still a plain XML file icon refreshing the project may change the icon to an Ant file icon. In the future, you can use Open With to get the Ant Editor instead of the XML Editor. This script will replace the manual exporting of a .jar that the user would otherwise perform via Eclipse.
This script can be added to Project > Properties > Builders. It would be placed second in the list of Builders. First in the list of Builders is the Java Builder which should already exist. When an Eclipse build is invoked the entire list of Builders will be processed in the order shown in the list of Builders. Thus not only will .class files be generated but also the .jar.
What is achieved is greater automation since the .class generation and .jar generation are now integrated, which arguably was the point of using the exported build.xml in a failed attempt to generate the .jar.
Here is the dialog at Project > Properties > Builders that you can use to create a new Builder. Select New then select Ant Builder. I gave the name makeJar to the new Builder.
Here is the dialog for the new Ant Builder that will allow you to browse to your buildfile which is your manually written Ant script that creates a .jar file. In this example the script is makeJar.xml. It also allows you to browse to the base directory to be used when the script is run.
After setting up the new Builder, a project "clean" or project "build" will create .class files and also the .jar.
Eclipse's documentation on this subject is at the link. Note that it seems impossible to link the exact page that contains the instructions so you have to browse down the documentation tree to the section about "Ant buildfiles as project builders".
Link to Eclipse and Ant

failed to create task or type cobertura-instrument

Hi I am getting the Below Error:
build.xml:61: Problem: failed to create task or type cobertura-instrument
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Build .xml contains the below target :
<target name="instrument" depends="init,compile">
<!--
Remove the coverage data file and any old instrumentation.
-->
<delete file="cobertura.ser"/>
<delete dir="${instrumented.dir}" />
<!--
Instrument the application classes, writing the
instrumented classes into ${build.instrumented.dir}.
-->
<cobertura-instrument todir="${instrumented.dir}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
Build.properties
# The source code for the examples can be found in this directory
src.dir=C:/Rahul/SVN_CodeBase/services/src
# The path to cobertura.jar
cobertura.dir=C:/Rahul/SVN_CodeBase/cobertura-2.0.3
# Classes generated by the javac compiler are deposited in this directory
classes.dir=C:/Rahul/SVN_CodeBase/services/build/classes
# Instrumented classes are deposited into this directory
instrumented.dir=services/build/classesinstrumented
# All reports go into this directory
reports.dir=services/build/reports
# Unit test reports from JUnit are deposited into this directory
reports.xml.dir=${reports.dir}/junit-xml
reports.html.dir=${reports.dir}/junit-html
# Coverage reports are deposited into these directories
coverage.xml.dir=${reports.dir}/cobertura-xml
coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
coverage.html.dir=${reports.dir}/cobertura-html
please let me know how to resolve the above error tanks in advance.
I found that the guidelines in the official docs are a little bit misleading. The jar file in the cobertura folder contains the version number in the file name, thus you cannot copy&paste the element.
Try this instead:
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura*.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>
Did you use taskdef to define the corbertura-instrument task?
Ant allows you to create new and exciting tasks that can be used in Ant, but you need to tell Ant how these tasks are defined and how to execute them. Fortunately Corbertura tells you exactly how to defined their taskdef.
I recommend putting the required Corbertura jars under ${basedir}/antlib/corbertura in your project. This way, when someone checks out the project, they get the required Corbertura jars automatically, and don't have to install them on their particular system:
<taskdef resource="tasks.properties">
<path>
<fileset dir="${basedir}/antlib/corbertura"/>
<fileset dir="${lib.dir}/>"
</path>
</taskdef>

I wonder if there is something wrong with my ant junit task

I'm writing my build.xml. However, it seems there is something go wrong with my junit task. when I run my junit task. I can build success, but the junit report only show that run 1 test with error. But I have more than 10 tests. So I wonder if there is something run with my junit task. Here is my code.
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="dest.dir" value="dest"/>
<property name="test.dir" value="test/>
<property name="lib.dir" value="lib"/>
<path id="classpath">
<pathelement location="${lib.dir}/junit-4.11.jar"/>
<pathelement location="${lib.dir}/ant-junit4.jar"/>
</path>
<target name="test" depends="compile">
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement location="${bin.dir}"/>
<path refid="classpath"/>
</classpath>
<formatter type="plain" usefile="false"/>
<batchtest fork="yes">
<fileset dir="${test.dir}" includes="*Test*.java"/>
</batchtest>
</junit>
</target>
I cannot figure out what is wrong so could somebody help me out?
And what is happening? Do you get any error messages?
You usually need to do the following:
Compile your normal code. The resulting *.class files should be placed inside a directory such as target/classes or build/classes. Use the destdir parameter of the <javac> task to do this.
Compile your JUnit tests.
In your classpath, you need all of the jars you needed to compile your normal classes
You need a reference to the destdir where your normal classes were compiled to.
You need the JUnit jar.
These should be compiled to a different directory from your normal jars. Normally, this is target/test-classes or build/test-classes.
Once you've compiled the JUnit tests, you may run them. You can use the <junit> task like you did.
You need to make sure that the includeantruntime parameter is set to true
You should also set fork to true.
You need the same classpath (with all three elements you had) when you compiled the test classes. More jars might be needed, but usually not.
You run the tests on the compiled JUnit test classfiles (the ones you saved to target/test-classes or build/test-classes. In your example, you're trying to run them against the source.
I use the Maven standards for my directory layout. That means my Java source is under src/main/java while my JUnit Java files are under src/test/java. Any XML or properties or other none source files needed are stored in src/main/resources. The regular source is compiled to target/classes while the Junit sources are compiled to target/test-classes.
This makes it easy to compile your code and test code separately without worrying about **/test/**, **/Test/**, **/JUnit/** exceptions in directory compiling since everything is separate.
Hope this helps.

Ant build script not seeing refid

I have a build.xml file that includes a common.xml file that defines some refid values. However, my task cannot see the refid value. I have not been able to find a solution on the web and am looking for some help.
I call the genbeans target in the build.xml file. It fails on the xmlbean taskdef with the message Reference my_classpath_jars not found.
build.xml
----------------------------
[includes common.xml]
**my_classpath_jars fails to be seen at this point - defined in common.xml**
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean">
<classpath refid="my_classpath_jars"/>
</taskdef>
<!-- Generate the XMLBeans java code from our source XSD file(s) -->
<target name="genbeans" description="Generate XML Bean files" depends="build_my_jar_cpath">
<mkdir dir="${lib}"/>
<xmlbean destfile="${lib}/${appname}Beans.jar" failonerror="true">
<classpath refid="my_classpath_jars"/>
<fileset dir="src/XSD Files" includes="*.xsd, *.wsdl"/>
</xmlbean>
</target>
common.xml
-----------------------------
<target name="build_my_jar_cpath">
<path id="my_classpath_jars">
<fileset dir="${jardir}" includes="**/*.jar" />
</path>
<pathconvert pathsep="${path.separator}" property="myjar.clpath" refid="my_classpath_jars"/>
</target>
When in doubt, use the ant -d switch when calling your target. You'll see a ton of output. Save it to a file and parse through it.
Do that, and the first thing you'll notice in the output is that it's defining your taskdefbefore you have defined your my_classpath_jars. That my_classpath_jars refid is only set when you call that greenbeans target. Your <taskdef> is executed before any of your targets are called.
Either take the definition of my_classpath_jars out of the target greenbeans, or put your <taskdef> in there.

Making jUnit output info and compile to /build folder

I have the following Ant buildfile:
<?xml version="1.0"?>
<!-- the value of the default attr must be one of the targets. -->
<project name="Money" default="build-source" basedir=".">
<description>The Money project build file.</description>
<property name="src" location="."/>
<property name="build" location="build"/>
<property name="junit" location="lib/junit-4.9b3.jar"/>
<path id="_classpath">
<pathelement path="${junit}"/>
<pathelement path="${build}"/>
</path>
<target name="prepare">
<mkdir dir="${build}"/>
</target>
<target name="build-source" depends="prepare"
description="compile the source ">
<javac srcdir="${src}" destdir="${build}">
<classpath refid="_classpath"/>
</javac>
</target>
<target name="run" depends="build-source">
<junit printsummary="on" showoutput="on">
<test name="money.MoneyTest"/>
<classpath refid="_classpath"/>
</junit>
</target>
</project>
It's pretty basic - I'm just trying to get this thing to run properly. What I don't get is: 1) Why does it output the compiled files to a /build/money directory? I want the output directory to be just /build, given this directory structure for my files:
build/
build.xml
lib/
src/
test/
2) When there are tests that don't pass, it says "Test money.MoneyTest FAILED". I'd like it to output info about the failure, expected / actual values, line number, etc.
I can't figure this out by staring at the buildfile above. Any advice?
It outputs the compiled files under build, creating a directory structure that corresponds to the layout of your packages.
Since you put your classes in the money package, the output will be under build/money. If you put your classes under a org.example.foo package, your output would be in the build/org/example/foo directory.
To have your .class files in build, you would have to use the default package.
Edit
I assume your source files have a package money; declaration, as in:
package money;
public class MoneyTest {
...
}
If you add a <formatter> element, detailed reports about test failures will be written to an output file (by default, named TEST-name). See also the Ant Junit Task Documentation.
<junit printsummary="withOutAndErr" showoutput="on">
<formatter type="plain"/>
<test name="money.MoneyTest"/>
<classpath refid="_classpath"/>
</junit>
I have not found a way to directly print the failed tests reports to standard output.

Resources