Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

2.4 Building a Product

Building a product involves packaging up only those elements to be delivered in a form that the customer can install into his or her environment. You can build the product in several different ways, including manually or by using a Windows batch script, a UNIX shell script, or an Apache Ant script. You can deliver the end product as a single compressed file or as a stand-alone executable. For our purposes, the Favorites plug-in will be delivered with source code as a single compressed zip file.

2.4.1 Building manually

Building a product manually involves launching an Eclipse Export wizard, filling out a few fields, and clicking the Finish button. Select the File > Export command to launch the desired export wizard. On the first wizard page (see Figure 2-14), select Deployable plug-ins and fragments and then click the Next button.

Figure 2-14

Figure 2-14 Export wizard page 1—choosing the type of export.

On the second page of the Export wizard (see Figure 2-15), select the plug-ins to be exported, enter the name of the zip file to contain the result, and select the options shown. In addition, specify that this export operation be saved as an Ant script in a file named build-favorites.xml in the com.qualityeclipse.favorites project and check the Include source code option, then click Finish.

Figure 2-15

Figure 2-15 Export wizard page 2—specifying the zip file’s contents.

The created zip file contains a single plug-in JAR file (a plug-in can be deployed as a single JAR file as of Eclipse 3.1):

plugins/com.qualityeclipse.favorites_1.0.0.jar

And that plug-in JAR file contains the plug-in as specified in the Export wizard:

com.qualityeclipse.favorites classes
com.qualityeclipse.favorites source files
plugin.xml
icons/sample.gif
META-INF/MANIFEST.MF

Unfortunately, this process is manual, and therefore prone to errors. Manually building a product is fine once or twice, but what if a different person in the company needs to build the product? What happens as the product grows and encompasses more plug-ins? A product needs a repeatable and reliable method for building it.

2.4.2 Building with Apache Ant

An Apache Ant script provides a reliable, flexible, and repeatable process for building a plug-in project. There is a little more up-front work to set up an Ant script, but it is much less error-prone over time than building a product manually. For more information about Ant and constructing more complex build scripts, see Chapter 19.

Eclipse can generate simple Ant scripts. The prior section specified that the Export wizard generates an Ant script file named build-favorites.xml in the com.qualityeclipse.favorites project:

<?xml version="1.0" encoding="UTF-8"?>
<project default="plugin_export" name="build">
   <target name="plugin_export">
      <pde.exportPlugins
         destination="C:\Build\QualityEclipse"
         exportSource="true"
         exportType="zip"
         filename="FavoritesProduct.zip"
         plugins="com.qualityeclipse.favorites"
         useJARFormat="true" />
   </target>
</project>

The preceding simple script works well from the Eclipse UI; however, unfortunately, the pde.exportPlugins and other pde.export* tasks are asynchronous and cannot be used in a headless environment (see Bugzilla entry 58413 at bugs.eclipse.org/bugs/show_bug.cgi?id=58413) making it difficult to build more than simple scripts.

If you want your build script to do more than just export plug-ins (see Section 3.2.1, Link files, on page 111), then you’ll need a more complex Ant script similar to the following. For more on Ant and build scripts, see Chapter 19, Building a Product.

<?xml version="1.0" encoding="UTF-8"?>
<project default="plugin_export" name="build">
   <target name="plugin_export">

      <!-- Define build directories -->
      <property name="build.root"
         location="/Build/QualityEclipse" />
      <property name="build.temp"
         location="${build.root}/temp" />
      <property name="build.out"
         location="${build.root}/product" />

      <!-- Create build directories -->
      <delete dir="${build.temp}" />
      <mkdir dir="${build.temp}" />
      <mkdir dir="${build.out}" />

      <!-- Read the MANIFEST.MF -->
      <copy file="META-INF/MANIFEST.MF" todir="${build.temp}" />
      <replace file="${build.temp}/MANIFEST.MF">
         <replacefilter token=":=" value="=" />
         <replacefilter token=":" value="=" />
         <replacetoken>;</replacetoken>
         <replacevalue>
         </replacevalue>
      </replace>
      <property file="${build.temp}/MANIFEST.MF"/>

      <!-- Plugin locations -->
      <property name="plugin.jarname" value=
         "com.qualityeclipse.favorites_${Bundle-Version}" />
      <property name="plugin.jar" location=
         "${build.temp}/jars/plugins/${plugin.jarname}.jar" />
      <property name="product.zip" value=
         "${build.out}/Favorites_v${Bundle-Version}.zip" />

      <!-- Assemble plug-in JAR -->
      <mkdir dir="${build.temp}/jars/plugins" />
      <zip destfile="${plugin.jar}">
         <zipfileset dir="bin" />
         <zipfileset dir="." includes="META-INF/MANIFEST.MF" />
         <zipfileset dir="." includes="plugin.xml" />
         <zipfileset dir="." includes="icons/*.gif" />
         <zipfileset dir="." includes="src/**/*" />
      </zip>

      <!-- Assemble the product zip -->
      <zip destfile="${product.zip}">
         <fileset dir="${build.temp}/jars" />
      </zip>

   </target>
</project>

To execute this Ant script, right-click on the build-favorites.xml file and select Run Ant... (see Figure 2-16). When the Ant wizard appears, click on the JRE tab and select the Run in the same JRE as the workspace option (see Figure 2-17). Click the Run button to build the product.

Figure 2-16

Figure 2-16 The build.xml popup context menu.

Figure 2-17

Figure 2-17 The Ant wizard.

  • + Share This
  • 🔖 Save To Your Account