Currently, we define a path with
<path id="sources.production">
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
and then compile using
<javac ...>
<src refid="sources.production"/>
<classpath refid="classpath.production">
</javac>
Now we create some modified .java files (original files in src/module2) before this <javac> call and have put them before the other sources:
<path id="sources.production">
<pathelement path="generated-sources"/>
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
Unfortunately, the compile fails now because the original and the modified .java files are both fed to the javac task. How to exclude the original source files easily from the javac's sources without large charges?
you need an intermediate step which you copy all your source files to a new location and override them with generated ones, then use this new location as source folder for javac.
it may look something like this:
<path id="sources.4compile" location="all-sources" />
<target name="prepare-4compile">
<!-- clean -->
<delete dir="all-sources"/>
<mkdir dir="all-sources"/>
<copy todir="all-sources">
<fileset dir="src/module1"/>
<fileset dir="src/module2"/>
<fileset dir="generated-sources"/>
</copy>
</target>
<target name="compile" depends="prepare-4compile">
<javac ...>
<src refid="sources.4compile" />
<classpath refid="classpath.production" />
</javac>
</target>
other way, you may specify a fileset to javac disabling javac's default searching mechanism as suggested here.
Related
I try to send email using JavaMail and activation jars and compiling my project with ant.
Ant classpath looks like this:
<path id="classpath.test">
<pathelement location="lib/javax.mail.jar"/>
<pathelement location="lib/activation.jar"/>
<pathelement location="build/classes/"/>
</path>
Ant compile looks like this:
<target name="compile">
<mkdir dir="build/classes/"/>
<javac srcdir="src" destdir="build/classes/">
<classpath refid="classpath.test"/>
</javac>
<copy todir="build/classes/">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
But when I try to execute this (also using ant), it throws
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/mail/MessagingException
How can I fix this?
If you need to add a jar to the classpath to compile the code, then you try the following <javac> and it will look like this:
<javac srcdir="./src" destdir="./build/classes">
<classpath>
<pathelement path="lib/javax.mail.jar"/>
<pathelement path="lib/activation.jar"/>
</classpath>
</javac>
The problem was that dependencies (javax.mail.jar and activation.jar) were not properly included in jar created by ant, so I created a fat jar, and after doing so everything worked.
I am trying to write a ant build which calls a Java class
<target name="validate" depends="forge-jar">
<taskdef name="spec" classname="SpecBuild" classpathref="java.classpath" />
<spec a="${forge}" mail="${mail}" dd="${dd}" wdd="${wdd}"/>
</target>
<target name="forge-jar" >
<path id="project.class.path">
<pathelement location="classes/" />
<pathelement path="classes/ant/tasks/SpecBuild.class" />
</path>
<path id="java.classpath">
<path refid="project.class.path" />
</path>
<javac includeantruntime="false" srcdir="src" destdir="classes" classpathref="java.classpath">
<filename name="**/SpecBuild.java" />
</javac>
</target>
In the SpecBuild.java class if I use slf4j logger I am getting this warning :-
CLASSPATH element /Users/classes/ant/tasks/SpecBuild.class is not a JAR.
[taskdef] CLASSPATH element /Users/classes/ant/tasks/SpecBuild.class is not a JAR.
Can anyone please help me fix this
You can specify the 'classes' directory in the class path as below. Remove the 'SpecBuild.class' file from pathElement location. You can either use 'includes' property and mention the file name in it or just include the 'classes' directory as below (exclude the JUni test cases package, if you have).
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
<dirset dir="${build.dir}">
<include name="apps/**/classes"/>
<exclude name="apps/**/*Test*"/>`enter code here`
</dirset>
</classpath>
Good luck.
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
How to compile java project using Apache ANT with the JRE system libraries and other libraries i have.
The code i am using is
<path id="master-classpath">
<fileset dir="junit">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<target name="build" description="Compile source tree java files">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" source="1.5" target="1.5" includeantruntime="false">
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
</target>
its only taking the jar files in the junit folder.
I dont know how to compile with JRE system libraries.
I have following ant script to generate the jar file
<project name="myProject" basedir="." default="jar">
<property name="src" value="Java Source"/>
<property name="output" value="bin"/>
<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="myProject.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="myProject.jar">
<fileset dir="bin"/>
</jar>
</target>
<target name="clean">
<delete dir="${output}"/>
</target>
<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>
When I run ant script i get following error
Reference myProject.classpath not found.
I am not sure how to solve this error. It requires path of .classpath file ?
I also tried with
refid="classpath"
and it didnt work.
Can anyone help please!
Thanks
You need to define first something like because right now MyProject.classpath is not defined:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
assuming that your classpath has what you need.
If it does not, create another entry under classpath element that has references to jars or whatever you need, or you need to custom specify path:
<path id="MyProject.classpath">
<pathelement location="lib/"/>
<pathelement path="${classpath}/"/>
<pathelement path="${additional.path}"/>
</path>
http://ant.apache.org/manual/using.html#path