1 / 23

CSC 395 – Software Engineering

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

Download Presentation

CSC 395 – Software Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 395 –Software Engineering Lecture 24: Apache Ant –or– Programming the results of programming Based upon a talk by Anthony Wat

  2. In This Lecture • Provide overview of using Apache Ant • What does Ant do? • How do we use it? • Why should I care?

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

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

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

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

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

  8. 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}”

  9. 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 &lt;) • Properties like final variable in Java • Once defined, cannot easily modify values

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

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

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

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

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

  15. Helper Target (cont.) <target name=“echoTwice”><echo message=“${message}” /><echo message=“… and again: ${message}” /> </target> … <antcall target=“echoTwice”><param name=“message” value=“Hi.” /> </antcall>

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

  17. 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!

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

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

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

  21. 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)

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

  23. For Next Lecture • Read about SCM • Change is inevitable • SCM looks to control the results of changes that occur

More Related