Cobertura ant script is missing Log4J classes - java

I tried to get Cobertura running inside my ant script, but I'm stuck right at the beginning. When I try to insert the cobertura taskdef I'm missing the Log4J libraries.
Ant properties & classpath
<property name="cobertura.dir" location="/full/path/to/cobertura-1.9.3" />
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
My ant target
<!-- =================================
target: cobertura
================================= -->
<target name="cobertura" depends="clean, init" description="Generates cobertura coverage reports">
<cobertura-instrument todir="${dir.build.instrumented}">
<fileset dir="${dir.build}">
<include name="**/*.class" />
</fileset>
</cobertura-instrument>
</target>
I think I did everything like it is described in the Cobertura documentation but I get this
Ant build error
BUILD FAILED
build.xml:95: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
Inside the ${cobertura.dir} there is the lib directory with all files. I unzipped it from the cobertura distribution ZIP directly into that directory.
Am I missing a step? Something wrong with my configuration so far?

I also encountered this problem today and solved it by specifying the location of all the required libraries as part of the class path provided to my taskDef task.
<path id="cobertura.class.path">
<pathelement location="${common.dir}/../tools/cobertura/cobertura.jar" />
<pathelement location="${common.dir}/../tools/cobertura/lib/asm-3.0.jar" />
<pathelement location="${common.dir}/../tools/cobertura/lib/asm-tree-3.0.jar" />
<pathelement location="${common.dir}/../tools/cobertura/lib/log4j-1.2.9.jar" />
<pathelement location="${common.dir}/../tools/cobertura/lib/jakarta-oro-2.0.8.jar" />
</path>
<taskdef classpathref="cobertura.class.path" resource="tasks.properties" />

Go to your ant/lib dir and make sure there is NO file cobertura.jar there. If it's there - remove it and try again.

Change this
<include name="lib/**/*.jar" />
to
<include name="*.jar" />
Hope this helps!

I just upgraded to the latest cobertura and mine works fine. Is it possible that something else is on the CLASSPATH with a different version of log4j so it is picking up wrong one?

Make sure that classpath used in taskdef and cobertura-instrument are the same. This helped me with the same issue.

I too faced this problem, I just added all jars given with cobertura in the classpath to resolve this issue

Related

Java Ant XML Build Failed, Cause: The name is undefined

<path id="checkstyle.classpath">
<fileset dir="${basedir}/lib/">
<include name="checkstyle-6.12.1-all.jar" />
</fileset>
</path>
<target name="checkstyle">
<!-- CheckStyle -->
<taskdef resource="checkstyle.properties" classpath="C:\Users\Pil\Desktop\Workspace2\Cs376bDemoApp\lib
\checkstyle-6.12.1-all.jar" />
<checkstyle config="DsrJavaFormattingProfile.xml" classpath="C:\Users\Pil\Desktop\Workspace2\Cs376bDemoApp\lib
\checkstyle-6.12.1-all.jar" />
<fileset dir="${src}" includes="**/*.java" />
</target>
This is my checkstyle target in my build.xml file. I have spent the past 6 hours debugging this before resorting to StackOverFlow but my best efforts proved fruitless. I am consistently met with the error report [taskdef] Could not load definitions from resource checkstyle.properties. It could not be found.
BUILD FAILED
C:\Users\Pil\Desktop\Workspace2\Cs376bDemoApp\build.xml:86:
Problem: failed to create task or type checkstyle
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.
My intention for this code is for it to use my DsrJavaFormattingProfile.xml on the .java files in my src directory. I have tried replacing the class path with the relative path "lib/" and with the checkstyle.classpath path above aswell as every possible other solution I could think of.
<taskdef resource="checkstyle.properties" classpathref="checkstyle.classpath" />
<checkstyle config="DsrJavaFormattingProfile.xml" classpathref="checkstyle.classpath" />
also resulted in the same error. Please save me StackOverFlow, I need a hero.
https://www.youtube.com/watch?v=OBwS66EBUcY
Update: Solved by CAustin
`
<target name="checkinit">
<path id="checkstyle.lib.path">
<fileset dir="lib" includes="checkstyle8.8all.jar" />
</path>
<taskdef resource="com/puppycrawl/
tools/checkstyle/ant/checkstyle-ant-task.properties" classpathref="checkstyle.classpath" />
</target>
<target name="checkstyle" depends="checkinit">
<!-- CheckStyle -->
<checkstyle config="${basedir}\DsrCheckStyleConfig.xml" classpathref="checkstyle.lib.path">
<path refid="project.sourcepath" />
</checkstyle>
</target>`

Ant can't find classpath classes

So I'm extending my company's ant build script to add in a special module we want build in some cases. I've written an ant script that points to where I know the compiled class files for the rest of our codebase are, because they get compiled earlier in the build process. I know with 100% certainty the files are in this location.
However, whenever I try to compile this module, the classpath reference can't see those classes, and I get a bunch of "package does not exist" and "can't find symbol" errors.
I just can't seem to figure out what I'm doing wrong. Hoping for help here.
Here's my build script code:
<property name="classpath" value="${dir.dev}/out/production/Main"
<path id="pfClasspath">
<fileset dir="${classpath}">
<include name="**/*.class"/>
</fileset>
<fileset dir="${dir.dev.lib}">
<include name="**/*.jar" />
</fileset>
<fileset file="${lib.json}" /> <!-- TODO try removing this -->
</path>
<target name="compile" depends="prepare">
<javac source="1.7" classpathref="pfClasspath" srcdir="${dir.project}/src" destdir="${dir.project.build.classes}" />
</target>
The directory the "classpath" property is pointing at 100% contains all of the class files for the rest of the project. That level is the equivalent of the "src" directory on the sources side, immediately within it are the com/companyName/etc... folders.
My code contains references to the classes compiled at this location. Yet ant isn't finding them. Any help?
Try
<path id="pfClasspath">
<pathelement path="${classpath}" />
...
</path>
instead. Specifying the classpath does not mean to specify every single class file that's on the classpath, which is what you do when you define your <path> element using a <fileset>.

Fail build on PMD errors/warnings

I have a JAVA ANT project and I am trying to integrate PMD scripts with it so that I can check for all errors and warnings in my project.
Below is snippet of ANT script which I have added in my build.xml:
<property name="pmd.dir" value="buildconfig/build/pmd/" />
<path id="pmd.lib" >
<fileset dir="${pmd.dir}">
<include name="*.jar"/>
<exclude name="/rulesets" />
</fileset>
</path>
<target name="pmd" depends="init">
<echo message="PMD Starting-----" />
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.lib"/>
<pmd shortFilenames="true">
<ruleset>unusedcode</ruleset>
<formatter type="text" toFile="${pmd.dir}/pmd-ant-results.txt"/>
<fileset dir="modules/app/">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
ANT build is working fine with PMD giving proper error reports but I need to abort the build as failure when PMD encounters any errors in code.
I tried adding failOnRuleViolation="yes" but that did not stop build.
Is there anything else I need to add in script?
Try failOnRuleViolation="true" instead of failOnRuleViolation="yes"...
<pmd shortFilenames="true" failOnRuleViolation="true">
...
Some Ant tasks treat true and yes as equivalent, but many tasks don't know how to handle yes at all. It is possible that <pmd> is one of those tasks that doesn't handle yes.
As a rule of thumb, avoid this gotcha in the future by always using true and false instead of yes and no.

JooQ Ant Codegeneration not working

Following the example given at http://www.jooq.org/doc/2.4/manual/META/Configuration/#N10607 on how to run the jooq code-generation I ran into the problem that the build fails with the message:
codegen.xml:7: taskdef class org.jooq.util.GenerationTask cannot be found
For a reference, here is codegen.xml
<project name="..." default="generate-test-classes"
basedir=".">
<property name="path.to.jooq.distribution" value="${basedir}/libs"/>
<property name="path.to.mysql.driver" value="${basedir}/libs"/>
<property name="mysql.driver" value="mysqlcon"/>
<!-- Task definition -->
<taskdef name="generate-classes" classname="org.jooq.util.GenerationTask">
<classpath>
<fileset dir="${path.to.jooq.distribution}">
<include name="jooq.jar" />
<include name="jooqmeta.jar" />
<include name="jooqcodegen.jar" />
</fileset>
<fileset dir="${path.to.mysql.driver}">
<include name="${mysql.driver}.jar" />
</fileset>
</classpath>
</taskdef>
<!-- Run the code generation task -->
<target name="generate-test-classes">
<generate-classes jdbcurl="jdbc:mysql://localhost:3306/crawler"
jdbcuser="..." jdbcpassword="..." generatordatabaseinputschema="..."
generatortargetpackage="model.persistence.jooq"
generatortargetdirectory="${basedir}/src" />
</target>
</project>
I triple checked the definition of the classpath and every file listed exists under the given location. So what would be the problem? Am I missing something? Do I need to configure ant somewhere to recognize the task?
Since I already checked ant: failed to create task or type, I tried to move the taskdef inside the target, but to no avail.
The ant task was an example implementation in jOOQ 2.x. It has been deprecated a long time ago and removed from jOOQ 3.0:
http://www.jooq.org/notes.php?version=3.0
https://github.com/jOOQ/jOOQ/issues/1118
http://www.jooq.org/doc/3.1/manual/code-generation/codegen-configuration/#N12E23
The version of the manual that you've linked is quite outdated.

Linked folder not resolved by Ant in eclipse

In my build.xml, below works fine :-
<path id="build.classpath">
<fileset dir="lib [myUtils]" includes="*.jar" />
</path>
if lib [myUtils] is of folder type, but don't works, if it's of Linked Folder type.
Also, I found this when googled :-
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
https://bugs.eclipse.org/bugs/show_bug.cgi?id=43081
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
Is there any trick to achieve this, without copying the dependencies in work folder??
Note that ant should work outside of eclipse as well. So you can't reply in IDE abstractions. You can use symbolic links (if your OS supports them).
If not, you can use the FileSync plugin to synchronize eclipse project folders with external folders. Or you can simply use the <copy> ant task.
I resolved this using copy task:
<copy todir="target/web/linked1">
<fileset dir="../linkdProject/source1" />
</copy>
<copy todir="target/web/linked2">
<fileset dir="../linkedProject/source2" />
</copy>
....
<war destfile="target/webApp.war">
<fileset dir="WebContent" />
<fileset dir="target/web" /> <!-- copy linked resources -->
...
</war>
<delete dir="target"/>

Categories

Resources