views:

1676

answers:

4

I would like to make a very simple ant script that does 1 thing, which is to bulid a jar file. But when I try to use a very simple example, it fails due to dependancies on jars that my source depends on. So, How you you specify jars that there are jars that need to be in the class path when building an Ant target.

<project name="project" default="default">
<property name="src.dir"     value="src"/>
<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>
<property name="lib.dir"     value="//tomcat/common/lib"/>
<description> description </description>

<!-- ================================= 
      target: default              
     ================================= -->
<target name="default" depends="compile" description="description">
    <jar destfile="/path/to/dir/Library.jar">

    </jar>
</target>
  <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

</project>
A: 

Here is the ant file we use to build the Timeline opensource project. It is pretty straight forward. It doesn't build a jar, but it does use libraries to minimize JS files.

http://simile-widgets.googlecode.com/svn/timeline/trunk/build.xml

Larry

Larry K
+2  A: 

Your question isn't entirely clear - I suspect you mean you want to compile your source (with the javac task) and then build a jar file from the results. If that's not the case, I don't see where your source dependencies come into it. If that is the case, then the jar task is irrelevant.

In the javac task, use the classpath attribute to specify other jar dependencies.

Jon Skeet
I will look at the javac task, and see if that helps.
Milhous
So this was a good guess after all
Vincent Ramdhanie
+3  A: 

Here's an ANT script generated by using the Eclipse Runnable JAR Export Wizard. This is a project that updates stats on a Google Spreadsheet for a small fantasy baseball league with some friends. It gets the stats by scraping ESPN.com player pages.

Class-Path attribute inside the manifest element is used to set the classpath used by the jar. This defaulted "." but I had to add my src path explicitly so that log4j would pick up log4j.properties.

zipfileset elements are external jars used by my source that I wanted to be included with my jar. I suspect this might be what you're looking for.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project cob_fantasy_baseball">
    <!--this file was created by Eclipse Runnable JAR Export Wizard-->
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="C:/workspace/cob_fantasy_baseball/cob_fantasy_baseball.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Main-Class" value="com.me.cob_fantasy_baseball.UpdateCobStats"/>
                <attribute name="Class-Path" value=".;src/com/me/cob_fantasy_baseball"/>
            </manifest>
            <fileset dir="C:/workspace/cob_fantasy_baseball/classes"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/gdata/java/lib/gdata-core-1.0.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/gdata/java/lib/gdata-spreadsheet-2.0.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/jericho-html-2.6/lib/jericho-html-2.6.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/apache-log4j-1.2.15/log4j-1.2.15.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/jaf-1.1.1/activation.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/javamail-1.4.2/mail.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/javamail-1.4.2/lib/smtp.jar"/>
            <fileset dir="C:/workspace/cob_fantasy_baseball/src/com/me/cob_fantasy_baseball"/>
        </jar>
    </target>
</project>

Also, here's a link to the Ant documentation for the jar task: http://ant.apache.org/manual/CoreTasks/jar.html

Owen
+1  A: 

Based on your example you can just put libs inside javac:

<javac srcdir="${src.dir}" destdir="${classes.dir}">
    <classpath>
        <pathelement location="${lib.dir}/lib1.jar"/>
        <pathelement location="${lib.dir}/lib2.jar"/>
    </classpath>
</javac>
serg