Creating JUnit report programmatically - java

My current approach right now is from my application, just call the ant build and it will create the junit reports needed, but I want to have the flexibility to code junit reports programmatically.
I tried this approach
Ant:create JUnit report task programmatically
FileSet fs = new FileSet();
fs.setDir(new File(".."));
fs.createInclude().setName("TEST-*.xml");
XMLResultAggregator aggregator = new XMLResultAggregator();
aggregator.addFileSet(fs);
AggregateTransformer transformer = aggregator.createReport();
transformer.setFormat(Format.FRAMES);
transformer.setTodir(new File("..");
but I can't even get it running. Any more ideas?

The code you've pasted is the equivalent of calling the junitreport task:
<junitreport todir="..">
<fileset dir="..">
<include name="TEST-*.xml" />
</Fileset>
<report format="frames" todir=".." />
</Junitreport>
You need to put this into a method and run it yourself. This code will take all files called TEST-*.xml and create a report with them. It will not create those files. Those files are created by the junit task in Ant. So you need to:
Run the junit task programmatically (see JUnitTask (Apache Ant API)), ensuring that the TEST*.xml files are created in a temp directory somewhere.
Run the above code to produce the report, using those temp files.
The easiest way to do this is probably what you've done, to have a build.xml somewhere, and invoke ant directly. If the fileset you're using is stable, then this is probably the easiest way to go. Use the ProcessBuilder java class for this.

Related

custom ant task with additional libraries

i was writing a custom task for ant in java and my idea was that i can give someone the .jar which contains the java files like the classes and the libraries and the build.xml for ant and he can use it.
If i export my java project the .jar (antTask.jar) contains :
a folder for the compiled classes, one for the libraries, meta-inf folder and .classpath .project files
The ant build.xml looks like this:
<?xml version="1.0" ?>
<project name="repair" basedir="." default="repairTask">
<taskdef name="antTask" classpath="antTask.jar" classname="def.RepairTask"/>
<target....
i don't really understand all this classpath stuff, so can someone tell me what i have to add in my build file so it will work only with this .jar file without the java code sources?
right now i am getting an error that ant can't find one of the libraries i use in the java code with this error (but the antTask.jar contains this lib as another .jar):
taskdef A class needed by class def.RepairTask cannot be found: org/apache/commons/...
using the classloader AntClassLoader[C:...\AntTask\antTask.jar]
i am trying for hours but i just can't figure out how i have to edit my build.xml so i just have to point to this single .jar file and it works..
Thank you guys
All a taskdef does is associate a task name to a classfile that contains the code to execute that task. However, in order to find that classfile, you need to tell <taskdef/> where to find the jar that contains it. That's all classpath does is.
You don't have to define a classpath with the <taskdef/> task. Ant by default looks for all jars that contain code for the <taskdef/> tasks in $ANT_HOME/lib. If you copy your jar to that folder, you could simply define that task this way:
<taskdef name="antTask" classname="def.RepairTask"/>
No need for the classpath. However, I actually don't recommend doing that. Instead, I recommend putting that jar file into your project, so other developers can use your project without having to install that task jar into their $ANT_HOME/lib folder:
<taskdef name='antTask' classname="def.RepairTask">
<classpath>
<fileset dir="${basedir}/antlib/antjar"/>
</classpath>
</taskdef>
Now, when a developer checks out the project that requires the optional task jar, that task jar comes with the project, so they can simply do their build.
There are two ways to define tasks. One is to give a task a name, and then tell <taskdef/> what classfile is associated with that jar as you did above. However, you can also define a resource that also will associate task names with their classes. Here's a common way to include the Ant-Contrib ant tasks:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<fileset dir="${basedir}/antlib/antcontrib"/>
</classpath>
</taskdef>
If I expand the antcontrib jar, I'll see it contains a net/sf/antcontrib/antcontrib.properties1 file inside the jar. That file looks something like this:
...
# Logic tasks
if=net.sf.antcontrib.logic.IfTask
foreach=net.sf.antcontrib.logic.ForEach
throw=net.sf.antcontrib.logic.Throw
trycatch=net.sf.antcontrib.logic.TryCatchTask
switch=net.sf.antcontrib.logic.Switch
outofdate=net.sf.antcontrib.logic.OutOfDate
runtarget=net.sf.antcontrib.logic.RunTargetTask
timestampselector=net.sf.antcontrib.logic.TimestampSelector
antcallback=net.sf.antcontrib.logic.AntCallBack
antfetch=net.sf.antcontrib.logic.AntFetch
assert=net.sf.antcontrib.logic.Assert
relentless=net.sf.antcontrib.logic.Relentless
# Math Tasks
math=net.sf.antcontrib.math.MathTask
...
All it does is define each task with a classfile for that task. I would recommend you do something similar with your custom ant task. This way, if you decide to include other tasks, you can simply modify this one file, and developers won't have to change their <taskdef/> definition in their jars, or add in multiple ones.
By the way, you should make good and sure that your class doesn't clash with another class that someone else may use. You might want to give your classname a full path that includes a unique prefix:
<taskdef name='antTask' classname="com.vegicorp.anttasks.RepairTask">
Assuming you work for VegiCorp...
1 Ant contrib tasks contain two such files. One is XML format and the other is in properties format. I always use the XML format, and that's what your suppose to use when you define Ant Task resources. I used the properties file because it's a simpler format and easier to see what's going on.

How to run all rulesets from a folder using PMD Ant in Eclipse?

I am trying to run PMD from Ant in Eclipse when I build the project.
This is my build.xml file:
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<target name="check_pmd">
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\basic.xml">
<formatter type="html" toFile="pmd_report.html" toConsole="true"/>
<fileset dir="C:\Users\Nikolay\ProjectName\src">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
It works well for basic.xml, but I want to run for all rulesets in java folder (It has around 20 rulesets) So I have tried:
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\*.xml">
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\*">
But both of them fail when I try to run. Is there a way to specify folder, not a single file without specifying list of files manually?
For future readers to configure Ant PMD under Eclipse:
Download pmd-bin.zip from official website
Unpack pmd.jar, jaxen.jar and asm.jar
Add jars above to Window - Preferences - Ant - Runtime - Ant Home Entries - Add External JARs
Unpack rulesets folder
Reference location of ruleset from <pmd rulesetfiles=...>
(expanding answer from coolfan for ant task)
The documentation of PMD rulesetfiles says it is comma separated list of files.
rulesetfiles A comma delimited list of ruleset files
('rulesets/basic.xml,rulesets/design.xml'). If you write your own
ruleset files, you can put them on the classpath and plug them in
here. Yes, unless the ruleset nested element is used
Ant provides a way to convert fileset into such a format. The task is pathconvert
here is an example from website
<fileset dir="${src.dir}" id="src.files">
<include name="**/*.java"/>
</fileset>
<pathconvert pathsep="," property="javafiles" refid="src.files"/>
Maybe the param doesn't support wildcard, as the document suggests.
A quick look over its source code also supports my guess, see RuleSetReferenceId.java, line 194.
So, it takes a property which contains a "list" using , as delimiter, like:
"rule1,rule2,rule3,path-to-rule-file4"
The workaround could be scanning the directory, list all the rule-xml files, and build a property in the comma-delimited format and then pass it to <pmd> task.
Unfortunately, I don't know any ant task which can do this. So you may have to write some code.
I can come up with two ways:
write a ant task; there are many Q&As about this for Java, like this.
write groovy inside a <groovy> task; also many Q&As.
EDIT:
Jayan suggests <pathconvert> task, which should be the right answer.
In the pmd library jar there is an all-java.xml where all the rule sets have been included.
Try to use the following:
<pmd rulesetfiles="rulesets/internal/all-java.xml">

creating javadoc with ant for only the dependent classes of a given class (java file)

I want to create javadoc with ant from a java source file and just from the dependent classes of this file. These are in a java project, but i don't need javadoc from all the java files.
Is there a way to create javadoc like javac
javac includes="package/Java_source.java" destdir="dir/classes"
that compiles the source files and just the dependent classes? If there isn't, then is there another way?
Thanks, Tamas
Well you can add a fileset and / or a packageset parameter:
Example with a fileset:
<javadoc
destdir="docs/api"
author="true"
version="true"
use="true"
windowtitle="Test API">
<fileset dir="src">
<include name="foo/bar/BaseClass.java"/>
<include name="foo/bar/baz/DependentClassA.java"/>
<include name="foo/bar/phleem/DependentClassB.java"/>
</filset>
</javadoc>
But you will have to figure out the dependent classes yourself, I'm afraid.
Reference:
Ant javadoc Task
There is no way to find out what the "dependent classes" are without starting the job. The only thing I can think of is a brutal hack:
On the compiled classes, use a byte code analysis tool like ASM and check all classes in the code base for their usage of your given class (source code analyis is not enough because of possible wildcard imports and same-package usage). From the List of used classes, build a list of source files, and pass that to the Javadoc task (probably best to create an Ant task that does all this). But this is heavy stuff.

Does Ant Support File-based Dependencies?

I would like to make an ant dependency where the target file depends on a source file. How do you describe this in ant?
For example, convert this Make target to ant
data.txt: header1.txt body.txt footer.txt
cat header1.txt body.txt footer.txt > data.txt
You might be able to do something like this, but it's starting to sound like scripting. Ant isn't a scripting language. If you have a lot of "if/then/else" logic in mind you're probably doing it wrong.
Please describe "other data". Are we talking about copying files? Is this a devl/test/prod environment issue? In that case, you can certainly pass in a parameter specifying environment name and using conditional tests to decide which set to copy. Read this to see how.
If you're just wanting to bring files in one directory up-to-date with respect to your source tree,
you might use the sync task. Here's a basic example from the docs:
<sync todir="site">
<fileset dir="generated-site"/>
</sync>
overwrites all files in site with
newer files from generated-site,
deletes files from site that are not
present in generated-site.
If you need to determine which resources need update,
in order to carry out a more complex operation than a sync,
you might use the ant-contrib outofdate task. For example
<outofdate property="compile.needed" outputsourcespath="sources.for.recompile">
<sourcefiles>
<fileset dir="${src}" includes="*.c"/>
</sourcefiles>
<mapper type="glob" dir="${src}" from="*.c" to="${obj}/*.o"/>
</outofdate>
will set compile.needed to true if any object files are out-of-date compared to source,
and also set the path sources.for.recompile with a list of just the sources that need recompile -
you can then compile for just those sources.
(The assumption here is that a single file in the build output area is directly related to one source.)
My simple solution right now is to manually add a test for the source/dest file age in my shell script called from exec task in ant.

Compile multiple flavors of the same Java software from Ant

I'm looking for a way to compile a few flavors of my Java software using Ant. By flavors I mean, for instance, trial and full version.
In the code I have a
public static final boolean TRIAL
variable which specifies whether it is trial or full version. It would be very nice to be able to set it from Ant and to compile both versions automatically.
I could not find a good way to do this, while I do believe that I'm not the first one to face this problem.
In C I would simple use "ifdef" and set the define from Makefile....
Look at this: http://code.google.com/p/preprocessor/
Allows you to do stuff like this:
//#ifdef CUSTOMER1
private String abc = "abc";
//#else
private String abc = "cba";
//#endif
and set it from ant
I'd create a template file and use ANT's copy filters to change the value before compiling. I've never done it with source code, but I've used it quite a bit for configuration files.
In a source template file, you could do:
public static final boolean TRIAL = #TRIAL_VALUE#
Then in the ant build.xml, you could do this:
<filter token="TRIAL_VALUE" value="true" />
<copy tofile="${your.target.file.name.here}" filtering="true">
<fileset dir="${location.of.your.template}">
<include name="${template.file}" />
</fileset>
</copy>
I'm not sure I like the idea of doing this with a real source file (I think there's a good chance it would make the IDE angry). You might want to consider using a configuration file embedded in the jar (and use the copy filter technique on that instead)
If you have several versions of your software to avoid a easy hacking (java decompiler) I strong suggest you the use of parallel set of sources where the trial are just mocks, and so on.
With ANT you could choose the source of exactly what version will use.
Of course that this duplicate the sources, but is one possible solution without use of external software (like previous preprocessor)

Categories

Resources