tags:

views:

38

answers:

3

We use Ant to build a Java web application. The application will compile and run on both Tomcat 6 and Tomcat 5.5. However, the build process is slightly different depending on which version of Tomcat you have installed: 5.5 keeps jar files that the build needs in $CATALINA_HOME/common/lib and $CATALINA_HOME/server/lib, while 6.0 keeps them all in $CATALINA_HOME/lib.

How can I set up Ant to choose the correct classpath(s) depending on a setting in build.properties? I'd be ok with even having to list the directories to be included in the classpath in a setting in build.properties, but I haven't been able to make this work.

Any suggestions?

+4  A: 

Create 2 different path items:

<path id="path.tomcat55">
    <fileset ... (tomcat5.5 files) />
</path>

<path id="path.tomcat6">
    <fileset ... (tomcat6 files) />
</path>

Then create separate targets for building for 55, using the tomcat55 path and another for building 6 using tomcat6.

<target name="compile.tomcat55" depends="build">
    <echo message="Compiling for Tomcat 5.5" />
    <javac srcdir="${project.basedir}/src/test" destdir="${build.dir}" fork="true" source="1.5" classpathref="path.tomcat55" />
</target>

<target name="compile.tomcat6" depends="build">
    <echo message="Compiling for Tomcat 6" />
    <javac srcdir="${project.basedir}/src/test" destdir="${build.dir}" fork="true" source="1.5" classpathref="path.tomcat6" />
</target>

Then, just call the appropriate target.

Matthew Flynn
Thanks, that might be the way I have to do it. I was hoping to end up with just a single compile target, but at least this should work.
fizban
+2  A: 

Because the directory structure is mutually exclusive for Tomcat 5.5 and Tomcat 6.0, you may be able to specify all 3 of them and then ant will pick up only what's available:

<classpath>
  <fileset dir="${catalina.home}/lib"
    erroronmissingdir="false"
  >
    <include name="**/*.jar"/>
  </fileset>
  <fileset dir="${catalina.home}/common/lib"
    erroronmissingdir="false"
  >
    <include name="**/*.jar"/>
  </fileset>
  <fileset dir="${catalina.home}/server/lib"
    erroronmissingdir="false"
  >
    <include name="**/*.jar"/>
  </fileset>
</classpath>

Specify erroronmissingdir="false", so ant does not complain about missing directories.

Alexander Pogrebnyak
That doesn't seem to work for me, ant (or javac) complains about the missing directories.
fizban
@fizban: add `erroronmissingdir` argument.
Alexander Pogrebnyak
A: 

I would go the a simpler solution. Just add the 3 directories in the build path. Ant/javac should ignore if a classpath element is not found.

Thierry-Dimitri Roy