Editing Ant buildfiles

Because Ant buildfiles are simple text files, you can use any text editor to edit them. But there are several advantages to using the Eclipse Ant editor, including syntax coloring, content assist and an outline view. To get familiar with the Eclipse Ant editor, we will create a simple Ant buildfile using this editor.

  1. Create an Ant buildfile called HelloWorld.xml (see the previous page if necessary).
  2. Open the Ant editor on the file by selecting Open With>Ant Editor from the file's context menu. Note that the default editor for a .xml file is a simple text editor, but this can be changed in the Window > Preferences > Workbench > File Associations.
  3. Enter the following content in the editor.

    HelloWorld file content

  4. Notice the syntax coloring for property values.
  5. Begin to enter a second target by typing '<tar', then hit Ctrl-Space to activate content assist. You are presented with a list of valid completions. In this case there is only one, the <target> tag. Select this completion and notice that the editor inserts both the opening and closing tags and leaves the cursor positioned for you to enter properties for this tag.
  6. Name this target 'World'.
  7. Enter an 'echo' task for this target similar to that for the Hello target, but change the text to 'World'.
  8. Save the changes to HelloWorld.xml.
  9. Make the Outline view visible and notice that there are entries for each property and each target. In addition, each task under a target has an entry.
    Outline view for an Ant buildfile
  10. Clicking on an entry in the Outline view will scroll the editor to that entry. In addition, when the Outline view has focus, typing a character will move the selection in the Outline view to the next visible entry beginning with that character.
  11. Position the cursor just past the end of one of the '<target>' tags, type '<tar', then hit Ctrl-Space to activate content assist. Notice that now the only valid completion is the 'tar' tag. This is because the Ant editor knows that nested targets are not allowed. Previously, when we used content assist to create a target tag, the editor knew that a tar task was not allowed outside of a target.
    Code assist in Ant editor
  12. Close the editor and do not save changes.


Copyright IBM Corporation 2000, 2003. All Rights Reserved.