java ant buildfile for project with dependencies - java

I am new to ant, but am trying to create an ant script that builds my current proeject with another project as a dependency. I have the ant script building my current project, but am unsure how to add the other project to the classpath. Neither project get put into jar files at the moment.
My current portion of the build.xml file is
<target name="run" depends="compile">
<java classname="com.mypackage.Main">
<classpath>
<pathelement location="../project1/out"/>
<pathelement location="${bin}"/>
</classpath>
</java>
</target>
Thanks for your help!

I was able to do it using
<target name="run" depends="compile">
<java classname="com.mypackage.Main" fork="true">
<classpath>
<dirset dir="${other.dir}">
<include name="out/**"/>
</dirset>
<pathelement location="${bin}" />
</classpath>
</java>
</target>
Where ${other.dir} is the relative path to the root of the other project.

Related

How to correctly add javamail and activation dependencies to ant build.xml file?

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.

run target in ant that depends on jar

I have a jar target in ant that makes a jar out of two classes that I have. Then, I want to run this jar in another target that depends on the jar target. Would there be an easy way to do this in ant? I have my compile and jar targets pasted below.
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
</project
To run a Java application, use the java task (see https://ant.apache.org/manual/Tasks/java.html for its documentation). An example from the docs:
<java jar="dist/test.jar" fork="true" failonerror="true" maxmemory="128m">
<arg value="arg1"/>
<arg value="arg2"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>

Add library jar files in Java Task?

The spark-de.jar used other jar files as libraries. Assume running spark-de.jar requires lib1.jar and lib2.jar, how to set them in the following Java task of Ant?
<target name="makeQuery">
<java classname="sparkDemo.SparkTest" failonerror="true">
<classpath>
<pathelement location="${basedir}/spark-de.jar"/>
</classpath>
</java>
</target>
This should do the trick:
<target name="makeQuery">
<java classname="sparkDemo.SparkTest" failonerror="true">
<classpath>
<pathelement location="${basedir}/spark-de.jar"/>
<pathelement location="${basedir}/lib1.jar"/>
<pathelement location="${basedir}/lib2.jar"/>
</classpath>
</java>
</target>

Eclipse don't recognize test folder as source folder when importing ant build file

I have an ant build file for a java project the project tree looks like this :
DataBaseFidling.
|-->src (contains production code source)
|-->tests (contains tests code source)
|-->bin (contains .class)
|-->reports(contains junit xml reports)
|-->build.xml
Whenever I import this project with eclipse using "Java Project From Existing Ant Build File", eclipse does not reconize the tests folder as a source folder.
What to do to fix this?
Here is the ant build file :
The DatabaseFidling Project.
<property name="src.dir" location="./src/" />
<property name="tests.dir" location="./tests/" />
<property name="bin.dir" location="./bin/" />
<property name="lib.dir" location="/home/chedy/workspace/lib"/>
<target name="clean">
<delete verbose="true">
<fileset dir="${bin.dir}"/>
</delete>
</target>
<target name="compile">
<javac srcdir="${src.dir}" destdir="${bin.dir}">
</javac>
<javac srcdir="${tests.dir}" destdir="${bin.dir}">
<classpath>
<pathelement location="${lib.dir}/junit4.jar"/>
<pathelement location="${lib.dir}/mockito-all-1.9.5.jar"/>
<pathelement location="${lib.dir}/SQLScriptRunner.jar"/>
</classpath>
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="yes" fork="true" >
<formatter type="xml"/>
<classpath>
<pathelement path="${bin.dir}"/>
<pathelement location="${lib.dir}/junit4.jar"/>
<pathelement location="${lib.dir}/mockito-all-1.9.5.jar"/>
<pathelement location="${lib.dir}/SQLScriptRunner.jar"/>
<pathelement location="${lib.dir}/mysql-connector-java-5.1.23-bin.jar" />
</classpath>
<batchtest todir="./report">
<fileset dir="${bin.dir}">
<include name="**/**Test*.*"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="run" depends="compile">
<java classname="com.esprit.is.Main" fork="true">
<classpath>
<pathelement path="${bin.dir}"/>
<pathelement location="${lib.dir}/mysql-connector-java-5.1.23-bin.jar" />
</classpath>
</java>
</target>
</project>
You can manually add the test folder as a source folder. Right click the project, Build Path -> Configure Build Path -> Java Build Path. In the Source tab, click Link Source then browse to your folder.
The compile target had to contain one javac task which compiles both the src and test folders.

Ant script to generate Jar - Reference not found error

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

Categories