Build Scripts 
Reading: Ant article
Why Build Scripts? 
§ For large enterprise systems using the IDE is 
not sufficient for building 
§ Complicated Builds 
§ Automatic Nightly Builds 
§ Special Build Machines 
§ Important to be able to separate build and 
development
What is Ant? 
§ ANT is a tool to build programs 
§ “make” tool 
§ Uses XML file to describe the solution 
§ Default file is build.xml 
§ Uses Java classes to run tasks 
§ Built-in tasks 
§ Jakarta project 
§ http://ant.apache.org 
§ Current version 1.8.2 
§ Installation guidelines are on the course web
Ant in use 
Command Prompt 
Verkefni> 
Verkefni 
build.xml 
src/ 
ant 
Buildfile: build.xml 
... 
Project 
classes/ 
dist/ 
ANT file 
Source 
Class files 
JAR file 
Project>ant 
BUILD SUCCESSFUL 
Total time: 4 seconds
Running Ant 
§ Usage 
ant [options] [target [target2 [target3] ...]] 
§ Target can be selected 
§ Default is used if target is not specifed 
§ Examples 
>ant -buildfile test.xml 
>ant -buildfile test.xml dist 
>ant -buildfile test.xml -Dbuild=build/classes dist
Using Ant 
§ Each build file is one project (one output) 
§ Build file is XML file (for example build.xml) 
§ Project has 
§ Name 
§ Default target 
§ Basedir 
§ One or more target 
§ Target 
<project name="MyProject" 
</project> 
§ Target is a collection of tasks 
§ Task depend on 
other tasks 
default="dist" 
basedir="."> 
... 
<target name="A"/> 
<target name="B" depends="A"/> 
<target name="C" depends="B"/> 
<target name="D" depends="C,B,A"/>
Using Ant 
§ Properties 
§ Have names and values 
<property name="dist" value="dist"/> 
§ Referenced with ${property} 
§ Example: 
<target name="dist" depends="compile"> 
<mkdir dir="${dist}/lib"/> 
<jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}"/> </ 
target>
Example build.xml file 
Import Contents Process
Structure 
§ Work space 
§ build.xml 
§ src/java – contains the Java files 
§ src/msg – contains resource bundles 
§ lib/ - contains libraries (jar files) 
§ Ant made directories 
§ classes/ - compiled Java files – class files 
§ dist/ - distributable - jar files 
§ JAR file 
§ dist/ImportContentProcess.jar
build.xml 
<project name="ImportContentProcess" default="jar" basedir="."> 
<property name="src.java" value="src/java"/> 
<property name="src.msg" value="src/msg"/> 
<property name="output.dir" value="classes"/> 
<property name="dist.dir" value="dist"/> 
<property name="lib.dir" value="lib"/> 
<property name="jar.file" value="ImportContentProcess.jar"/> 
<property name="classpath" 
value="${lib.dir}/rome-0.8.jar:${lib.dir}/jdom.jar:$ 
{lib.dir}/spring.jar:${lib.dir}/commons-logging.jar:${lib.dir}/ 
ruframework.jar"/> 
<target name="init" 
description="Prepare by creating output directories"> 
<mkdir dir="${output.dir}"/> 
<mkdir dir="${dist.dir}"/> 
</target>
build.xml 
<target name="compile" depends="init" 
description="Compilation of all source files"> 
<javac srcdir="${src.java}" 
destdir="${output.dir}" 
classpath="${classpath}"/> 
</target> 
<target name="jar" depends="compile" 
description="Create the JAR"> 
<jar jarfile="${dist.dir}/${jar.file}" 
basedir="${output.dir}"> 
<fileset dir="${src.msg}"/> 
</jar> 
</target> 
<target name="clean" description="Delete all generated files"> 
<delete dir="${output.dir}"/> 
<delete dir="${dist.dir}"/> 
</target> 
</project>
Building 
C:hrHonn>ant 
Buildfile: build.xml 
init: 
[mkdir] Created dir: C:hrHonnclasses 
compile: 
[javac] Compiling 11 source files to C:hrHonnclasses 
jar: 
[jar] Building jar: C:hrHonndistImportContentProcess.jar 
BUILD SUCCESSFUL 
Total time: 2 seconds 
C:hrHonn>java -cp libcommons-logging.jar;libjdom.jar;librome-0.8.jar;libruf 
ramework.jar;libspring.jar;distImportContentProcess.jar 
is.ruframework.process.RuProcessRunner
Using Ant with IntelliJ 
§ Most IDEs support ANT 
§ Must use the same 
folders 
§ IntelliJ has ANT support 
§ Easy to use 
§ Ant is powerful when 
using IntelliJ to develop 
container-based 
programs 
§ For example EJBs, Web 
Apps

Build Scripts

  • 1.
  • 2.
    Why Build Scripts? § For large enterprise systems using the IDE is not sufficient for building § Complicated Builds § Automatic Nightly Builds § Special Build Machines § Important to be able to separate build and development
  • 4.
    What is Ant? § ANT is a tool to build programs § “make” tool § Uses XML file to describe the solution § Default file is build.xml § Uses Java classes to run tasks § Built-in tasks § Jakarta project § http://ant.apache.org § Current version 1.8.2 § Installation guidelines are on the course web
  • 5.
    Ant in use Command Prompt Verkefni> Verkefni build.xml src/ ant Buildfile: build.xml ... Project classes/ dist/ ANT file Source Class files JAR file Project>ant BUILD SUCCESSFUL Total time: 4 seconds
  • 6.
    Running Ant §Usage ant [options] [target [target2 [target3] ...]] § Target can be selected § Default is used if target is not specifed § Examples >ant -buildfile test.xml >ant -buildfile test.xml dist >ant -buildfile test.xml -Dbuild=build/classes dist
  • 7.
    Using Ant §Each build file is one project (one output) § Build file is XML file (for example build.xml) § Project has § Name § Default target § Basedir § One or more target § Target <project name="MyProject" </project> § Target is a collection of tasks § Task depend on other tasks default="dist" basedir="."> ... <target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/>
  • 8.
    Using Ant §Properties § Have names and values <property name="dist" value="dist"/> § Referenced with ${property} § Example: <target name="dist" depends="compile"> <mkdir dir="${dist}/lib"/> <jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}"/> </ target>
  • 9.
    Example build.xml file Import Contents Process
  • 10.
    Structure § Workspace § build.xml § src/java – contains the Java files § src/msg – contains resource bundles § lib/ - contains libraries (jar files) § Ant made directories § classes/ - compiled Java files – class files § dist/ - distributable - jar files § JAR file § dist/ImportContentProcess.jar
  • 11.
    build.xml <project name="ImportContentProcess"default="jar" basedir="."> <property name="src.java" value="src/java"/> <property name="src.msg" value="src/msg"/> <property name="output.dir" value="classes"/> <property name="dist.dir" value="dist"/> <property name="lib.dir" value="lib"/> <property name="jar.file" value="ImportContentProcess.jar"/> <property name="classpath" value="${lib.dir}/rome-0.8.jar:${lib.dir}/jdom.jar:$ {lib.dir}/spring.jar:${lib.dir}/commons-logging.jar:${lib.dir}/ ruframework.jar"/> <target name="init" description="Prepare by creating output directories"> <mkdir dir="${output.dir}"/> <mkdir dir="${dist.dir}"/> </target>
  • 12.
    build.xml <target name="compile"depends="init" description="Compilation of all source files"> <javac srcdir="${src.java}" destdir="${output.dir}" classpath="${classpath}"/> </target> <target name="jar" depends="compile" description="Create the JAR"> <jar jarfile="${dist.dir}/${jar.file}" basedir="${output.dir}"> <fileset dir="${src.msg}"/> </jar> </target> <target name="clean" description="Delete all generated files"> <delete dir="${output.dir}"/> <delete dir="${dist.dir}"/> </target> </project>
  • 13.
    Building C:hrHonn>ant Buildfile:build.xml init: [mkdir] Created dir: C:hrHonnclasses compile: [javac] Compiling 11 source files to C:hrHonnclasses jar: [jar] Building jar: C:hrHonndistImportContentProcess.jar BUILD SUCCESSFUL Total time: 2 seconds C:hrHonn>java -cp libcommons-logging.jar;libjdom.jar;librome-0.8.jar;libruf ramework.jar;libspring.jar;distImportContentProcess.jar is.ruframework.process.RuProcessRunner
  • 14.
    Using Ant withIntelliJ § Most IDEs support ANT § Must use the same folders § IntelliJ has ANT support § Easy to use § Ant is powerful when using IntelliJ to develop container-based programs § For example EJBs, Web Apps