Could not initialize class net.sourceforge.pmd.lang.xpath.Initializer - ant

Here is my pmd script for ant build.xml
<property name="pmd.dir" value="${basedir}/pmd" /><!-- directory that contains pmd.jar -->
<property name="pmd.test.results" location="${build.dir}/pmd"/>
<path id="pmd.lib" >
<fileset dir="${pmd.dir}">
<include name="*.jar"/>
<exclude name="/rulesets" />
</fileset>
</path>
<target name="pmd" depends="compile" >
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.lib"/>
<pmd shortFilenames="true">
<ruleset>b</ruleset>
<formatter type="text" toFile="pmd-ant-results.txt"/>
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
Here is a screen shot of my projects explorer, here is a pastebin link to ruleset.xml
(source: iforce.co.nz)
And here is the error
pmd:
BUILD FAILED
C:\Users\Michael\Desktop\log4jassignment.s06005586\build.xml:112: Can't find resource ruleset/java/basic.xml. Make sure the resource is a valid file or URL or is on the CLASSPATH. Here's the current classpath: C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-antlr.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-bcel.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-bsf.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-log4j.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-oro.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-regexp.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-resolver.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-apache-xalan2.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-commons-logging.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-commons-net.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-jai.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-javamail.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-jdepend.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-jmf.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-jsch.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-junit.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-junit4.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-launcher.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-netrexx.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-swing.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant-testutil.jar;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.3.v20120321-1730\lib\ant.jar;C:\Program Files\eclipse\configuration\org.eclipse.osgi\bundles\57\2.cp\lib\antdebug.jar;C:\Program Files\eclipse\configuration\org.eclipse.osgi\bundles\57\2.cp\lib\remote.jar;C:\Program Files\eclipse\configuration\org.eclipse.osgi\bundles\58\2.cp\lib\remoteAnt.jar;C:\Program Files\Java\jdk1.6.0_25\lib\tools.jar;C:\Program Files\eclipse\plugins\org.eclipse.swt.win32.win32.x86_64_3.100.0.v4233d.jar;C:\Users\Michael\Desktop\log4jassignment.s06005586\tools\jdepend-2.9.1.jar
The main problem I'm having is lack of information regarding pmd and ant (its terrible there is nothing out there) so I'm pretty much lost at this point at what I'm doing wrong... I'm pretty sure I havent set up my rulesets correctly (but there isn't a way to tell because of this lack of information for ant/pmd implementations)....

The error clearly says...
BUILD FAILED C:\Users\Michael\Desktop\log4jassignment.s06005586\build.xml:112: Can't find resource ruleset/java/basic.xml
and I don't see java folder inside ruleset. So get the folder and files in right place and then see whether it works or not

Related

how to check if class file or jar file is instrumented?

First Question -
Is there any way i can verify that the specific jar files or class files has been instrumented using cobertura?
Second Question - Can you please let me know if following ant scipt is fine. I dont get any output from this. nor instrumented file or cobertura.ser and build says ok.
<project>
<property name="cobertura.dir" value="../cobertura-2.0.3" />
<property name="instrumented.dir" value="../destination" />
<property name="jars.dir" value="../basedir" />
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura-2.0.3.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>
<target name="instrument-classes">
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<delete file="cobertura.ser" />
<cobertura-instrument todir="${instrumented.dir}">
<fileset dir="${jars.dir}">
<include name="XXX.jar" />
</fileset>
<fileset dir="${jars.dir}">
<include name="YYYY.jar" />
</fileset>
</cobertura-instrument>
</target>
</project>
You can use a java decompiler to see references to net.sf.cobertura.xxx classes in the instrumented classes. You can also use a simple text editor that accept to visualize binary files (e.g. TextPad) and you will see net.sf.cobertura references as well, if you look carefully (using a text compare tool to compare the original class and the instrumented one makes it more obvious).
Your ant snippet looks all right except possibly for the suspicious:
<property name="jars.dir" value="../basedir" />
Perhaps you meant something like:
<property name="jars.dir" value="${basedir}/.." />
The point is: you should validate that the nested filesets actually include files, otherwise there won't be anything accomplished by the cobertura-instrument task.

ant javac can't find other project compiled classes

The following is a simplified version of my ant script (it's got the project element etc).
I'm new to ant and unable to figure out why 'compileTests' doesn't compile, whereas 'compileFoo' does.
The error I get is 'package does not exist' as the class in the compileTests project can't find the compiled classes in the compileFoo project, even though they've compiled fine, i can see them on the file system and the path to them is listed in the classpath (i assume this is necessary?)
Clearly there is something basic I don't understand. Can someone please help by explaining?
<path id="build_classpath">
<fileset dir="${other_required_jars}" includes="**/*.jar" />
<fileset dir="${foo_build_location}" includes="**/*.class" />
</path>
<target name="compileFoo" description="compile">
<javac srcdir="${foo_source_directory}\test-src" includeantruntime="false" destdir="${foo_build_location}" includes="**/*.java" excludes="" debug="on" optimize="off" deprecation="on" verbose="on">
<classpath refid="build_classpath" />
</javac>
</target>
<target name="compileTests" description="compile">
<javac srcdir="${test_source_directory}\test-src" includeantruntime="false" destdir="${test_build_location}" includes="**/*.java" excludes="" debug="on" optimize="off" deprecation="on" verbose="on">
<classpath refid="build_classpath" />
</javac>
</target>
Your classpath is wrong. A classpath doesn't contain a set of .class files. It cntains a set of jar or directories, each containing the root of a package tree. So the classpath should simply contain one element : ${foo_build_location}:
<path id="build_classpath">
<fileset dir="${other_required_jars}" includes="**/*.jar" />
<pathelement location="${foo_build_location}"/>
</path>

Ant: Source and target files are the same. How to detect a change?

We are using JiBX. The important thing to know is that JiBX modifies the already compiled class files.
We do our compile:
<javac destdir="${main.destdir}">
<src path="${main.srcdir}"/>
<classpath refid="main.classpath"/>
</javac>
Then, we call JiBX:
<jibx load="true"
binding="{$binding.file}">
<classpath refid="main.classpath"/>
<classpath refid="main.destdir.classpath"/>
</jibx>
This uses an XML file that updates the classfiles compiled by <javac> above. The problem is how do I know that the files have been compiled, but not processed by JiBX? I'd like to put some logic in my program, so that files are not updated twice by JiBX. Besides, it's bad form to duplicate work that already been done.
After the jibx build step, generate a marker file, e.g.
<touch file="${target.dir}/jibx.marker" />
Only perform the jibx build step if that marker file is older than the .class files (indicating that the javac ran more recently than the last jibx).
For that bit of logic, you can use the traditional ant way:
<uptodate property="jibx.uptodate" targetfile="${target.dir}/jibx.marker">
<srcfiles dir="${main.destdir}" includes="...../*.class" />
</uptodate>
And then use the property with an unless clause when invoking the jixb target.
Or, you can use Antcontrib's outofdate alternative:
<outofdate>
<sourcefiles>
<fileset dir="${main.destdir}" includes="...../*.class" />
</sourcefiles>
<targetfiles>
<fileset dir="${target.dir}" includes="jibx.marker"/>
</targetfiles>
<sequential>
<jibx load="true"
binding="{$binding.file}">
<classpath refid="main.classpath"/>
<classpath refid="main.destdir.classpath"/>
</jibx>
</sequential>
</outofdate>
I'm giving this to Patrice M. because his suggestion put me on the right track. However, it didn't quite work out as he stated. (Sorry, if I got he pronoun wrong, but Patrice can be both a male or female name.)
What I had to do was create two watch files: One for the Java compile, and one for the JiBX changes.
<!-- Check if Javac is out of date. If so, create javac watcher -->
<outofdate verbose="true">
<sourcefiles>
<fileset dir="${main.srcdir}">
<include name="*.java"/>
</fileset>
</sourcefiles>
<mapper type="regexp"
from="${main.srcdir}/(.*)\.java"
to="${main.destdir}/(\1).class"/>
<sequential>
<echo message="Java compiled"/>
<echo message="Java compiled"
file="${target.dir}/${javac.monitor.file}"/>
</sequential>
</outofdate>
<javac destdir="${main.destdir}"
debug="${javac.debug}">
<src path="${main.srcdir}"/>
<classpath refid="main.classpath"/>
</javac>
<!-- Compare javac and jibx monitoring file -->
<!-- If out of date, rerun jibx -->
<outofdate>
<sourcefiles>
<fileset dir="${target.dir}">
<include name="${javac.monitor.file}"/>
</fileset>
</sourcefiles>
<targetfiles>
<fileset dir="${target.dir}">
<include name="${jibx.monitor.file}"/>
</fileset>
</targetfiles>
<sequential>
<jibx load="true"
binding="${target.dir}/binding-gg.xml">
<classpath refid="main.classpath"/>
<classpath refid="main.destdir.classpath"/>
</jibx>
<!-- Create JiBX monitoring file -->
<echo message="Compiled and JiBX"
file="${target.dir}/${jibx.monitor.file}"/>
</sequential>
</outofdate>
I create the javac monitoring file if the source is out of date with the classes because that's when I compile. I have to create the JiBX outofdate monitoring file only when I run JiBX and that's inside the <outofdate> for JiBX.
I guess I could also put a source on the XML JiBX files too just to be sure.

CreateProcess error=206, The filename or extension is too long

I'm trying to call Findbugs via Ant, but receiving this error:
Cannot run program "C:\Program Files (x86)\Java\jre6\bin\javaw.exe" (in
directory "H:\Users\MyName\workspace\MyProject"):
CreateProcess error=206, The filename or extension is too long
How can I fix this? o.O
I had the same problem.
I used
<fileset dir="${basedir}/build">
<include name="**/*.class"/>
</fileset>
inside findbugs target and it seems that there is too much .class files to be passed to findbug (?via command line?) because when I used
<fileset dir="${basedir}/build/com/domain/package">
<include name="**/*.class"/>
</fileset>
that had low number of classes, the error was gone.
So, I solved the problem by making one jar file and feeding it to findbugs target with
<findbugs home="${findbugs.home}">
...
<class location="${basedir}/targets/classes-to-analyze.jar"/>
</findbugs>
I think one of the effective file paths are really long when java tries to compile clases.
One worth try is to put codebase in a directory such as C:\MyProject instead of something like C:\Users\MyName\workspace\MyProject
To solve this issue you need to generate a manifestclasspath and a pathing jar.
First Generate your classpath.
<path id="javac.path">
<fileset dir="lib/" includes="**/*.jar"/>
</path>
Next Generate your manifestclasspath
<target name="generate-manifest-classpath">
<manifestclasspath property="manifest.classpath" jarfile="pathing.jar">
<classpath refid="javac.path"/>
</manifestclasspath>
<jar destfile="pathing.jar" basedir="${the location of your build classes}">
<manifest>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
<path id="javac.classpath">
<pathelement path="pathing.jar"/>
</path>
</target>
Next Implement your Manifestclasspath
<javac srcdir="${foo.dir}" destdir="${bar.dir}"
<classpath refid="javac.classpath"/>
</javac>
This will solve the 206 error message if implemented correctly.
I had the same error on IntelliJ while starting debug mode only. To fix is I've changed:
Run > Edit Configurations > "Configuration" tab > Shorten command line
to "JAR-manifest"

What does the Ant warning "Reference * has not been set at runtime..." mean?

Since the upgrade from Ant 1.6.5 to 1.7.1, my build output starts off with:
Warning: Reference project.classpath has not been set at runtime, but was found during
build file parsing, attempting to resolve. Future versions of Ant may support
referencing ids defined in non-executed targets.
Warning: Reference project.test.classpath has not been set at runtime, but was found during
build file parsing, attempting to resolve. Future versions of Ant may support
referencing ids defined in non-executed targets.
I have problems understanding this and why it is outputted, let alone trying to get rid of it. Any help would be appreciated!
EDIT:
Classpath is defined:
<path id="project.classpath">
<pathelement path="./${build.dir}" />
<path refid="libs.ant" />
<fileset dir="${lib.dir}/dependencies/bar" includes="compile/*.jar" />
<fileset dir="${lib.dir}/dependencies/foo" includes="provided/*.jar" />
</path>
<!-- The classpath for compiling this projects' tests -->
<path id="project.test.classpath">
<path refid="project.classpath" />
<fileset dir="${lib.dir}/dependencies/bar" includes="test/*.jar" />
<fileset dir="${lib.dir}/dependencies/foo" includes="test/*.jar" />
</path>
<property name="project.classpath" refid="project.classpath" />
It is referenced (e.g. in ) in this way:
<classpath refid="project.classpath"/>
I had the same issue before. Just make sure the "project.classpath" is defined in the beginning of the build file before other targets reference it. Also your "libs.ant" path should be defined before "project.classpath".
In Ant 1.8 this will actually be error instead of warning.
You can set classpath in the build file as following, to get rid of this warning. See reference. http://ant.apache.org/manual/using.html
<project ... >
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
<target ... >
<javac ...>
<classpath refid="project.class.path"/>
</javac>
</target>
</project>
Unless this is a typo, it looks like trouble. You really should rename one for clarity even if it isn't the cause of the warning. It still sounds like project.classpath is defined in different targets and they are called in the wrong order. You may need to show more code for more help.
<property name="project.classpath" refid="project.classpath" />

Resources