Java Tutorial - Java Scipt : Compiling the Bean

Java Tutorial - Java Scipt :

Compiling the Bean


We will use Ant to compile the bean. Ant uses a file named build.xml to describe how to build a project. In this section, we will cover the build.xml file needed to compile our code, construct the .jar file, and deploy the .jar file
to JBoss. This build file is based on Ant version 1.5. Following are the first two lines of this file:

<?xml version=”1.0” encoding=”UTF-8”?>
<project basedir=”.” default=”compile” name=”oldfriends”>
Each Ant build file must start with a project. The project attributes define the base directory for the project, the default target for the project, and the project name. The base directory is used as the relative directory for all directory accesses. The default target will be executed if no other target is requested on the command line. The name serves as the project name and is available as a reference through the build file. Ant provides the ability to set properties that can used throughout the file. These can also be stored in a properties file, as is shown in Chapter 10.
<property name=”app.name” value=”oldfriends”/>
<property name=”jar.name” value=”user-cmp”/
<property name=”jboss.home” value=”C:/jboss-3.0.6_tomcat-4.1.18”/>
<property name=”jboss.deploy”
value=”${jboss.home}/server/default/deploy”/>
<property name=”lib.dir” value=”${jboss.home}/client”/>

These lines should be edited to reflect the values needed for your system. You can replace these parameters with their values by using the ${parameter_ name} notation within the attributes of any tag. Ant provides a very useful capability to build classpaths. The next lines build a classpath that includes every .jar file in the client subdirectory of the JBoss distribution.

<path id=”oldfriends.classpath”>
<fileset dir=”${lib.dir}”>
<include name=”*.jar”/>
</fileset>
</path>

The next line is the first target in the build file. Atarget is something that Ant will try to build. This target is the “init” target and is designed to handle any initialization that might be needed. It doesn’t do anything yet, but is provided as a placeholder to meet future needs.

<target name=”init”/>
The next target, shown here, has the responsibility for compiling our code:
<target depends=”init” name=”compile”>
<javac classpathref=”oldfriends.classpath”
debug=”true”
deprecation=”true”
destdir=”.”
srcdir=”.”>
</javac>
</target>

The target invokes the Java compiler by using the javac task. A task is something that Ant knows how to do. Ant has a number of predefined tasks, and new tasks can be added by writing Java code for Ant. The javac task uses the previously defined classpath by including a classpathref attribute. The destdir and srcdir attributes provide the option to compile the code in one directory and send the class files to a different directory. The other attributes correspond to compiler command-line arguments. This target depends on the init target. This means that Ant will not execute this target until after the init target is up to date. The next target, shown in the following code, builds the .jar file:

<target depends=”compile” name=”jar”>
<jar jarfile=”${jar.name}.jar” update=”no”>
<fileset dir=”.”>
<include name=”**/*.class”/>
</fileset>
<metainf dir=”etc”/>
</jar>
</target>

The .jar task will run the .jar utility and copy all of the files described in the fileset to the .jar file. The metainf element within the .jar task copies the files in the /etc directory into the META-INF directory of the .jar file.

 If we try to name the source directory for the meta-inf file meta-inf, the task copies the files into the .jar directory meta-inf/meta-inf. This is incorrect. and the best workaround is not to name the directory meta-inf.
Therefore, we call our directory /etc.

This is how our ejb-jar.xml, jboss.xml, and jbosscmp-jdbc.xml files are moved to the correct directory within the .jar file. The next two targets, shown here, provide a convenient means to deploy the .jar file to JBoss or to undeploy it.

<target depends=”jar” name=”deploy”>
<copy file=”${jar.name}.jar” todir=”${jboss.deploy}”/>
</target>
<target depends=”” name=”undeploy”>
<delete file=”${jboss.deploy}/${jar.name}.jar”/>
</target>

The deploy target depends on the jar target, which in turn depends on the compile target. In this way, Ant automatically rebuilds anything that is needed before deploying the .jar file. One very nice feature of JBoss is that you can start the deployment process simply by copying the .jar file into the proper deployment directory. Likewise,
you can simply delete the file to undeploy it. We defined the deployment directory as the default directory earlier in the parameter section of the build.xml file.The next target is a useful utility for removing all of the compiled files.
<target depends=”init”
description=”Clean all build products.” name=”clean”>
<delete>
<fileset dir=”.”>
<include name=”**/*.class”/>
</fileset>
</delete>
<delete file=”${jar.name}.jar”/>
<delete dir=”apidoc”/>
</target>
</project >

We close the XML file by providing the project end tag. You can run the file using the command:
ant deploy If you are successful, the output should look something like the following:
Buildfile: build.xml
init:
compile:
[javac] Compiling 16 source files to C:\mysrc\chapter13
jar:
[jar] Building jar: C:\mysrc\chapter13\user-cmp.jar
deploy:
[copy] Copying 1 file to C:\jboss-3.0.6_tomcat-4.1.18\server\
default\deploy
BUILD SUCCESSFUL
Total time: 5 seconds
If JBoss is running, the output in the JBoss console when you deploy the bean should be something similar to the following:
00:38:29,403 INFO [EjbModule] Creating
00:38:29,433 INFO [EjbModule] Deploying UserEJB
00:38:29,503 INFO [EjbModule] Created
00:38:29,503 INFO [EjbModule] Starting
00:38:29,753 INFO [EjbModule] Started
00:38:29,753 INFO [MainDeployer] Deployed package:
file:/C:/openjava/jboss-3.0.6_tomcat-4.1.18/server/default/deploy/usercmp. jar