Ant build overview and J2EE targets.htm

Software Engineering
Ant build overview and J2EE targets

Description of Ant targets and build.xml file:

 

This is a basic introductory article to Ant. It discusses the following:

1.       Build.xml targets for a basic build.xml file.

2.       Good structure and practices for build.xml file. Using build.properties, classpath etc.

3.       Typical targets for an EJB/J2EE build.

 

Note: Some points are related to JBoss application server, and eclipse. This is because this article is based on training I gave on JBoss, Eclipse, Ant, and more… (phew). Using build.properties and other good practices, the examples can easily be used for another application server say Weblogic.

 

Overview of Ant and folder structure

  1. Ant is a Java tool, which helps to manage compilation, build, deploy, and testing of Java applications.
  2. Uses XML syntax to define the structure of the configuration elements of a build.
  3. Eclipse has Ant plugin, which is inbuilt, so there is no need to install Ant separately when using Ant through Eclipse.

 

Folders and files used in projects

 

Folder/File

Purpose

Src

Will contain java source files for EJBs and servlets

Classes

Classes will be compiled into this folder

Dist

Jars, Wars, and Ears are built into this folder, from where they can then be deployed

Config

Holds *.xml deployment descriptors for Jar, War, and Ear

Lib

Contains libraries used by application

build.xml

Ant file for building of project

build.properties

Properties file used to configure variables inside build.xml

 

 

 

 

 

 

 

Advantages of having a build.properties file:

 

It is a best practice in development using Ant, to use a separate properties file, from which the build.xml build file, can pick up the values of build related variables.

 

  1. Path to jar files can be configured centrally, e.g. dir.lib -> lib.
  2. Path to source and compiled classes folders, can be defined by single variables, e.g., dir.src, and dir.classes.
  3. Path to packaging folders, dir.dist.
  4. Path to xml deployment descriptors, e.g. dir.config.
  5. Configuration for accessing JNDI, and naming server access, can be handled easily. E.g. java.naming.factory.initial can be changed to point to JBoss or Weblogic class for JNDI.

 

Description of Ant targets and build.xml file:

 

 

Project Name

 

<project name="Product" default="all" basedir=".">

<property file="build.properties"/>

 

  1. Each project must have a project name, and must have a default target. Basedir attribute is used to refer to folder relative to where the build.xml file is placed.

 

 

 

The following examples use a build.properties similar to one below:

 

java.naming.factory.initial = org.jnp.interfaces.NamingContextFactory

debug = on

propertiesfile_name = build.properties

dir.lib = lib

dir.config = config

jvm = java.exe

dir.classes = classes

ejb_jar = Product.jar

debuglevel = lines,\

vars,\

source

dir.src = src

appserver_ejbjar_DD_name = jboss.xml

deploy_folder = c:\\java\\jboss-3.2.3\\server\\default\\deploy

basedir = e:\\src\\product

dir.dist = dist

java.naming.provider.url = localhost

java.naming.factory.url.pkgs = org.jboss.naming

 

 

Classpath

 

Classpath is used for compilation using javac, as well as for running tests using java. Since classpath settings create many troubles, it is best to set it in a single place at the top of ant build file.

 

<path id="classpath.base">

<fileset dir="${dir.lib}" includes="**/*.jar"/>

<pathelement location="${dir.classes}" />

</path>

 

The above classpath setting defines a variable for classpath, which can be referred to as follows later in build file:

 

<classpath refid="classpath.base"/>

 

The following entry allows all jars needed by application (for J2EE, naming etc) inside dir.lib folder to be included in classpath:

<fileset dir="${dir.lib}" includes="**/*.jar"/>

 

Following entry adds to classpath, the path to compiled classes:

<pathelement location="${dir.classes}" />

 

 

Target: init

<target name="init" depends="eclipse">

<mkdir dir="${dir.dist}" />

<mkdir dir="${dir.classes}" />

</target>

 

Purpose: init target is useful to create the folders required for compilation, Jars etc.

 

Target: clean

<target name="clean">

<delete dir="${dir.classes}" />

<delete dir="${dir.dist}" />

</target>

 

Purpose: clean target is used to clean all compiled files, build Jars from the built folders. Essentially, this is used to create a fresh build of application.

 

Target: eclipse

<target name="eclipse" if="eclipse.running">

<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />

</target>

 

Purpose: This target is used internally by eclipse to set parameters for Java compilation through eclipse environment. The if="eclipse.running" specifies that the target will be run only when the build file is being executed inside eclipse.

 

Target: compile

 

<target name="compile" depends="init">

<javac srcdir="${dir.src}" destdir="${dir.classes}" deprecation="on" debug="${debug}" debuglevel="${debuglevel}" compiler="modern">

<classpath refid="classpath.base"/>

</javac>

</target>

 

Purpose: Used to compile the java source files. It has many options, some of which are shown above.

 

 

Target: ejb_jar

 

<target name="ejb_jar" depends="compile">

<jar destfile="${dir.dist}/${ejb_jar}">

<fileset dir="${dir.config}"/>

<fileset dir="${dir.classes}"/>

</jar>

</target>

 

Depends on: compile

Purpose: Builds jar file from compiled classes. Adds *.xml deployment descriptors also to jar.

 

 

Target: deploy

<target name="deploy" depends="ejb_jar">

<copy file="${dir.dist}/${ejb_jar}" todir="${deploy_folder}"/>

</target>

 

Purpose: To deploy the built jar or ear to application server. It does it by copying the build jar file to the appropriate folder of application server.

 

 

Target: undeploy

 

<target name="undeploy">

<delete file="${deploy_folder}/${ejb_jar}" />

</target>

 

Purpose: To undeploy the built jar or ear from the application server. Deletes the earlier copied jar file from the application server folder.

 

Target: run

 

<target name="run" depends="deploy">

<java classname="examples.HelloClient" fork="true" dir="${dir.classes}">

<sysproperty key ="java.naming.factory.initial" value="${java.naming.factory.initial}"/>

<sysproperty key ="java.naming.provider.url" value="${java.naming.provider.url}"/>

<classpath refid="classpath.base"/>

</java>

</target>

 

Purpose: Can be used to run a test, for verifying that the build and application was proper. Note that by using variables for naming factory, and JNDI provider, the client can easily be run against another application server, say Weblogic. All that is required is to change the settings in build.properties.