230 likes | 307 Views
CSC 395 – Software Engineering. Lecture 24: Apache Ant –or– Programming the results of programming. Based upon a talk by Anthony Wat. In This Lecture. Provide overview of using Apache Ant What does Ant do? How do we use it? Why should I care?. Ant in a Nutshell. Java-based build tool
E N D
CSC 395 –Software Engineering Lecture 24: Apache Ant –or– Programming the results of programming Based upon a talk by Anthony Wat
In This Lecture • Provide overview of using Apache Ant • What does Ant do? • How do we use it? • Why should I care?
Ant in a Nutshell • Java-based build tool • Uses scripts to automate large processes • Enables multiplatform actions • Useful aspect of Java-based implementation • Scripts must be written to take advantage • Many built-in tasks available • Javac, Copy, Delete, etc. • Can write & import additional tasks
Sample Build Script <?xml version="1.0"?> <project name=“build” default=“all”> <target name=“all” depends=“hello”> <echo message=“Good bye world.” /> </target> <target name=“hello”> <echo message=“Hello world.” /> </target> </project>
Properties • Name-value pair used like variables in scripts • Many ways of defining/importing • Some properties automatically available • basedir • ant.file • ant.version • ant.project.name • ant.java.version
Define In External File • File written in plain text (usually XYZ.properties) • Specify in file using name=value • Example:bin.dir=${basedir}/binHelloWorldStr=Hello world. • Load file using <property> task: • Example:<project name=“build” default=“all”> <property file=“build.properties” />
Define Properties Other Ways • Use name & value attributes of <property> • Example:<project name=“build” default=“all”> <property name=“HelloWorldStr” value=“Hello world.” /> • Specify in command that calls Ant • Example:ant -Ddate=050206
Using Properties • Syntax similar to UNIX environment variables • Examples: <echo message=“${HelloWorldStr}” /> <delete dir=“${bin.dir}” quiet=“yes” /> • First line could do 1 of 2 things • If HelloWorldStr defined, then ${HelloWorldStr} replaced by its value • Otherwise, prints literal value “${HelloWorldStr}”
Caution! • Characters must escaped in properties files • Must escape ‘\’ in Windows-based directories: C:\tmp is bad (\t replaced to become “C: mp”) C:\\tmp is good • Do NOT escape ‘\’ inside build script, but instead escape XML characters (e.g. ‘<’ becomes <) • Properties like final variable in Java • Once defined, cannot easily modify values
Filesets • A fundamental structures in Ant • Define rules including and/or excluding files: • Include only .java files in build directory • Delete backup files generated by a program • Copy only files specified in a list
Using Filesets in Ant • Use <fileset> to delete .bak files:<delete quiet=“yes”> <fileset dir=“doc” includes=“**/*.bak” /> </delete> or <delete quiet=“yes”> <fileset dir=“doc”> <include name=“**/*.bak” /> </fileset> </delete> • Can often nest multiple <filesets>
Implicit Filesets • Some tasks define implicit filesets • Include <javac>, <tar>, <zip> • Treat task like genuine <fileset> • To compile Ant’s Java files:<javac srcdir=“src” destdir=“bin”> <include name=“org/apache/ant/**/*.java” /></javac>
Chaining Scripts • Usually not necessary • <ant> task calls another script • Example:<ant antfile=“other.xml” /> • Runs Ant script named other.xml • Called script inherits all defined properties • New script can also define new properties • Enables combining individual projects into single larger project
Helper Target • Helper targets used like a method • Can be called using different parameters • <antcall> task used to call targets • Specify parameters using <param> • <param> can also override global properties • Initially set a default message globally • Use <param> to override message when needed
Helper Target (cont.) <target name=“echoTwice”><echo message=“${message}” /><echo message=“… and again: ${message}” /> </target> … <antcall target=“echoTwice”><param name=“message” value=“Hi.” /> </antcall>
Building With Ant • Run single script to build end-to-end • Can also make daily builds • Schedule daily builds to run automatically • Send e-mails when build available • Send e-mails whenever submission breaks build
One-Step Build Process • Ant provides a lot of useful tasks: • <cvs> check out and update repository • <copy> & <delete> rearrange files • <javac> & <java> compile and run Java programs • <exec> executes program from command-line • But this will be specific to the system and OS • <zip> & <jar> helps package software • Also provides ability to write your own tasks!
One-Step Build Process (cont.) • Should be enough for end-to-end build • Can fit everything into build.xml script • Default name for Ant script • Otherwise must use -buildfile parameter to run script • Start build by running Ant • Uses ant or ant.bat in ant\bin directory
Build Notification • Send as part of build script • <mail>sends e-mail <mail mailhost=“smtp.canisius.edu” subject=“Build Successful”> <from address=“me@canisius.edu” /> <to address=“developers@canisius.edu” /> <message>Today’s build is done</message> </mail>
Build Notification – Mail Logger • Another way is to use the mail logger • org.apache.tools.ant.listener.MailLogger • Call Ant using the -logger option to specify the logger • Example: • ant -logger org.apache.tools.ant.listener.MailLogger –DMailLogger.properties.file=mail.properties • All of the required properties for the mail logger (SMTP, addresses, etc.) are defined in mail.properties
Build Notification – Mail Logger (cont.) • Instead of piping output to console, it will be cached and sent as e-mail when the build finishes successfully or fails • The logs can be sent to developers • Schedule Ant builds with cron (UNIX) orTask Scheduler (Windows)
Summary • We’ve looked at: • Properties and filesets • Multiple build scripts and helper targets, which provide better maintainability of build scripts • How to achieve one-step automated build process using Ant • Ant is used everywhere – from open source projects to commercial software projects • A good skill to have
For Next Lecture • Read about SCM • Change is inevitable • SCM looks to control the results of changes that occur