Search code examples
javaantjavac

How to specify directory or file glob as javac classpath value with Apache Ant?


Is there a way to set the value of the javac classpath property in Apache Ant (v1.9.6) so that I don't have to literally specify all the jars that I want to include e.g. a directory or file glob.

So, if I have something like:

<javac classpath="./lib/one.jar":./lib/two.jar:./lib/three.jar..."

...is there anyway to just specify my ./lib directory once, like the way you can do when you run a java application like:

java -cp ./lib/'*'

I've tried that, and just using ./lib, or./lib/* or ./lib/*.jar but they don't work.


Solution

  • And another solution using a reusable classpath reference:

    <path id="compile.path">
      <fileset dir="lib" includes="*.jar"/>
    </path>
    
    <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    
    <junit printsummary="yes" haltonfailure="yes">
      <classpath>
        <path refid="compile.path"/>
        <pathelement path="${classes.dir}"/>
      </classpath>
      ..
      ..