Software Development COMP220 Seb Coope Ant: Structured Build These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran. Manning.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Advertisements

Data Dependencies Describes the normal situation that the data that instructions use depend upon the data created by other instructions, or data is stored.
•7/12 /07 F-1 © 2010 T. Horton CS 4240 Principles of SW Design Packages in Java and UML.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
Software Development Tools COMP220/COMP285 Seb Coope Ant and XML: Getting Started These slides are mainly based on “Java Development with Ant” - E. Hatcher.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
ANT: Another Nice Tool Ali Beyad October 1, 2003.
ANT – Another Neat Tool Representation and Management of Data on the Internet.
ANT: Another Nice Tool Ali Beyad October 1, 2003.
MC365 Ant. Today We Will Cover: Overview of Ant Installing Ant Using the Ant command line tool Various Ant commands available Using Ant in Eclipse.
1 Ant – Another Neat Tool Representation and Management of Data on the Internet.
© S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Winter 2005Jason Prideaux1 Apache ANT A platform independent build tool for Java programs.
Using Ant to build J2EE Applications Kumar
Introduction to ant Guy Rixon AstroGrid Consortium Meeting
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Software Development Tools COMP220 Seb Coope Week 8 Lecture 1 Ant, Testing and JUnit (2) These slides are mainly based on “Java Development with Ant” -
Servlets Environment Setup. Agenda:  Setting up Java Development Kit  Setting up Web Server: Tomcat  Setting up CLASSPATH.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
CMSC 341 – Data Structures Spring 2012 University of Maryland, Baltimore County ANT, CVS and CVS Utilities Slides prepared by Prajit Kumar Das – Summarized.
Apache Ant Java-Based Build Tool. Making your builds boring… Building projects should be easy and standardized. You should not be spending a substantial.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.
Interacting with a UNIX computer: Navigating through the directory tree.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission.
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
Ant Presentation by: Bart Taylor. What is Ant? The simple definition: A Java-based build tool The Official Definition: “Apache Ant is a Java-based build.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Chapter 1 : The Linux System Part 2 Lecture 2 11/14/
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 3 Understanding Ant datatypes and properties.
Overview of the Automated Build & Deployment Process Johnita Beasley Tuesday, April 29, 2008.
Software Development COMP220/COMP285 Seb Coope Introducing Ant These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran.
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Chapter 1 Introducing Ant. What is ant? Ant is a build tool  Automate the tasks of compiling code, running test, and packaging the results for redistribution.
Henrik Bærbak Christensen1 Build Management Clean environment that works...
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Building Packages BCIS 3680 Enterprise Programming.
GT3 Tutorial Chapter 3 and Chapter 4 Lecture for Cluster and Grid Computing, CSCE 490/590 Fall 2004, University of Arkansas, Dr. Amy Apon
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Java Package Advantage of Java Package
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
1 Development Environments AUBG, COS dept Lecture Title: Dev Env: NetBeans (Extract from Syllabus) Reference:
Using Ant in Eclipse Dwight Deugo Nesa Matic
Software Development Tools COMP220 Seb Coope Ant: Datatypes and Properties These slides are mainly based on “Java Development with Ant” - E. Hatcher &
Software Development Tools COMP220 Seb Coope More Ant Features These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran.
SG Introduction to ANT scmGalaxy Author: Rajesh Kumar
Software Development Tools COMP220 Seb Coope Ant: Datatypes and Properties These slides are mainly based on “Java Development with Ant” - E. Hatcher &
Software Development Tools COMP220 Seb Coope Eclipse and Ant These slides are mainly based on “Java Development with Eclipse” – D.Gallardo et al., Manning.
Software Development Tools
Unit Testing.
COMP220/COMP285 Seb Coope Ant: Structured Build
Instructor: Prasun Dewan (FB 150,
Packages and Interfaces
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Nilanjan Banerjee Java Packages Ant CVS Project Submission
CS 240 – Advanced Programming Concepts
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Presentation transcript:

Software Development COMP220 Seb Coope Ant: Structured Build These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran. Manning Publications, 2003

2 Imposing Structure In the build process discussed in the previous lecture and considered in a Lab sessions: source files, output files, and build file were in the same directory. These were files Main.java, Main.class and build.xml in C:\Antbook\ch02\firstbuild (In Labs you will use H: instead of C: )

3 Imposing Structure In a bigger project, things could get out of hand. We want to automate the cleanup in Ant. If done wrong, this could accidentally delete source files. Thus, let us separate source and generated files into different directories.

4 Imposing Structure We also want to place Java source file into a Java package. We want to create a JAR file containing the compiled code. We should be able to clean up the directories with compiled files and this JAR file before starting the next build. Hence, use (de facto) standard directory names as in the next table:

5 STANDARD DIRECTORY NAMES Directory name Function src source files build\classes (or bin – a default in Eclipse) intermediate output (generated; cleanable) dist distributable files (generated; cleanable) Let these directories be sub-directories of a new base directory: C:\Antbook\ch02\secondbuild

STANDARD DIRECTORY NAMES Thus, we will have the following directory structure: C:\Antbook\ch02\secondbuild\src \build\classes \dist which will be further extended. Please relate all the following considerations and your Lab exercises exactly with the directory structure suggested in these slides (except your personal directory structure under src related with your personal packages – to be discussed below). 6

7 Laying out the source directories and source files When working on a project it makes sense to add some (your personal) package declaration to our java classes such as Main.java considered in the previous lecture (and a Lab): package org.example.antbook.lesson1; //package declaration public class Main { public static void main(String args[]) { for(int i=0;i<args.length;i++) { System.out.println(args[i]); } } } In this case we should also put so modified Main.java into the sub-…-sub-directory of src : src\org\example\antbook\lesson1 corresponding to (or matching) the above package name.

8 Laying out the source directories and source files Recall that Java packages and “contained” in these packages Java classes are always organized in such a way: – package names of Java classes exactly match directory system where to find them (also in JAR files). That is, we use traditional agreement that any Java class declaring its package package aaa.bbb.ccc; MUST be contained in a corresponding sub-sub-…- directory...\aaa\bbb\ccc

9 Laying out the source directories and source files Recall also that packages give an appropriate level of access to their classes and methods. It is quite reasonable that related classes are in the same package (i.e. have the same package declaration). Packages also allow to use “qualified” class and method names specific to these packages like org.example.antbook.lesson1.Main - without any conflict even with possibly identical names in some other projects within other packages.

10 Compilation from command line TRY the following: Compile our renewed java class with the full name C:\Antbook\ch02\secondbuild\src\ org\example\antbook\lesson1\Main.java by the command C:\Antbook\ch02\secondbuild\src>javac -d..\build\classes org\example\antbook\lesson1\Main.java  javac option -d..\build\classes specifies directory (full or relative name) where to place generated class files.  org\example\antbook\lesson1\Main.java is source file to be compiled:  this should be either full name or a name relative to the current directory C:\Antbook\ch02\secondbuild\src.

11 Compilation from command line Then the resulting compiled Main.class will have the corresponding full path: C:\Antbook\ch02\secondbuild\build\classes\ org\example\antbook\lesson1\Main.class provided that the subdirectories build\classes exist. Check that existence of these directories is really necessary!

12 Compilation from command line Summarise that, the general form of compile command is: javac –d [where to compile] [what to compile] Package declaration aaa.bbb.ccc of the compiled class shows the path aaa\bbb\ccc for the compiled class relatively to [where to compile] WHAT TO CHANGE above in the command javac -d..\build\classes org\example\antbook\lesson1\Main.java if you compile from secondbuild instead of src ??

13 Compilation from command line TRY the same command again, but with commented package declaration in Main.java: // package org.example.antbook.lesson1; package org.example2.antbook.lesson1; In which directory the compiled Main.class will appear? (Use time stamps to identify.) TRY the same command again with a different package declaration in the class Main.java Where now the compiled class Main.class will appear? (Use time stamps to identify.) RECOVER the original package name!!!

Summary on compiling with package declarations in source files The general form of compile command is: javac –d [directory where to compile] [what to compile] Package declaration in a.java file says where to put the resulting compiled class relatively to a directory ( -d [directory] ). When the Java compiler compiles the files, it always places the output files in a directory path that matches the package declaration. 14

task, laying out directories and dependency checking Recall that in Ant build file: task means compiling. The next time task runs, it does dependency checking : looks at the directories tree of generated class files and compares them to the source files - whether they are up-to date and should be recompiled. When doing dependency checking, it relies on matching the source tree to the destination tree. 15

task, laying out directories and dependency checking For Java source dependency checking to work, you MUST lay out source Java files in a directory tree that matches the package declarations in the source files. When the source file has no package declaration (the empty package) you must place this file in the base of the source tree (typically, the directory src ). If Ant keeps on unnecessary recompiling your Java files every time you do a build, it is probably because you have not placed them correctly in the package hierarchy. Unnecessary recompiling, even if done automatically, is time consuming! That is, if the package name aaa.bbb.ccc of the source File.java does not match with its directory path, e.g.,...\aaa\bbbbb\ccc\File.java (by your mistake) then the directory path leading to...\aaa\bbb\ccc\File.class will be different from that of the source and will not be able to compare time stamps of these files. 16

17 Comments on laying out directories It may seem inconvenient to rearrange your files into a system of subdirectories. But on a large project, such a layout is critical to separating and organizing classes. Modern Integrated Development Environments (IDE) (such as Eclipse) also prefer and supports using this layout structure, as does the underlying Java compiler javac (as we have already seen). Recall again that correctly laid out directories also serve for dependency checking of which consists in comparing the timestamps of the source and destination files. In big projects this saves time!

18 Adding dependency checks Besides dependency check by task, there is an additional task to do more advanced dependency checking. The dependency logic (to insure that out of date classes are not recompiled during incremental builds) implements a rudimentary check that only passes.java files to the compiler if the corresponding.class file is older or nonexistent. It does not rebuild classes when the files that they depend upon change, such as a parent class or an imported class. The latter problem is resolved by the task

19 Laying out the build and dist directories Imagine that you have a huge project where we should create many intermediate files, as well as delivered or deployed files Taking into account our previous discussion, the directories for a project may look like in the following slide.

base Main.Java antbook Main.class dist project.jar build classes org example build.xml lesson1 src org example antbook lesson1 A source tree (branch) is separated from the build and distribution output. All the shaded directories and files are created by Ant during the build automatically! Note, that in our case base directory is secondbuild Red colour and darker shade correspond to the package name 20 Laying out the build and dist directories

21 The directory layout Assume putting all intermediate files into the build directory tree. Recall that Java compiler lays out compiled *.class files into a (darker shaded above) directory structure that matches the package declarations in the source files. The compiler will create the appropriate directories under build/lasses subdirectory automatically, so we do not need to create them manually and therefore to bother too much about this. After deciding on package name and creating corresponding directories under src, we need to prescribe in the build file (only once) that the following directories be generated: -the top level build directory, and the classes subdirectory, -as well as the dist directly for deployed (JAR, Zip, tar, WAR, etc.) archive files.

22 The directory layout Note that the dist directories are usually much simpler than the intermediate file directories under build. All these (shaded above) files and directories will be created automatically (and even can be automatically deleted before any new build) by Ant. So, we are not worrying about them. We only should write in the build file:

23 Create the build file structured.xml <javac srcdir="src" destdir="build/classes" includeAntRuntime="no"/> Creates the output directories Compiles into the output directories C:\Antbook\ch02\secondbuild\structured.xml (continues)

24 Creating the build file structured.xml <jar destfile="dist/project.jar" basedir="build/classes" /> Creates the Java archive from all compiled classes Cleans the output directories (when invoked) (continued)

25 Creating the build file structured.xml (continued) The meaning of all these targets is evident. Let us note only that: - the Ant task compiles all Java source from src directory and all its subdirectories. - the Ant task creates JAR file containing all files in and below the build/classes directory, which in this case means: all *.class files created by compile target.

26 Target dependencies in structured.xml clean init compile archive Here clean depends upon init ; archive depends on compile directly and on init indirectly (via compile ). The target archive is declared as default in the build file structured.xml

27 Running the new build file Recall that the command ant runs, by default, the build file named as build.xml In the case of a different build file, like our structured.xml, we should use the following form of the command: ant -f structured.xml

Running the new build file Since the target archive is default one in structured.xml, the command ant -f structured.xml will run only the chain of targets init -> compile -> archive structured.xml calls archive which calls compile which calls init In this run clean target will not be executed since it will not be called. Now, DELETE build and dist directories to start on a clean space, and RUN the above command. 28

29 C:\Antbook\ch02\secondbuild>ant -f structured.xml Buildfile: C:\Antbook\ch02\secondbuild\structured.xml init: [mkdir] Created dir: C:\Antbook\ch02\secondbuild\build\classes [mkdir] Created dir: C:\Antbook\ch02\secondbuild\dist compile: [javac] Compiling 1 source file to C:\Antbook\ch02\secondbuild\build\classes archive: [jar] Building jar: C:\Antbook\ch02\secondbuild\dist\project.jar BUILD SUCCESSFUL Total time: 3 seconds Running the build

30 Rerunning the build again C:\Antbook\ch02\secondbuild>ant -f structured.xml Buildfile: C:\Antbook\ch02\secondbuild\structured.xml init: compile: archive: BUILD SUCCESSFUL Total time: 1 second Why no real action, but BUILD SUCCESSFUL ?

31 Rerunning the build again None of the tasks,, say that they are doing any real work. All of these tasks check their dependencies: does not create directories that already exist; compares source and class file timestamps:  if up to date – do actually nothing;

32 Rerunning the build again compares the time of all files to be added to the archive with the time of the.jar file itself. If the resulting files are up to date, these tasks, although invoked, do actually nothing. TRY the same in -verbose or –v mode to see the similar comments from Ant.

33 Clean it! Finally, TRY the command ant -f structured.xml clean which deletes build and dist directories. Hence, you can start build process again on a clean place (after changing something in your source files under src ).

34 What if…? What if our subdirectories under src were laid out wrongly, not according to the package declaration in Main.java ? TRY to change the package declaration in our source file src\org\example\antbook\lesson1\Main.java to and RUN repeatedly the command ant -f structured.xml Check that Ant really keeps on unnecessary recompiling Main.java every time you do a build because you have not placed them correctly in the package hierarchy. RECOVER the original package name!!! package org.example2.antbook.lesson1;

Multiple targets on the command line The command with multiple targets as arguments ant -f structured.xml compile archive is equivalent to running Ant twice: ant -f structured.xml compile ant -f structured.xml archive The resulting sequence of targets will be: init -> compile, and then init -> compile -> archive Thus, for multiple targets called, repetitions of targets are possible in the resulting sequence!! TRY it! 35

Multiple dependencies in build file When a target lists multiple dependencies then Ant executes them in the order listed : It first calls archive and then clean. Note that archive will also call its dependencies, that is, init -> compile -> archive will be executed. Then clean will be called, but now init will not be repeated: init -> compile -> archive -> clean -> all When Ant build file runs in itself, i.e. one or no targets is called from the command line, then targets are not repeated TRY to check this by adding the above target all to structured.xml; use the command ant -f structured.xml all 36

37 Running Java Program from inside Ant We now have a structured build process that compiles Java files and creates the JAR file from the Java compiled classes. The next question is: How to run a Java program with Ant?

38 First, Executing from Command Line To execute our program Main.class we should first compile Main.java. (See Main.java on Slide 7) Then we could just call our program Main.class as usually from the command line (or on console) by stating the classpath (showing where to find Main.class), the qualified class name (using the package name) and the arguments “ a “, “ b “, and “. “ : C:\Antbook\ch02\secondbuild>java -cp build\classes org.example.antbook.lesson1.Main a b. a b. This program Main.class just types the argument values. Three inputs Three identical outputs

39 Why execute from inside Ant? Running this program from the build file provides some benefits in comparison with command line : no need to split program compilation from execution a target to run depends upon the compilation target, so we know we always - run the latest version of the code easy to pass complex arguments to the program easier to set up the classpath the program can run inside Ant’s own JVM: - it loads faster

40 Adding an execute target <java classname="org.example.antbook.lesson1.Main" classpath="build/classes" > Extend the previous build file structured.xml to new file execute.xml by adding target task executes the program Main.class with the arguments specified. See below on the difference between value and file attributes of

41 tags adds a command-line argument somevalue. The action of this task is evident (with the value attribute). The last argument is of another kind: It tells Ant to resolve the file attribute "." (meaning “this directory”) to an absolute build file location (more precisely, to an absolute base directory location) and consider this location as an argument value before calling the program. The latter differs from the ordinary used implicitly in the above command line running (Slide 38).

42 Running task in the target C:\Antbook\ch02\secondbuild>ant -f execute.xml execute Buildfile: C:\Antbook\ch02\secondbuild\execute.xml init: compile: execute: [java] a [java] b [java] C:\Antbook\ch02\secondbuild BUILD SUCCESSFUL Total time: 1 second TRY it! Try it also with

43 For the Lab: Getting information about the project -projecthelp lists the main and other targets in a project build file. C:\Antbook\ch02\secondbuild>ant -projecthelp –f execute.xml Buildfile: C:\Antbook\ch02\secondbuild\execute.xml Main targets: Other targets: archive clean compile execute init Default target: archive Here Ant lists no main targets because main targets are those which contain the optional description attribute, as these are the targets intended for public consumption.

44 For the Lab: Getting information about the project The above example is not very informative, which is our fault for not documenting the file. Add a description attribute to each target of execute.xml, such as description= "Compiles the source code" for the compile target. Add also a element right under the opening tag. Look at the resulting build.xml file (downloadable from corresponding Lab Web page).

45 For the Lab: Getting information about the project Note, that build.xml differs from execute.xml only 1. by such descriptions 2. by declaring execute as a default target, and 3. by changing the project name with secondbuild PUT this build.xml in C:\Antbook\ch02\secondbuild directory. TRY the command ant -projecthelp (calling by default build.xml ). Compare the result with the previous command (from the previous slide) ant -projecthelp -f execute.xml :

46 C:\Antbook\ch02\secondbuild>ant -projecthelp Buildfile: C:\Antbook\ch02\secondbuild\build.xml Compiles and runs a simple program Main targets: archive Creates the JAR file clean Removes the temporary directories used compile Compiles the source code execute runs the program Default target: execute "Described" targets are listed as "Main targets“ now. Other "sub targets" are hidden from view. Use –verbose (or –v ) to see these Other targets as well For the Lab: Getting information about the project