Building HelloWorld with Ant
The best way to learn Ant is to build a very simple Java class using our build file. We'll need two files for our example: the Java source class and our build.xml file. Open your favorite text editor and enter the code in Listing 16.5 and 16.6. Save your Java file as HelloWord.java, and your Ant file as build.xml.
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World!");
}
}
<project name="HelloWorld" default="compile" >
<property name="build.dir" value="output"/>
<target name="init" description="Clean and setup directory">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="init" description="Compile source">
<javac srcdir="." destdir="${build.dir}"/>
</target>
</project>
Let's take a line-by-line look at our build file:
- Line 1: We set the name of our project and the default target.
- Line 2: We use a user-defined property to store our build output directory name (output in our case).
- Line 3: We define the first of our two targets. The init target initializes or prepares the output directory.
- Line 4: We perform a recursive delete on our output directory.
- Line 5: We create the output directory using our build.dir property. Ant will replace ${"build.dir"} with the value of the build.dir property.
- Line 7: We define our main target and include a dependency on the init target. This means that the target compile will execute init before it begins its own tasks.
- Line 8: We use the javac task to call the Java compiler on any .java files located in the current folder. The destdir attribute holds the value of our output directory.
- All that remains now is to run Ant across our build file. We're using the default filename (build.xml), so there is no need to specify the build file when we invoke Ant. Open a command window and navigate to the folder you saved your sample files in. We've used c:\ant as our source folder, but usually your build root will exist under your main source or project directory. To build HelloWorld, simply type Ant <enter> at the command prompt. You should see output we have displayed in Figure 16.3.Congratulations, you've built your first application with Ant! You'll find the result of the build, Helloworld.class, in the output directory. We caused Ant to execute the default target (compiler) by not specifying any startup parameters. It's likely that you'll want to call particular targets, such as a release or debug build. In our case, we'll call the init target to clear out the results of our sample build. To do this, we run Ant withAnt init
Finally, we can get a listing that describes the build file by calling Ant with the projecthelp switch. To run this enter the following at the command prompt:Ant -projecthelpThe projecthelp switch causes Ant to parse through your build file and print out the description attributes it finds. Figure 16.5 illustrates the results of executing Ant with projecthelp on our sample build file.Most of this hour has been slanted toward Java development, but we don't want to leave Microsoft readers in the cold! With this in mind, we'll end with some tips to using Ant with Microsoft .NET.
No comments:
Post a Comment