5.2 JARing Your Output

     

Here's another example; in this case, we'll build an Eclipse project and store the resulting class in a JAR file. You won't need to be an Ant professional to follow along because we're interested in looking at Ant from an Eclipse point of view, not in the details of Ant per se . This example is designed to give you the basics of creating a working Ant build file in Eclipse; if you want more details on Ant itself, take a look at the manual at http://ant.apache.org/manual/index.html.

Our goal here is to create a new Java project in Eclipse, use Ant to compile it, and store the resulting .class file in a JAR file. To follow along, create a new Java project, Ch05_02 . To emulate a somewhat real-world project, we're going to store the example's source code in a directory named src and its output in a directory named bin . You can set those directories up when you create the project in the third pane of the New Java Project dialog by clicking the Source tab, then clicking the Add Folder button, then the Create New Folder button to open the New Folder dialog. Enter the name src in the Folder name box and click OK twice. Eclipse will ask if you want to remove the project as source folder and update the build output folder to Ch05_02/bin . Click Yes, then click Finish to create the new project, which will be complete with src and bin folders.

Next, add a new class, Ch05_02 , in a package named org.eclipsebook.ch05 , to the project. Add code to the main method in this example to display the message "This code was built using Ant.", as you can see in Example 5-2.

Example 5-2. A sample project
 package org.eclipsebook.ch05; public class Ch05_02 {     public static void main(String[] args) {  System.out.println("This code was built using Ant.");  } } 

Finally, add build.xml to the project by right-clicking the project in the Package Explorer and selecting New File. Type build.xml in the File name box and click Finish, which creates the file and opens it in the Ant editor. We'll start writing build.xml with the standard XML declaration and a <project> element that identifies the Main Build task as the default:

 <?xml version="1.0" encoding = "UTF-8"?> <project name="Ch05_01" default="Main Build" basedir=".">         .         .         . </project> 

Next we'll create the properties corresponding to the directories we'll use src , bin , a directory for the JAR file, jardir (we'll create a lib directory under the bin directory to store JAR files), and the JAR file itself, jarfile (we'll call this file Ch05_02.jar ). Setting up properties this way lets you access these directory names later in the build file. We'll also set the build.compiler property to the adapter for the JDT compiler, org.eclipse.jdt. core .JDTCompilerAdapter , which Ant will use:

 <?xml version="1.0" encoding = "UTF-8"?> <project name="Ch05_01" default="Main Build" basedir=".">  <property name="bin" location="bin"/>   <property name="src" location="src"/>   <property name="jardir" location="${bin}/lib"/>   <property name="jarfile" location="${jardir}/Ch05_02.jar"/>   <property name="build.compiler  "  value="org.eclipse.jdt.core.JDTCompilerAdapter"/>  .         .         . </project> 

Now we'll create the main task, Main Build . We'll use three stages in this Ant filean initialization stage, a compile stage, and a JAR-creation stageeach with its own task: Initialize , Compile , and Jar . To make sure that all those tasks are performed, we'll make the main, default task dependent on them using the depends attribute. Then the main task only has to echo a message to the console indicating that Ant is at workAnt will take care of the details of running each needed task:

 <?xml version="1.0" encoding = "UTF-8"?> <project name="Ch05_01" default="Main Build" basedir=".">     <property name="bin" location="bin"/>     <property name="src" location="src"/>     <property name="jardir" location="${bin}/lib"/>     <property name="jarfile" location="${jardir}/Ch05_02.jar"/>      <property name="build.compiler"          value="org.eclipse.jdt.core.JDTCompilerAdapter"/>  <target name="Main Build" depends="Initialize, Compile, Jar">   <echo message="Ant at work!"/>   </target>  .         .         . </project> 

The Initialize task will delete everything in the output ${bin} and ${jardir} directories and then recreate them:

 <target name="Initialize">     <delete dir="${bin}"/>     <delete dir="${jardir}"/>     <mkdir dir="${bin}"/>     <mkdir dir="${jardir}"/> </target> 

The Compile task will compile the source files in ${src} (which is just Ch05_02.java ) and put the resulting .class file into ${bin} :

 <target name="Compile" depends="Initialize">     <javac srcdir="${src}"          destdir="${bin}">     </javac> </target> 

Finally, the Jar task will compress Ch05_02.class into a JAR file and store that file as ${jarfile} note that this task depends on the Initialize and Compile tasks:

 <target name="Jar" depends="Initialize, Compile">     <jar destfile="${jarfile}" basedir="${bin}"/> </target> 

That completes build.xml ; you can see the whole file in Example 5-3.

Example 5-3. A sample Ant build file
 <?xml version="1.0" encoding = "UTF-8"?> <project name="Ch05_01" default="Main Build" basedir=".">     <property name="bin" location="bin"/>     <property name="src" location="src"/>     <property name="jardir" location="${bin}/lib"/>     <property name="jarfile" location="${jardir}/ch05_01.jar"/>      <property name="build.compiler"          value="org.eclipse.jdt.core.JDTCompilerAdapter"/>           <target name="Main Build" depends="Initialize, Compile, Jar">         <echo message="Ant at work!"/>     </target>          <target name="Initialize">         <delete dir="${bin}"/>         <delete dir="${jardir}"/>         <mkdir dir="${bin}"/>         <mkdir dir="${jardir}"/>     </target>     <target name="Compile" depends="Initialize">         <javac srcdir="${src}"              destdir="${bin}">         </javac>     </target>     <target name="Jar" depends="Initialize, Compile">         <jar destfile="${jarfile}" basedir="${bin}"/>     </target> </project> 

Eclipse can generate Ant scripts for you under certain circumstances. If your project already has an XML-based manifest file, as the plug-in projects we're going to create in Chapter 11 and Chapter 12 will have (the plug-in manifest file is named plugin.xml ), all you have to do is right-click the manifest file and select the Create Ant Build File item.


When you enter this XML into build.xml , you can see its properties and tasks in the Outline view, as in Figure 5-4.

Figure 5-4. Our new build.xml
figs/ecps_0504.gif

To build the project, right-click build.xml in the Package Explorer and select Run Ant. This opens the Ch05_02 build.xml dialog you see in Figure 5-5. You can see the various Ant targets we've set up here, which you can build independently. The default target, Main Build , is already selected, so just click Run now to build the project.

Figure 5-5. Selecting which target to run
figs/ecps_0505.gif

You can see the results in Figure 5-6the build was successful, as you see in the Console view.

Figure 5-6. A successful build
figs/ecps_0506.gif

Here's the complete text that appears in the Console viewyou can see the results of each task as it runs:

 Buildfile: D:\eclipse211\eclipse\workspace\Ch05_02\build.xml Initialize:       [delete] Deleting directory D:\eclipse211\eclipse\workspace\Ch05_02\bin        [mkdir] Created dir: D:\eclipse211\eclipse\workspace\Ch05_02\bin        [mkdir] Created dir: D:\eclipse211\eclipse\workspace\Ch05_02\bin\lib Compile:        [javac] Compiling 1 source file to D:\eclipse211\eclipse\workspace\Ch05_02\bin        [javac] D:\eclipse211\eclipse\workspace\Ch05_02\src\org\eclipsebook\ch05\Ch05_ 02.java        [javac] Compiled 20 lines in 210 ms (95.2 lines/s)        [javac] 1 .class file generated Jar:          [jar] Building jar: D:\eclipse211\eclipse\workspace\Ch05_02\bin\lib\ch05_01. jar Main Build:         [echo] Ant at work! BUILD SUCCESSFUL Total time: 1 second 

And that's itthe project was built and Ch05_02.jar was created in the bin/lib directory. As you can see, Ant lets you go far beyond the normal Eclipse build process to copy files, create directories, create JAR files, and so on.



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

Similar book on Amazon

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