Ant doesn't find org.junit but it's right here(?) - java

When trying to perform an ant build, I receive a message package org.junit does not exist. However, in build.xml, I have:
<junit>
<classpath>
... some stuff ...
<fileset dir="dependlibs" includes="**/*.jar" />
</classpath>
... other stuff ...
</junit>
Now, in the same directory in which I am trying to run ant (the directory with build.xml), I have an org directory which contains junit.jar. Can Anyone point Me in the direction of information to show Me what I am doing wrong?

The problem is that junit.jar is not in ants classpath. Provided your basedir is correct, you should be able to add it to the classpath by adding:
<junit>
<classpath>
<fileset dir="org">
<include name="**/*.jar" />
</fileset>
....
</classpath>
</junit>

Related

ant build missing jar from lib

I have a build.xml and I have a path for the classpath that I set to classpathref="compile.classpath" during compile:
<path id="compile.classpath">
<fileset dir="./lib">
<include name="*" />
</fileset>
</path>
The lib folder contains weblogic.jar but when i try to compile the project, i got many errors because of missing the weblogic.jar
If I modify my path to this:
<path id="compile.classpath">
<fileset dir="./lib">
<include name="*" />
</fileset>
<fileset dir="${env.WL_HOME}/wlserver/server/lib">
<include name="weblogic.jar" />
</fileset>
</path>
So I add the weblogic.jar from my local installed weblogic directory, there are no errors, and it's compiled.
I copied the weblogic.jar to my project lib folder from the local installed weblogic folder, so it must be the same weblogic.jar
What should I try? Thank you!
I would do something like this in your build.xml (probably just before you do the compilation will work).
<property name="echo.classpath" refid="compile.classpath"/>
<echo message="compileClasspath - ${echo.classpath}"/>
What you probably need to do is to be quite explicit about where your lib directory is, relative paths are tricky if you have multiple build.xml files, and nested directories and stuff.
What I have done before is to make sure that you explicitly define a property in the right place for your lib directory, and just use that rather than ./
<project basedir=".">
<target name="init">
<property name="local.lib.dir" value="${basedir}/lib">
</target>
<target name="compile" depends="init">
<path id="compile.classpath">
<fileset dir="${local.lib.dir}">
<include name="*" />
</fileset>
</path>
....
</target>
</project>

Using dirset with Ant + Jacoco

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>

build.xml javac is complaining about missing classes from javax.servlet.http during compliation

I've written a build.xml (Updated 8:49 PM) for a netbeans project. And I've found that the tomcat libraries such as serlvet-api.jar are in the directory C:\apache-tomcat-7.0.35\lib. But I'm not sure how I'm meant to connect the target="class_compile" using the fieldset dir to the tomcat directory (without changing the build.xml each time I wish to compile from another computer).
I've read the question error while including external JARs in ant script, with the solution being a missing classpathref attribute within the javac element (although my classpathref attribute seems to be correct).
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="class_compile" depends="prepare" description="Compile the whole project">
<javac destdir="${build.classes}"
debug="${debug}"
deprecation="on"
optimize="off"
srcdir="${src.dir}"
classpathref="build.classpath"
includes="*/**"
/>
<copy todir="${build.classes}">
<fileset dir="${src.dir}" includes="**/*.properties"/>
</copy>
</target>
<target name="prepare">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<path id="run.classpath" >
<pathelement location="${build.classes}" />
</path>
<mkdir dir="${build.lib}"/>
<mkdir dir="${qa.dir}"/>
</target>
Currently, once the class_compile target is executed multiple errors regarding missing class files are reported..
emma:
Created dir: C:\capstonegroup3\TTTserver\build\emma-instr
Created dir: C:\capstonegroup3\TTTserver\build\emma-reports
prepare:
Created dir: C:\capstonegroup3\TTTserver\build\classes
Created dir: C:\capstonegroup3\TTTserver\build\lib
Created dir: C:\capstonegroup3\TTTserver\build\qa-reports
class_compile:
C:\capstonegroup3\TTTserver\build.xml:152: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
Compiling 21 source files to C:\capstonegroup3\TTTserver\build\classes
C:\capstonegroup3\TTTserver\src\java\AuthServer\AuthenticationInterface.java:8: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
Is there any way to set a property to the tomcat apache/lib directory, from persay a .property file? I saw in my build_impl.xml (generated by netbeans, that has a property file included during the -init-private target).
<target depends="-pre-init" name="-init-private">
<property file="nbproject/private/private.properties"/>
</target>
But I'm unsure how to gain access to those properties for my build.xml. But basically I'm after a solution that generates a relative path to the apache-tomcat\lib directory, and successfully compiles the class files without missing packages.
You should declare the servlet-api.jar file to build.classpath as below
<path id="build.classpath">
<fileset dir="C:/apache-tomcat-7.0.35/lib">
<include name="servlet-api.jar" />
</fileset>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
What I like to do is keep a local.properties file that describes my local environment in the parent folder of the project folder (e.g. under NetBeansProjects), e.g.:
NetBeansProjects
+- local.properties
+- MyProject
+- build.xml
+- <other stuff>
The build.xml is like:
tomcat.home=/C:/java/tomcat
gwt.dir=/C:/java/google/gwt-2.5.1
This file is not kept under version control and allows each developer to configure his/her own environment. It is read by Ant using something like:
<property file="../local.properties" />
And used e.g. as (this is only an example, adjust for proper usage):
<path id="project.classpath">
<pathelement path="war/WEB-INF/classes" />
<fileset dir="war/WEB-INF/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/lib"><!-- tomcat.home defined in local.properties -->
<include name="**/*.jar" />
</fileset>
</path>

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

Ant build fail - because ant forgets property?

I am getting the following build error-
BUILD FAILED
C:\eclipse\workspace\ContinuousTesting\build.xml:55:
C:\eclipse\workspace\ContinuousTesting\${lib.dir}
Here is the build.properties file:
src.dir=./src
build.dir=./bin
lib.dir=./lib
This is the whole task
<target name="compile" depends="properties, create.build.dir, xmlmapping.jar.import" description="Perfom compilation">
<!-- Compile the java code -->
<echo message="[compile] compiling sources with lib ${lib.dir} to ${build.dir} source dir ${basedir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" listfiles="no" debug="true" classpathref="build.classpath" fork="true" memoryInitialSize="128m" memoryMaximumSize="512m" />
<antcall target="backup" />
</target>
and it generates the following output
compile:
[echo] [compile] compiling sources with lib ./lib to ./bin source dir C:\eclipse\workspace\ContinuousTesting
[javac] Compiling 42 source files to C:\eclipse\workspace\ContinuousTesting\bin
What is my build.classpath I hear you ask....
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<pathelement path="${build.dir}" />
</path>
I am running this through eclipse.
What am I missing?
Thanks!
Azriel
Problem is down to the fact that classpath variable is being evaluated prior to the build.properties file being loaded.
This is resolved by not using configurable lib.dir, as it is quite constant.
thanks for your time and help

Categories