Ant build fail - because ant forgets property? - java

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

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>

Order of compilation in ant target

The ant compile target below compiles all .java files in any of the src folders specified using a <src path="..."/> tag. My question is about the order of compilation. Does ant first compile the files referenced by the first src tag (i.e. does it first compile the Java files in ${xtext.project.path}/src), then the second src tag, etc.? What order are the files compiled in? I'm trying to figure out the dependencies between folders and wonder if the order they are listed tells me anything.
<target name="compile">
<echo message="${ant.project.name}: ${ant.file}"/>
<deps-load-path conf="core" pathid="core.ivy.classpath" />
<deps-load-path conf="test" pathid="test.ivy.classpath" />
<javac debug="true" includeantruntime="false" debuglevel="source,lines,vars" destdir="${bin.path}" source="1.8" target="1.8">
<src path="${xtext.project.path}/src"/>
<src path="${xtext.project.path}/src-gen"/>
<src path="${project.path}/src"/>
<src path="${project.path}/src-gen-umpletl"/>
<src path="${project.path}/src-gen-umple"/>
<src path="${project.path}/test"/>
<src path="${vendors.path}/jopt-simple/src"/>
<exclude name="**/.git"/>
<exclude name="**/*.ump" />
<exclude name="**/data" />
<classpath refid="project.classpath"/>
<classpath refid="validator.project.classpath"/>
<classpath refid="core.ivy.classpath" />
<classpath refid="test.ivy.classpath" />
<!-- Add compiler arguments here, see https://ant.apache.org/manual/using.html#arg for details, example below
<compilerarg value="-Xlint:deprecation" />
-->
</javac>
<copy todir="${bin.path}" overwrite="true">
<fileset dir="${project.path}/src"><include name="**/*.grammar"/></fileset>
<fileset dir="${project.path}/src"><include name="**/*.error"/></fileset>
</copy>
<delete file="cruise.umple/src/rules.grammar"/>
<delete file="cruise.umple/bin/rules.grammar"/>
</target>
You can see how <javac> compiles files by running Ant with the -verbose option.
For example, the following Ant script...
<javac debug="true" includeantruntime="false">
<src path="src1"/>
<src path="src2"/>
</javac>
...outputs the following with ant -verbose running on a Windows machine...
[javac] Compilation arguments:
[javac] '-classpath'
[javac] ''
[javac] '-sourcepath'
[javac] '.....\src1;.....\src2'
[javac] '-g'
In the above example, Ant combined the <src> elements into the semicolon-separated -sourcepath argument.
-sourcepath is an option of Oracle's javac tool:
-sourcepath sourcepath
Specifies the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by colons (:) on Oracle Solaris and semicolons on Windows and can be directories, JAR archives, or ZIP archives.
Note the distinction between Ant's <javac> task and Oracle's javac tool. The Ant <javac> task calls the Oracle javac tool.
For your question "What order are the files compiled in?", the answer is essentially: The Java files are all compiled at the same time.

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>

java.lang.NoClassDefFoundError + ant - running a jar

Tagging- Selenium as well just in case someone faced similar issue while creating selenium tests using Ant.
I have seen lot of questions/answers on this topic, tried all the options suggested on various forums but still my issue is not getting resolved. Basically i compile code(includes the test scripts), create JAR and run the same JAR. For some reason it does not seem to identify the libraries during run time. Same code(With tests) works fine when main() method is run from Eclipse. Here is the build.xml,
<project default="run">
<target name="clean">
<delete dir="build" />
</target>
<target name="init-classpath">
<path id="lib.classpath">
<fileset dir="./lib/">
<include name="**.jar" />
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="lib.classpath" />
<flattenmapper />
</pathconvert>
</target>
<target name="jar" depends="clean, init-classpath">
<javac classpathref="lib.classpath" destdir="./compiled" failonerror="true" srcdir="./src" />
<mkdir dir="build/jar" />
<jar destfile="build/jar/BANC.jar" basedir="compiled">
<manifest>
<attribute name="Main-Class" value="com.portico.driver.TestDriver" />
<attribute name="Class-Path" value="${mf.classpath}" />
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="build/jar/BANC.jar" fork="true">
</java>
</target>
</project>
Error:-Exception in thread "main" java.lang.NoClassDefFoundError: jxl/Workbook
Manifest content
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_41-b02 (Sun Microsystems Inc.)
Main-Class: com.portico.driver.TestDriver
Class-Path: activation.jar commons-lang-2.4.jar jna-3.4.0.jar jxl.jar
logging-selenium-1.2.jar mail.jar ojdbc14.jar poi-3.0.2-FINAL.jar rep
ortng-1.1.1.jar saxon-8.7.jar selenium-grid-demo-1.0.7.jar selenium-g
rid-demo-standalone-1.0.7.jar selenium-grid-hub-1.0.7.jar selenium-gr
id-hub-standalone-1.0.7.jar selenium-grid-remote-control-1.0.7.jar se
lenium-grid-remote-control-standalone-1.0.7.jar selenium-grid-tools-1
.0.7.jar selenium-grid-tools-standalone-1.0.7.jar selenium-server-1.0
.3-standalone.jar selenium-server-standalone-2.33.0.jar sikuli-script
.jar testng-6.8.jar velocity-1.7.jar
The first thing to check is, whether the problem is connected with the manifest or something else. If you can run your application with java -cp <allthejarfiles> <main-class> the problem is connected with the manifest. Keep in mind that the jar files specified in the manifest are relative to the jar file’s location. Trying to run the application with the -verbose:class option gives hint about which jar are really loaded.
Your manifest assumes the jars in the current working directory. So it would require dir attribute set to the folder where the jar exists.
Java task supports providing classpath in the arguments. Try giving the classpath in arguments.
<target name="run" depends="jar">
<java jar="build/jar/BANC.jar" fork="true" dir="build/jar" >
<classpath>
<pathelement path="${lib.classpath}"/>
</classpath>
</java>
</target>
I have faced the same issue in my project. I suggest that you should create a separate directory specially for the jar files, put all of your jars in that directory, and then point the lib address to that directory.
for example in your case say I have created a directory D:/jar_collection, where I have put all my jars physically.
<property name="lib.dir" value="D:/jar_collection"/>
<target name="setClassPath">
<path id="classpath_jars">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>
and it works fine. Please try it once.

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

Categories