How to include external libraries in Ant build.xml file?
Asked Answered
O

3

6

In my Java source-code I want to use different classes from java archives (.jar) stored in my application's "lib" directory. But if I do "ant run" then I always get a "java.lang.NoClassDefFoundError" message. I tried several things to fix it but nothing worked... Maybe someone here can help me?

This is my build.properties file:

app.name=MyApplication
app.version=1.0
main.class=mypackage.MyMain
build.dir=build
classes.dir=${build.dir}/classes
jar.dir=${build.dir}/jar
dist.dir=dist
src.dir=src
test.dir=test
lib.dir=lib

This is my build.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="My Project" default="run" basedir=".">
  <description>My description.</description>

  <property file="build.properties" />
  <path id="classpath">
    <fileset dir="${lib.dir}" includes="*.jar"/>
  </path>

    <!-- Initialization -->
  <target name="init" description="Prepare needed directories.">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${jar.dir}" />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${lib.dir}" />
  </target>

    <!-- Cleanup -->
  <target name="clean" description="Remove all files created by the build/test process.">
    <delete dir="${classes.dir}" />
    <delete dir="${dist.dir}" />
  </target>

    <!-- Compile application -->
  <target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" 
       destdir="${classes.dir}" 
       debug="yes"
       includeantruntime="false">
      <!-- <classpath refid="classpath" /> -->
    </javac>
  </target>

    <!-- Java Archive -->
  <target name="jar" depends="compile">
    <!--<delete file="${jar.dir}/${app.name}-${app.version}.jar"/>-->
    <delete dir="${jar.dir}"/>
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${app.name}-${app.version}.jar" basedir="${classes.dir}">
      <manifest>
        <attribute name="Class-Path" value="${lib.dir}"/>
        <attribute name="Main-Class" value="${main.class}"/>
      </manifest>
    </jar>
  </target>

    <!-- Run application -->
  <target name="run" depends="jar">

    <java jar="${jar.dir}/${app.name}-${app.version}.jar" 
        fork="true">
    </java>
    <!--
    <java fork="true" classname="${main.class}">
      <classpath>
         <path refid="classpath"/>
         <path location="${jar.dir}/${app.name}-${app.version}.jar"/>
      </classpath>
    </java>
    -->
  </target>
</project>

It would be nice if anyone could help.

Cheers!

Benny

Ostentation answered 27/12, 2010 at 23:15 Comment(2)
what does your lib directory look like? Are all of the jar files dumped in that directory, or are there subdirectories?Southerly
All jar files are in that directory (without any sub-directories). The structure looks like: ./lib/log4j-1.2.9.jar, ./lib/cobertura.jar, ./lib/easymock-3.0.jar, ... How can I include those libraries in my jar file?Ostentation
D
3

Replacing classes.dir=${build.dir}/classes with classes.dir=build/classes and jar.dir=${build.dir}/jar with jar.dir=build/jar from your build.properties file will work.

Drews answered 7/11, 2012 at 14:8 Comment(0)
S
2

Please edit target jar as follows. I am sure it will work.

<target name="jar" depends="compile">

<delete dir="${jar.dir}"/>
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${app.name}-${app.version}.jar" basedir="${classes.dir}">
  <manifest>
    <attribute name="Class-Path" value="${lib.dir}"/>
    <attribute name="Main-Class" value="${main.class}"/>
  </manifest>
   <zipgroupfileset  dir="${lib.dir}"/>

</jar>

Shelve answered 12/2, 2014 at 14:47 Comment(0)
S
-1

I see that you have been trying to set your classpath for the <java> task.

Have you tried:

<java jar="${jar.dir}/${app.name}-${app.version}.jar" fork="true">
  <classpath refid="classpath" />
</java>

EDIT: double check the values in your properties file. Do you have trailing spaces for the value of lib.dir or jar.dir?

http://ant.apache.org/faq.html#properties-not-trimmed

ant failed to build my program via javac even when I put the needed jars in an external build.properties file and reference them by pathelement or classpath refid.

When ant loads properties from an external file it doesn't touch the value of properties, trailing blanks will not be trimmed for example.

If the value represents a file path, like a jar needed to compile, the task which requires the value, javac for example would fail to compile since it can't find the file due to trailing spaces.

Southerly answered 28/12, 2010 at 12:46 Comment(2)
When I use this, then I get an error while "ant compile". At the moment I extract every source-code files of my external jar files and put them into my own "src" directory. Then I can run my project without problems because the dependencies are included when compiling and running my project. But I think that this is not the best use-case. But until now I have not found any other solution that will provide the external jar files in my own jar file. I think I have to focus again on the classpath declaration in the manifest as you told me...Ostentation
@Benny Neugebauer - double check your properties file entries to see if there is any trailing whitespace for your path values.Southerly

© 2022 - 2024 — McMap. All rights reserved.