5.1 Working with Ant

     

To use Ant from Eclipse, create a new project, Ch05_01 , and add a new class to it, Ch05_01 . In this class's main method, we'll just display the message "No worries.", as you see in Example 5-1.

Example 5-1. A sample project
 package org.eclipsebook.ch05; public class Ch05_01 {         public static void main(String[] args) {  System.out.println("No worries.");  } } 

To work with Ant, we'll need an Ant build file, which is named build.xml by default. To create that file in this project, right-click the project and select New File. Enter the name of the new file, build.xml , in the File name box and click Finish. You use the XML in this file to tell Ant how to build your project. Although Eclipse can automate the connection to Ant, Ant needs this XML build file to understand what you want it to do, which means that we will have to master the syntax in this file.

Eclipse recognizes that build.xml is an Ant build file and marks it with an ant icon, as you see at left in Figure 5-1.

Figure 5-1. Creating a build.xml file
figs/ecps_0501.gif

Enter this simple XML into build.xml all we're going to do here is have Ant echo a message, "Ant at work!", to the console:

 <?xml version = "1.0" encoding="UTF-8" ?> <project name = "Ch05_01" default = "Main Build">     <target name = "Main Build">         <echo message = "Ant at work!" />     </target> </project> 

This XML makes the target , which we've named "Main Build," into the default target . An Ant target specifies a set of tasks you want to have run, similar to a method in Java. The default target is that which is executed when no specific target is supplied to Ant. In this case, we're just going to have this default target echo a message to the console; nothing's going to be compiled.

You can see this new XML in the Ant editor in Figure 5-2. The Ant editor uses syntax highlighting, just as the JDT editor does, and you can see an outline of the build.xml document in the Outline view at right.

Figure 5-2. Entering XML in build.xml
figs/ecps_0502.gif

Save build.xml and right-click it, selecting the Run Ant item. This opens the Ch05_01 build.xml dialog you see in Figure 5-3. You can see that the default target, Main Build , is already selected.

Figure 5-3. Running Ant
figs/ecps_0503.gif

Click the Run button in this dialog to run Ant. When you do, you'll see something like this in the Console view (note that your message was echoed here):

 Buildfile: D:\eclipse211\eclipse\workspace\Ch05_01\build.xml Main Build:         [echo] Ant at work! BUILD SUCCESSFUL Total time: 430 milliseconds 

That's exactly what we wanted this example to do. That's a quick example to show how to interact with Ant from Eclipse, but all it does is display the message "Ant at work!" It doesn't compile anything, it doesn't create any JAR files, but it does give us a start on using Ant in Eclipse. We'll get more advanced in our next example.



Eclipse
Eclipse
ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net