apache ant a gateway to test-driven java development

24
Apache Ant A gateway to test-driven Java development.

Upload: zoe-cox

Post on 29-Dec-2015

246 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Apache Ant A gateway to test-driven Java development

Apache AntA gateway to test-driven Java

development.

Page 2: 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.

Page 3: Apache Ant A gateway to test-driven Java development

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

Page 4: Apache Ant A gateway to test-driven Java development

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

Page 5: Apache Ant A gateway to test-driven Java development

Ant Project Files

• One ant project per file

• <project/>

• Multiple build targets per project

• <target/>

Page 6: Apache Ant A gateway to test-driven Java development

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”/>

Page 7: Apache Ant A gateway to test-driven Java development

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>

Page 8: Apache Ant A gateway to test-driven Java development

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"/>

Page 9: Apache Ant A gateway to test-driven Java development

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>

Page 10: Apache Ant A gateway to test-driven Java development

Next: installing and using Ant

Page 11: Apache Ant A gateway to test-driven Java development

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

Page 12: Apache Ant A gateway to test-driven Java development

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

Page 13: Apache Ant A gateway to test-driven Java development

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=“”/>

Page 14: Apache Ant A gateway to test-driven Java development

An Example build.xml

<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>

Page 15: Apache Ant A gateway to test-driven Java development

Product.xml: invoking javac

<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>

Page 16: Apache Ant A gateway to test-driven Java development

Next:Ant and Test-Driven

Development

Page 17: Apache Ant A gateway to test-driven Java 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

Page 18: Apache Ant A gateway to test-driven Java development

Extensibility provides 3p tag libraries

• Existing libraries for:– testing– source control – Project deployment

Page 19: Apache Ant A gateway to test-driven Java development

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>

Page 20: Apache Ant A gateway to test-driven Java development

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

Page 21: Apache Ant A gateway to test-driven Java development

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

Page 22: Apache Ant A gateway to test-driven Java development

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

Page 23: Apache Ant A gateway to test-driven Java development

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

Page 24: Apache Ant A gateway to test-driven Java development

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