csc 395 – software engineering

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

Upload: hedley-boone

Post on 02-Jan-2016

11 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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

Page 2: CSC 395 – Software Engineering

In This Lecture

Provide overview of using Apache Ant What does Ant do? How do we use it? Why should I care?

Page 3: CSC 395 – Software Engineering

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

Page 4: CSC 395 – Software Engineering

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>

Page 5: CSC 395 – Software Engineering

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

Page 6: CSC 395 – Software Engineering

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

Page 7: CSC 395 – Software Engineering

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

Page 8: CSC 395 – Software Engineering

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

Page 9: CSC 395 – Software Engineering

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

Page 10: CSC 395 – Software Engineering

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

Page 11: CSC 395 – Software Engineering

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>

Page 12: CSC 395 – Software Engineering

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>

Page 13: CSC 395 – Software Engineering

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

Page 14: CSC 395 – Software Engineering

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

Page 15: CSC 395 – Software Engineering

Helper Target (cont.)

<target name=“echoTwice”><echo message=“${message}” /><echo message=“… and again: ${message}” />

</target>

…<antcall target=“echoTwice”><param name=“message” value=“Hi.” />

</antcall>

Page 16: CSC 395 – Software Engineering

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

Page 17: CSC 395 – Software Engineering

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!

Page 18: CSC 395 – Software Engineering

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

Page 19: CSC 395 – Software Engineering

Build Notification

Send as part of build script <mail> sends e-mail

<mail mailhost=“smtp.canisius.edu” subject=“Build Successful”>

<from address=“[email protected]” />

<to address=“[email protected]” />

<message>Today’s build is done</message>

</mail>

Page 20: CSC 395 – Software Engineering

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

Page 21: CSC 395 – Software Engineering

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

Task Scheduler (Windows)

Page 22: CSC 395 – Software Engineering

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

Page 23: CSC 395 – Software Engineering

For Next Lecture

Read about SCM Change is inevitable SCM looks to control the results of changes that

occur