other register

Saturday, January 26, 2008

Ant, Log4J, JUnit, Eclipse

1. Technical Specification

The target application environment Java compiler's version is Java 1.5.
Create a project in Eclipse.
Need to connect a database (e.g. Oracle).
Should log error message using Log4J.
Delivery the project as a jar.

2. Procedures

2.1 Create User Library for Log4J and JDBC driver (oracle-jdbc)

2.2 Add these two user libraries to the project build path

2.3 Establish The Application Environment in Eclipse

2.3.1 Create file as a property file for the application to read

2.3.2 Create a file for log4j to read

2.4 Follow the instructions to set up JUnit available to Ant

2.5 Create build.xml file

2.1 Create User Libraries

1. In Eclipse, Window > Preferences > Java > Build Path > User Libraries.
2. Click New button, and enter the 'user library name'.
3. Click Add JARs... button, and select the jar files to be added to this user library.
4. Click OK button.

Do the above steps for Log4J, and oracle-jdbc

2.2 Add User Libraries to the project

1. Right click the project, and select properties (or Alt+Enter).
2. Select Java Build Path > Libraries, and click Add Library... button
3. Select User Library, and click next> button
4. Select lo4j, and oracle-jdbc (the two we just created in the previous step).
5. Click OK to add these user libraries to the project environment.

Doing this can make sure these libraries are in the project classpath.

2.3 Establish Project Structure

1. Create build directory, and its sub directories (classes and jar)
md build/classes
md build/jar
2. Create lib directory, and copy log4j and oracle-jdbc jars in.

2.3.1 Create file as a property file for the application to read
name1 = value1
name2 = value2

2.3.2 In the src directory, create a file for log4j to read. Below is a sample

For more info about log4j, please look at: How to use log4j logging API and Log4J Manual

# Log levels
# Uncomment the following line to enable full loggin for every class
#log4j.rootLogger=trace, stdout, R
log4j.logger.pma=error, stdout, R

# Console appender configuration
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling File Appender
# Path and file name to store the log file.
# Keep one backup file
# Rolling File Appender layout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n


2.4 Follow the instructions to set up JUnit available to Ant

2.5 Create build.xml file

For more information about Ant and its build.xml file, please have a look at Tutorial: Hello World with Ant

Below is a sample build.xml file:


<?xml version="1.0"?>

<project name="pma" basedir="." default="main">
<property name="src.dir" value="src">
<property name="build.dir" value="build">
<property name="classes.dir" value="${build.dir}/classes">
<property name="jar.dir" value="${build.dir}/jar">
<property name="main-class" value="pma.PMADAO_Split">
<property name="lib.dir" value="lib">
<property name="report.dir" value="${build.dir}/junitreport">

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

<target name="clean">
<delete dir="${build.dir}">

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" excludes="**/*.java">

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${}.jar" basedir="${classes.dir}" update="true">

<zipfileset src="${lib.dir}/ojdbc14.jar"/>
<zipfileset src="${lib.dir}/log4j-1.2.15.jar"/>

<attribute name="Main-Class" value="${main-class}"/>

<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<path refid="classpath">
<path id="application" location="${jar.dir}/${}.jar">

<target name="clean-build" depends="clean,jar">
<target name="main" depends="clean,run">

<target name="junit" depends="jar">
<mkdir dir="${report.dir}" />
<junit printsummary="yes">
<path refid="classpath" />
<path refid="application" />
<formatter type="xml" />
<batchtest fork="yes" todir="${report.dir}">
<fileset dir="${src.dir}" includes="*" />

<target name="junitreport">
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}" includes="TEST-*.xml"/>
<report todir="${report.dir}"/>



In the task "jar", the following code can make sure the created jar (pma.jar) file contains the following libraries (log4j, and oracle-jdbc jars) in an extracted format. If the libraries (log4j, and oracle-jdbc jars) in the created jar (pma.jar) are in its jar format, then the application won't find the libraries in its classpath when running.

<zipfileset src="${lib.dir}/ojdbc14.jar"/>
<zipfileset src="${lib.dir}/log4j-1.2.15.jar"/>

In the task "compile", we have to add:

source="1.5" target="1.5" compiler="javac1.5" to make the application java 1.5 compatible on the target environment, otherwise it will complains about "class version something" when running.

No comments: