Ant Script Example

I would like to make a very simple ant script that does 1 thing which is for dumping the jar file. But when I try to use a very simple example it fails due to the dependency on the jars my source depends on. So, as you indicate jars, that there are jars that should be in the classpath when constructing the 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>

      

+1


a source to share


4 answers


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>

      

+1


a source


Your question is not entirely clear - I suspect you want you to want to compile the source code (with the task javac

) and then create a jar file from the results. If it doesn't, I can't see where your original dependencies go into it. If so, then the task jar

is irrelevant.



In, javac

set the classpath attribute to specify other jar dependencies.

+3


a source


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

The Class-Path attribute inside the element is manifest

used to set the classpath used by the jar. This is the default "." but I had to add my src path explicitly so that log4j would pick up log4j.properties.

zipfileset

the items are external jars used by my source that I would like to include in my jar. I suspect this might be what you are 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 task jar: http://ant.apache.org/manual/Tasks/jar.html

+3


a source


Here is the ant file that we use to create the original Timeline project. It's pretty straight forward. It doesn't create a jar, but uses libraries to minify JS files.

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

Larry

0


a source







All Articles