july 2011cmsc 341 cvs/ant 1 cmsc 341 java packages ant cvs project submission

26
July 2011 CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

Upload: adelia-nicholson

Post on 02-Jan-2016

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 1

CMSC 341

Java Packages

Ant

CVS

Project Submission

Page 2: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 2

Packages• Java packages create libraries– A group of classes organized together into a

single namespace.– Allows multiple classes with the same name

• MyPackage.Stack• YourPackage.Stack

Page 3: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 3

Packages

• A file may belong to only one package.• Packages serve as a namespace in Java and

create a directory hierarchy when compiled.• Classes are placed in a package using the

following syntax in the first line that is not a comment.package packagename;

package packagename.subpackagename;

Page 4: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 4

Multiple Stack Classes// My stack.java file

package MyPackage;

public class Stack

{

// …

};

// Your stack.java file

package YourPackage;

public class Stack

{

// …

};

Page 5: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 5

Packages (cont.)• The fully qualified name of a class is the packageName.ClassName.

java.lang.StringMyPackage.StackYourPackage.Stack

• To alleviate the burden of using the fully qualified name of a class, use an import statement before the class declaration.

import java.util.StringBuffer;import java.util.*;import MyPackage.Stack;import YourPackage.*;

Page 6: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 6

Packages (cont.)• It is common practice to duplicate the package directory

hierarchy in a directory named src and to compile to a directory named bin.

• Create this directory manually in your GL account

– Eclipse creates this directory structure for you

Project1

proj1

proj1

src

bin

Example.java

Example.class

Package name

Root of project directory tree

Package name

Page 7: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 7

Compiling with packages• When using the Unix command line, classes in a package are

compiled using the –d option.• Given the directory structure on the previous slide, run the

command below from the src directory to compile the code from the Project1/src directory to the Project1/bin directory.

• The -d switch tells the compiler where to place the .class file• The bin directory must exist before compiling. The compiler

will create the proj1 directory under bin.

javac –d ../bin proj1/Example.java

Page 8: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 8

Packages and Eclipse

• By default Eclipse automatically creates a src/ directory when you create a new Java Project

• Eclipse will then automatically create a bin/ directory and associated package directory hierarchy when it compiles your Java classes– Note: the bin/ directory tree is hidden by Eclipse

when in the Java perspective, but you can see it if viewing the file system.

Page 9: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 9

What is Ant?

Ant is a Java based tool for automating the build process Platform independent commands (works on Windows,

Mac & Unix) XML based format Easily extendable using Java classes Ant is an open source (free) Apache project

Ant files used in this course require the package Ant files used in this course require the package directory structure described earlierdirectory structure described earlier

Page 10: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 10

Anatomy of a Build File

Ant’s build files are written in XML Convention is to call file build.xml

Each build file contains A project At least 1 target

Targets are composed of some number of tasks Build files may also contain properties

Like macros in a make file Comments are within <!-- --> blocks

Page 11: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 11

Projects

The project tag is used to define the project to which the ANT file applies

Projects tags typically contain 3 attributes name – a logical name for the project default – the default target to execute basedir – the base directory relative to which all

operations are performed Additionally, a description for the project can

be specified from within the project tag

Page 12: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 12

Project tag<project name="Sample Project" default="compile" basedir=".">

<description> A sample build file for this project Recall that “.” (dot) refers to the current directory </description> </project>

Page 13: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 13

Properties

Build files may contain constants (known as properties) to assign a value to a variable which can then be used throughout the project Makes maintaining large build files more manageable and

easily changeable

Projects can have a set of properties

• Property tags consist of a name/value pair Use the property names throughout the build file The value is substituted for the name when the build file is

“executed”

Page 14: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 14

Build File with Properties

<project name="Sample Project" default="compile" basedir=".">

<description> A sample build file for this project </description> <!-- global properties (constants) for this build file --> <property name="source.dir" location="src"/> <property name="build.dir" location="bin"/> <property name="doc.dir" location="doc"/> </project>

Page 15: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 15

Tasks A task represents an action that needs execution Tasks have a variable number of attributes

which are task dependant There are a number of built-in tasks, most of

which are things which you would typically do as part of a build process mkdir - create a directory javac - compile java source code java - execute a Java .class file javadoc - run the javadoc tool over some files And many, many others…

For a full list see: http://ant.apache.org/manual/tasksoverview.html

Page 16: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 16

Targets The target tag has the following required attribute

name – the logical name for a target

Targets may also have optional attributes such as depends – a list of other target names for which this task is

dependant upon, the specified task(s) get executed first description – a description of what a target does

Targets in Ant can depend on some number of other targets For example, we might have a target to create a jarfile, which first

depends upon another target to compile the code Targets contain a list of tasks to be executed

Page 17: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 17

Build File with Targets<project name="Sample Project" default="compile" basedir="."> <!-- set up some directories used by this project --> <target name="init" description="setup project directories">

<!-- list of tasks to be executed --> </target> <!-- Compile the java code in src dir into build dir --> <target name="compile" depends="init" description="compile java sources">

<!-- list of tasks to be executed --> </target>

<!-- Generate javadocs for current project into docs dir --> <target name="doc" depends="init" description="generate documentation">

<!-- list of tasks to be executed --> </target>

<!-- Execute main in the specified class under ${build.dir} --> <target name=”run" depends=“compile” description=”run the application">

<!-- list of tasks to be executed --> </target>

<!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name="clean" description="tidy up the workspace">

<!-- list of tasks to be executed --> </target></project>

Page 18: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 18

Initialization Target & Tasks

Our initialization target creates the build and documentation directories The mkdir task creates a directory

<project name="Sample Project" default="compile" basedir=".">

...

<!-- set up some directories used by this project --> <target name="init" description="setup project directories">

<mkdir dir="${build.dir}"/> <mkdir dir="${doc.dir}"/> </target> ...

</project>

Page 19: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 19

Compilation Target & Tasks Our compilation target will compile all java files in the source

directory The javac task compiles sources into classes Note the dependence on the init task

<project name="Sample Project" default="compile" basedir=".">

...

<!-- Compile the java code in ${src.dir} into ${build.dir} --> <target name="compile" depends="init" description="compile java sources"> <javac srcdir="${source.dir}" destdir="${build.dir}"/> </target>

...

</project>

Page 20: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 20

Run Target & Tasks Our run target will execute main in the fully specified class

Typically dependent on the compile task

<project name="Sample Project" default="compile" basedir=".">

...

<!-- Execute main in the fully qualified name under ${build.dir} --> <target name=”run" depends=”compile" description=“run the application"> <java directory=“${build.dir}” classname=“${main.class}” fork=“yes”>

<arg line=“${args}” /></java>

</target>

...

</project>

Page 21: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 21

Running Ant – Command Line Move into the directory which contains the build.xml file Type ant followed by the name of a target

unix> ant run

unix> ant compile

Type ant at the unix prompt to run the project’s default target -- see screen shot on next page

unix> ant

Page 22: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 22

Ant screen snapshot

Page 23: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 23

Project Submission

• Projects in this course will be submitted electronically through the use of an industry standard version control system named CVS.

• CVS can be used from the Unix command line, or from an Eclipse perspective.

• In either case, we must be familiar with Java packages and their associated directory structure as discussed earlier.

Page 24: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 24

What is CVS? Concurrent Versioning System (CVS) is one of

the earlier SCM systems which gained wide adoption Open source Easy to install and use Simple command line client Wide integration in a lot of development tools Used for submitting projects in this course

For good introduction on version control and CVS see the following book… Pragmatic Version Control using CVS

Page 25: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 25

CVS Terminology

Repository – the place where resources (files) are stored

Checkout – copy resources from the repository and create a working copy

Checkin/Commit – place resources from your working copy into the repository

Add – place a resource under version control Remove – delete a resource from version control Update – pull down changes from the repository into

your working copy

Page 26: July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission

July 2011 CMSC 341 CVS/Ant 26

Using CVS in this course

• See http://www.cs.umbc.edu/courses/undergraduate/341/fall12/projects/CVS.html

• For additional slides about CVS submission via Eclipse:

http://www.cs.umbc.edu/courses/undergraduate/341/Lectures/CVS/intro-to-cvs.ppt