This page will show you how to write basic Ant script to automate the build process. Ant allows Java developers to automate common tasks, such as:

  • compile source code
  • copy files from one location to another
  • delete files or folders from a location
  • create Javadoc
  • package application binaries into EAR file

File and Directory Structure

The main build script is commonly named as build.xml. But you can also name it to anything you choose as long as you keep the .xml extension. We recommend that you create a new folder in the application base directory to store build scripts and property files. In the example below, a new folder called BuildScripts is created in the application base directory.

During the build process, Ant will compile source codes and create application artifact (EAR, JAR files) in the output folder. We recommend that you specify a new folder in the application base directory to store the output files and artifacts. In the example below, a new folder called target is created in the application base directory. Please note that the target folder will be created by Ant during the build process. Therefore, you should exclude this folder in the Subversion repository.

 In order to successfully compile the code and hava Bamboo retrieve the packaged .ear files, you must have a proper structure defined in your ANT script. 

Bamboo will automatically check-out your source code to a working directory on the build server. To implement the build scructure, create an ANT script that does the following:

  1. Create a target folder in the working directory which will be used to compile and package the build.

  2. Create an artifacts folder within the Target folder to hold the packaged artifacts based on the Environment (Dev, Stage, Prod...)

  3. Create an output directory within the Target folder to stage the compiled classes, property files and the WebContent files required for packaging the .jar, .war and .ear files.

Build Script Templates

Please use the templates below as a guide to customize your build process.

 build.xml:  build file that compiles and packages the application in the target directory.
 clean-target.xml: build file that cleans and prepares the target directory for the build.
build.version:  Version file that holds the details of the build Property file that holds the absolute paths to files required to build the application

The following sections explain the use of the build.xml template.


This is the build script that performs the java code compilation, packaging of the jar, war and ear files, and make the artifacts available through bamboo in the target/artifacts directory. The main target is the default target that is initiated from Bamboo, which in turn depends on other targets to successfully complete the build process.

The following diagram illustrates the order of build commands in the build.xml file: 


Set Up Property Values

Specify any specific properties that might be required by this build script, including reference to the file.

<property file="" /> 
<property file="clean-target.xml" /> 
<property name="Name" value="project"/>
<property name="version" value="${bambooBuildKey}"/>
<property name="projectName" value="project"/>
<property name="build.env" value="${deploy.env}"/>
<property name="project.artifacts" value="${build.artifacts}/${build.env}/projectName"/>

Clean the Target Location

The cleanTarget target removes all contents in the target directory. Its build steps are in the clean-target.xml. Therefore, we need to provide a reference in the build.xml file.

<target name="cleanTarget">
<ant antfile="clean-target.xml" dir="." target="main"/>

Prepare the Target Location

The target location contains the output files. We should create them prior to compiling source codes.

<target name="prepareTarget" depends="cleanMain">
<mkdir dir="${project.artifacts}"/>
   <mkdir dir="${}"/>
   <mkdir dir="${}"/>
   <mkdir dir="${project.ejb.classes}"/>
   <mkdir dir="${}"/>
   <mkdir dir="${}"/>
   <mkdir dir="${project.web.classes}"/>

Compile Source Codes

The next step is to compile source codes. Ant uses the javac task to compile. You can also specify classpath using the path task. In the template, he Java project requires the EJB project library. Therefore, we will first compile the EJB project.

<target name="compileEjb" depends="prepareTarget">
<path id="app.ejb.classpath">
<fileset dir="${project.source.lib}" includes="**/*.jar"/> 
<fileset dir="${websphere.lib}" includes="**/*.jar"/>        
   <javac encoding="${compiler.encoding}" destdir="${project.ejb.classes}" classpath="${}"
debug="on" deprecation="off" optimize="on" fork="false" verbose="false">
<src path="${project.ejb.source}"/>
      <classpath refid="app.ejb.classpath"/>

The next step is to create the JAR file for the EJB project

 <target name="createEJBJar" depends="compileEjb">
<jar jarfile="${project.artifacts}/${name.ejb.jar}" manifest="${}/WebContent/META-INF/manifest">
<fileset dir="${project.ejb.classes}" excludes="**/*.jar"/>

Finally, we will compile the Java project

   <target name="compileJava" depends="createEJBJar">
      <path id="">
         <fileset dir="${project1.source.lib}" includes="**/*.jar"/>
         <fileset dir="${websphere.lib}" includes="**/*.jar"/>

         <fileset dir="${project.artifacts}" includes="${name.ejb.jar}"/>
      <javac encoding="${compiler.encoding}" destdir="${}" classpath="${}"
             debug="on" deprecation="off" optimize="on" fork="false" verbose="false">
         <src path="${}"/>
         <classpath refid=""/>


Prepare the Output Location

There are times where you need to move other files to the output location prior to creating the EAR file. For example, different property files are used for each environment. In those situation, you can use the copy command to prepare the final output.

   <target name="prepareOutput" depends="PackageJar" >
      <copy todir="${}/${build.env}">
         <fileset dir="${project1.working.sub.dir}/JavaSource/properties/logger/${build.env}">
            <include name=""/>

      <!-- Populate the build version parameters -->

      <copy todir="${}/${build.env}">
         <fileset dir="${project.working.sub.dir}/BuildScripts/">
            <include name="build.version"/>

        <replace file="${}/${deploy.env}/build.version" token="{build.branch}" value="${buildBranch}" />

        <replace file="${}/${deploy.env}/build.version" token="{vcs.revision}" value="${vcs.revision}" />

        <replace file="${}/${deploy.env}/build.version" token="{build.env}" value="${deploy.env}" />

        <replace file="${}/${deploy.env}/build.version" token="{build.version}" value="${buildVersion}" />

        <replace file="${}/${deploy.env}/build.version" token="{build.result.key}" value="${resultKey}" />

        <replace file="${}/${deploy.env}/build.version" token="{bamboo.url}" value="${bambooURL}" />

        <replace file="${}/${deploy.env}/build.version" token="{}" value="${buildTime}" />


Create Application Packages (ejb, war, ear files)

The final step is to create the application package used for deployment. We first create the jar file for the Java project.

   <target name="createJar" depends="compileJava">
      <jar jarfile="${project.artifacts}/${}" manifest="${}/WebContent/META-INF/manifest">
         <fileset dir="${}" excludes="**/*.jar"/>

Next, we create the war file. It references both EJB and Java projects.

   <target name="createWar"  depends="prepareOutput">
      <war destfile="${project.artifacts}/${name.war}" update="true"
         <classes dir="${}/WebContent/WEB-INF/classes" />
         <fileset dir="${}">
            <exclude name="ejb-classes" />
            <exclude name="java-classes" />

In the final step, we create the ear file containing the war file.

<target name="createEar" depends="createWar">
   update="true" appxml="${}/application/application.xml">
   <fileset dir="${project.artifacts}">
    <include name="**/${name.war}"/>

    <fileset dir="${}/${deploy.env}">

               <include name="build.version"/>


  </ear> </target> 

Run the Build Script

To run the build script, right click on build.xml and choose Run As > Ant Build

  • No labels