Wednesday, September 22, 2010

14. Build System Samples

It seems that there are a lot of examples on how to execute a java file using Ant via a .jar file. But it was a little more difficult to figure out how to compile and run a .java file outright.

Here's my simple java file:
package ant;

public class HelloAnt {
public static void main(String[] args) {
System.out.println("Hello Ant!");
}
}


The main things to remember when using a package or IDE like Eclipse is to:
  1. include the classpath (the build directory)
  2. include the package name before the class name. E.g., ant.HelloAnt
<project name="HelloAnt" default="run">

<description>
Simple Ant build file for Hello Ant
</description>

<property name="src.dir" location="src" />
<property name="build.dir" location="build" />

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

<target name="makedir">
<mkdir dir="${build.dir}" />
</target>

<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}" />
</target>

<target name="run" depends="compile">
<java classname="ant.HelloAnt" fork="true">
<classpath>
<path location="${build.dir}" />
</classpath>
</java>
</target>

</project>

No comments:

Post a Comment