Apache Ant - University of California, Santa Cruz

Download Report

Transcript Apache Ant - University of California, Santa Cruz

Apache Ant
A gateway to test-driven Java
development.
What is Ant?
• Ant is an Apache Jakarta project
• It’s a build system, intended to replace Make
• It’s written in Java, intended for Java
• Ant project files are XML
–Ant leverages XML document features such as
ELEMENT references
– Ant directives are Tags
• It’s extensible—you can create your own tags.
Alternatives to Ant
• IDE provided projects
– typically closed format
• Make
– has a tricky file format ;-)
– Make can only be extended using shell scripts
or other executables
• Shell scripting
– cannot be extended
• Both are slower to build Java
Some Ant Tags
• Checksum, chmod, concat, copy, delete, filter,
FixCRLF, get, mkdir, move, patch
• Bzip2, cab, Ear, gzip, jar, Rpm, Jlink, SignJar, tar,
war, zip
• Depend, javac, JspC, RmiC, WljspC
• ServerDeploy, Javadoc, EJB
• Exec, Java, Parallel, Sequential, Sleep, Waitfor
• Echo, mail, sql, taskdef, tstamp, ftp, telnet
• Cvs, ClearCase, VSS
• Junit, Testlet
Ant Project Files
•
•
•
•
One ant project per file
<project/>
Multiple build targets per project
<target/>
Projects have Properties
• Properties are immutable
• Properties:
<property name=“” value=“”/>
• Environmental Variable Prefix:
<property environment=“env”/>
• Dereferncing env vars:
<echo message=“Shell path: ${env.PATH}”/>
• Load properties from Java property files:
<property file=“build.properties”/>
Projects use Paths
• Classpaths, File paths and arbituary sets of files using
filters can be described using path tags.
<path id=“beagle”>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<javac>
<classpath refid=“beagle”/>
</javac>
Targets can be conditional
• Targets execute conditionally using the “if”
or “unless” attributes
<target name="build-oracle"
if=“env.BUILD_ORACLE"/>
<target name=“build-psql"
unless=“env.SKIP_PSQL"/>
Advanced Conditionals
<target name="cond" depends="cond-if,cond-else"/>
<target name="check-cond">
<condition property="cond-is-true">
<and><not>
<equals arg1="${prop1}" arg2="$${prop1}"/>
</not>
<not><equals arg1="${prop2}" arg2="$${prop2}"/>
</not>
<equals arg1="${prop3}" arg2="$${prop3}"/>
</and>
</condition>
</target>
<target name="cond-if" depends="check-cond" if="cond-is-true">
<echo message="yes"/>
</target>
<target name="cond-else" depends="check-cond" unless="cond-is-true">
<echo message="no"/>
</target>
Next:
installing and using Ant
Installing and Caring for Ant
•
•
•
•
Download from jakarta.apache.org/ant
Requires Java 2 (?)
Extract to /usr/local/ or…
Manage Ant as 3rd party library
– managed in source control
– prepare for new releases of Ant
• Manage Ant Scripts in source control!
– Prepare to branch your build scripts for new versions of
Ant
Runing Ant
• By default, Ant reads ./build.xml
• define an $ANT_HOME in your .bashrc
• Call indirectly from a shell alias:
– Do alias Build=“$DEV/build.sh” where
– build.sh sets env variables
– Finally calls $ANT_HOME/bin/ant
Ant Project Files
• build.xml is the default “project” file where
you define you build targets
• Define a build target using
<target name=“” depends=“”/>
• Targets can call targets in the same file using
dependency or explictly using
<antcall target=“”/>
• Break out different subprojects into different files
• Call other files using
<ant antfile=“” dir=“” target=“”/>
<project default="usage" basedir=".">
<target name="usage">
<echo
message="Please specify a target. Example: build (core|clean)"/>
</target>
<target name="init">
<property environment="env"/>
<property name="TOOLS_DIR“
value="${env.IMPL_HOME}/admin/tools"/>
<property file=“build.properties”/>
</target>
<target name=“core" depends=“init,clean">
<ant file="product.xml"/>
</target>
<target name="clean">
<delete dir=“${env.IMPL_HOME}/tmp/classes”/>
</target>
</project>
An Example build.xml
<project default="jar_product">
<target name="core">
<javac destdir="${CLASSDIR}"
srcdir="${PRODUCTSOURCEDIR}" includes="**">
<classpath>
<pathelement path="${CLASSDIR}"/>
<fileset dir="${LIBDIR}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="jar_product" depends="core">
<property name="jarfile“
value="${LIBDIR}/${PRODUCT}_core.jar"/>
<jar jarfile="${jarfile}" basedir="${CLASSDIR}"/>
</target>
</project>
Product.xml: invoking javac
Next:
Ant and Test-Driven Development
Extending Ant
• Tasks extend the class
org.apache.tools.ant.Task
• <Taskdef>
– Makes new tag classes visible to Ant
• Support for writing nested tags
– Sophisticated tasks
Extensibility provides 3p tag
libraries
• Existing libraries for:
– testing
– source control
– Project deployment
3p lib: Junit
<junit printsummary="yes"
haltonfailure="yes"
haltonerror="yes" fork="yes">
<classpath>
<pathelement
location="${integration.test.jar}"/>
</classpath>
<test name=“gov.irs.TestApplicantPainThreshold"/>
</junit>
3p lib: Cactus
• Useful for in-container web-application testing
<runservertests
testURL="http://localhost/${PRODUCT}/“
startTarget="start_container“
stopTarget="stop_container“
testTarget="test-classes"/>
• StartTarget – starts container
• StopTarget – container shutdown
• TestTarget – like Junit test block
• HTTPUnit compliments cactus by testing entirely outside the
container
Core lib: CVS tags
<cvs cvsRoot=":pserver:[email protected]:/cvsroot"
package="jakarta-ant" dest="${ws.dir}" />
<cvs command="update -A -d"/>
• Invoke to get latest before a build
• Invoke to tag working builds upon successful test
execution
Test-Driven Development
• Kent Beck, Extreme Programming (XP) and
continuous integration
– Continuous integration involves validating
implementation with automated tests
– Run tests with every build
• Write the tests before coding the feature
– Write tests for multiple levels of the architecture
– Automated deployment follows passed tests
• Ant provides a framework for automating the build,
testing, source control and deployment tasks
Test-Driven Development Tools
• Junit Ant tags
– used for class-level tests
• Cactus library
– Servlet-container testing
• CVS Ant tag
– update source tree upon passing tests
• Cruise Control – integrates these tasks
– Scans your source-control repository periodically
– Starts build upon detecting updates
– test-failure reports posted to status web page
Conclusion
•
•
•
•
•
Improves Java project build times
Supports large and modular projects
Conditional compilation of components
Tag libraries easily extend Ants utility
Automating build, testing and source-control
tasks accelerate the build cycle