ade19
ade19

Reputation: 1200

How to include jar lib in my Ant build

I have the below build file

 <?xml version="1.0" ?> 
<project name="ClientAdvisor" default="compress">

<target name="init">
    <mkdir dir="build/classes" />
    <mkdir dir="dist" />
</target>

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build/classes">

    </javac>
</target>

<target name="compress" depends="compile">
        <jar destfile="dist/clientadvisor.jar" basedir="build/classes">

        </jar>
</target>


<target name="clean">
    <delete dir="build" />
    <delete dir="dist" />
</target>

</project>

I also have a list of jar files in my lib folder needed to compile my source files. Every time I run the ant command however, I get 'Class does not exist' compile errors. I have tried the following

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build/classes">
        **<classpath>
    <pathelement path="lib/*.jar"/>
</classpath>**

    </javac>
</target>


 <target name="compress" depends="compile">
        <jar destfile="dist/clientadvisor.jar" basedir="build/classes">
             **<zipgroupfileset dir="lib/" includes="**/*.jar" />**
        </jar>
</target>

None of this however seems to resolve the problem. What am I doing wrong here?

Upvotes: 1

Views: 10000

Answers (2)

SMA
SMA

Reputation: 37093

You need to either use classpath or classpathref attribute with javac task like:

 <path id="classpath">
    <fileset dir=".. lib dir.." includes="**/*.jar"/>
</path>
 <javac srcdir="src" destdir="build/classes" classpathref="classpath">

OR

<javac srcdir="src" destdir="build/classes">
     <classpath refid="classpath" />
     ...

Upvotes: 1

Robert
Robert

Reputation: 42774

If you want to playe multiple JAR files in the classpath I would use a <fileset>instead of a <pathelement>:

sample from Apache Ant documentation

<classpath>
      <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
      ...
</classpath>

Upvotes: 3

Related Questions