Recipe 7.2 Building an Eclipse Application Using Ant


7.2.1 Problem

You have an Ant build file, and you want to build your application.

7.2.2 Solution

Right-click the build file, click Run Ant, choose the Ant target(s) to execute, and click Run.

7.2.3 Discussion

You created a build.xml file for an Eclipse project in the previous recipe; to run that file in the version of Ant that comes with Eclipse, right-click build.xml, and click Run Ant. Doing so displays the AntProject build.xml dialog shown in Figure 7-4. You can see the Ant targets created there with the default Build target already selected. You can select targets to run independently, but in this case just leave Build selected, and click Run to execute all the targets in the build file and build the project.

You also can start an Ant build by highlighting the project in the Package Explorer and selecting Run External Tools Run As Ant Build. If you want to select the targets to run, you also can select Run External Tools External Tools Targets, select the targets you want to run, and click Run.

Figure 7-4. Running Ant

The results appear in Eclipse in Figure 7-5. Note the message we made Ant echo to the Console view here, Ant is building your project ., followed by the message BUILD SUCCESSFUL .

Figure 7-5. A successful Ant build

Here's the text that appears in the Console view as Ant executes each target:

 Buildfile: D:\eclipse212\eclipse\workspace\AntProject\build.xml Initialization:   [delete] Deleting directory D:\eclipse212\eclipse\workspace\AntProject\bin   [mkdir] Created dir: D:\eclipse212\eclipse\workspace\AntProject\bin   [mkdir] Created dir: D:\eclipse212\eclipse\workspace\AntProject\bin\lib Compilation:   [javac] Compiling 1 source file to D:\eclipse212\eclipse\workspace\AntProject\bin   [javac] Compiled 22 lines in 381 ms (57.7 lines/s)   [javac] 1 .class file generated Jar:   [jar] Building jar: D:\eclipse212\eclipse\workspace\AntProject\bin\lib      \AntProject.jar Build:   [echo] Ant is building your project. BUILD SUCCESSFUL Total time: 922 milliseconds 

If you switch to the Navigator view, you can see the new lib directory containing the .jar file, AntProject.jar , as shown in Figure 7-6.

Figure 7-6. The new .jar file
figs/ecb_0706.gif Eclipse 3.0

By default in Eclipse 3.0, Ant executes in the JVM that is running Eclipse. That means you don't have to set the build.compiler property, as we have in this example, to use the same JVM as Eclipse is using. In Eclipse 3.0, you can set what JVM Ant should use when by right-clicking build.xml, clicking the JRE tab, and then clicking Run Ant.

7.2.4 See Also

Chapter 5 of Eclipse (O'Reilly).

Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: