Creating a project builder Ant buildfile

To see how project builders work, we will create a simple project with a single source file and an Ant buildfile that jars up the single class file. Though this example uses Java, it should be noted that project builders are available for all projects, Java or otherwise.

  1. Create a Java project named 'HW'.
  2. Create a Java source file named 'HelloWorld' with a main method.
  3. Put a single 'System.out.println()' statement in the main method, and make it print a greeting of your choice.
  4. Save changes.
  5. Create a file named 'projectBuilder.xml', open the Ant editor on it, enter the following content, and save changes.
    Content of projectBuilder.xml file
  6. In the Navigator, select the HW project and choose Properties from its context menu.
  7. In the project properties dialog, select External Tools Builders, then click New....
  8. In the 'Choose configuration type' dialog, make sure 'Ant build' is selected, and click OK.
  9. The External Tools dialog appears.Set the name to 'Makejar'. In the Main tab, use the first Browse Workspace... button to set the Location to be the projectBuilder.xml buildfile created above. Then use the second Browse Workspace... button to set the Base Directory to be the HW project.
    Main tab of External tools dialog
  10. In the Refresh tab, we want to be sure that when our HelloWorld.jar is created, we see it in Eclipse. By default, no refreshing is done when a project builder finishes running, so check Refresh resource after running tool, then select ${project} in the list of scope variables. Because refreshing can be expensive, you should in general refresh the smallest entity that contains all resources that will be affected by your buildfile.
    Refresh tab of External tools dialog
  11. In the Targets tab, the default target should be selected.
  12. In the Build Options tab, you can specify when this project builder is executed. By default, this is set to full builds and incremental builds. Running your project builder during auto builds is possible, though not recommended because of performance concerns.
    Build options tab of External tools dialog
  13. Apply the changes and click OK.
  14. Back in the project properties dialog, you will now see a project builder named 'Makejar' that is set to run after the default Java Builder. Note that you can change the order so that your Ant buildfile runs before the Java builder, though that wouldn't make sense in this example. Click OK to save the project builder and close the dialog.
    Project properties dialog

For a Java project, the default Java Builder will always be run and cannot be removed. The Java Builder runs the internal Eclipse Java compiler which in turn is responsible for indexing your source so that searching, refactoring and many other features are available. Thus it is not possible to replace the internal Eclipse Java compiler by using a project builder. Your only option with the Java Builder is when it runs with respect to the project builders that you define.


Copyright IBM Corporation 2000, 2003. All Rights Reserved.