home.


Tagged: ant-javac


ant-simple

Here’s a fairly bare-bones ant example, the one I’m using for BriefWebServer.

It makes a jar in build/jar, and optionally run the jar.

<project name="BriefWebServer" basedir="." default="jar">
    <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="jar.name" value="BriefWebServer.jar" />
    <property name="package.name" value="org.denevell.briefwebserver" />

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

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${jar.name}" basedir="${classes.dir}">
          <manifest>
            <attribute name="Main-Class" value="org.denevell.briefwebserver.BriefWebServer" />
          </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/BriefWebServer.jar" fork="true" />
    </target>

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

</project>
ant ant-basic ant-delete ant-mkdir ant-property ant-javac ant-jar

Ant classpath and javac

You can add to the classpath giving pathelement tags to a path element. The path’s id will be used later on to reference this.

<path id="class.path">
    <pathelement location="lib/somjar.jar" />
    <pathelement location="${build.dir}" />       
</path>

(We’re including the classes in our build directory here, as when we run junit test we’ll need to find out where they are.)

Here’s the javac tag which takes a source and destination attributes. You can put a classpath tag in within, to reference the previous one we made.

<target name="compile" depends="clean, mkdirs">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
       <classpath refid="class.path" />
    </javac>
</target>
ant ant-classpath ant-javac

Page 1 of 1