Академический Документы
Профессиональный Документы
Культура Документы
To deploy Project
Version 1.2
Grandy.Su
Brief Contents
1.Introduction.......................................................................................................................................3
1.1 What is Ant................................................................................................................................3
1.2 What is a build process and why do you need one?..................................................................3
1.3 The Core Concepts Of Ant.........................................................................................................3
SUMMARY.....................................................................................................................................4
2.Packaging projects.............................................................................................................................4
2.1 Moving,Coping,Deleting files...................................................................................................5
2.1.1 How to delete files.............................................................................................................5
2.1.2 How to copy files...............................................................................................................6
2.1.3 How to move files..............................................................................................................6
2.2 Filtering......................................................................................................................................6
2.3 JAR files....................................................................................................................................7
2.3.1 Creating a JAR file.............................................................................................................7
2.4 CREATING ARCHIVE FILES.................................................................................................7
3. Some exemples.................................................................................................................................8
3 .1 Example one.............................................................................................................................8
3 .2 Anther example .......................................................................................................................9
Summary:..................................................................................................................................11
3. 3 An example project.................................................................................................................12
1.Introduction
This is a document about Ant. But much more than a reference document for Ant syntax,it is a
collection of best practices demonstrating how to use Ant to its greatest potential in real-world
situations. If used well, you can develop and deliver your software projects better than you have done
before.We begin by exploring what Ant is, its history, and its core concepts. Ant is not the only build
tool available, so we will also compare it to the alternatives and explain how Ant can fit in to whatever
formal or informal development methodologies you may encounter. Finally, we’ll introduce the sample
application we developed for this book that demonstrates much of Ant’s capabilities.
•Ants find the shortest distance around obstacles (“Behavior of Real Ants”).
•Ants can carry 50 times their own weight.
•Ants work around the clock; they do rest, but they work in shifts (Ant Colony FAQs).
Ant is a Java-based build tool designed to be cross-platform, easy to use, extensible,and scalable. It can
be used in a small personal project, or it can be used in a large,multiteam software project.
XML format
Declarative syntax
A build file contains one project
Each project contains multiple targets
Targets can have dependencies on other targets
Targets contain tasks
New tasks can easily be added in Java
SUMMARY
To summarize Ant briefly, it is a Java-based tool that can build, test, and deploy Java projects ranging in
size from the very small to the very large. Ant uses X M L build files to describe what to build. Each file
covers one Ant project; a project is divided into targets; targets contain tasks. These tasks are the Java
classes that actually perform the construction work. Targets can depend on other targets. Ant orders the
execution so they execute in the correct order. Unlike Make, which is driven by file dependencies and
derivation rules, Ant lets the tasks worry about file dependencies; the build file author merely lists the
sequence of steps.
2.Packaging projects
So far in this book we have created a build process that now compiles, tests, and executes the Java
programs being developed in our software project. It is now time to start thinking about packaging the
software for distribution and delivery to its destination.This does not mean the software is ready for
release yet, just that the software is ready to deploy to local client and server test systems. The same
targets used for the development phase work are used for the final release process, so the build process
will not only generate packages for testing, it will verify that the packaging process itself is working
correctly.
The steps that a team needs to cover when preparing a release usually include:
1 Writing the documentation
2 Writing any platform-specific bootstrap scripts, batch files, or programs
3 Writing any installer scripts, using installation tools
4 Checking all the source, documentation, and sundries into the source repository
5 Labeling the source in the source code repository
6 Running a clean build directly off the source repository image
7 Running the complete test suite
8 Packaging the software in a form suitable for distribution and installation
For early internal package builds, you can omit the documentation if it is incomplete.
Even internal builds will benefit from a change log and a build version number, so start adding
documentation like this as early as possible.
The steps in the production process that Ant can handle are shown in figure 1.1;
the Java source, data files, documentation, and shell scripts all need to be taken and
transformed into Zip and tar files containing the software packages for execution and the documentation
to accompany them.
Figure 1.1
The packaging process for a Zip or tar file of a JAR library consists of getting the source and data files
into the JAR, the manual and autogenerated documentation into a directory, then creating different final
packages fordownloading to different platforms. We will create Ant targets to mimic these dependencies.
Here, as well as providing a pattern to delete, we have told the task to ignore the
default exclusion patterns. We introduced these patterns in section 3.4.2. Usually,
automatically omitting editor- and SC M-generated backup files is useful, but when trying to delete such
files you need to turn this filtering off. Setting the defaultexcludes attribute to false has this effect.
We should warn that the <delete dir> option is unforgiving, as it can silently delete everything in
the specified directory and those below it. If you have accidentally set the directory attribute to the
current directory (dir="."), then the entire project will be destroyed. This will happen regardless of
any settings in nested filesets. Setting the directory to root, (dir="/"), would be even more destructive.
You can also give the complete destination file name, which renames the file during the copy:
<copy file="readme.txt" tofile="doc/README"/>
The task performs bulk copies when you specify a fileset inside the copy task; you must also specify the
destination directory with the todir attribute and omit the tofile attribute:
<copy todir="${dist.bin.dir}">
<fileset dir="src/scripts" >
<include name="**/*.*"/>
</fileset>
</copy>
Be aware that <copy> is timestamp-aware by default; sometimes that can catch you out. One of the
authors used Ant to install a web application off a CD onto a server, but one system wouldn’t upgrade
because the CD file was older than the dates of the file installed on the server. A build file that had
worked for months suddenly broke.
The solution to such a problem is to set overwrite="true", which tells Ant to overwrite the file
regardless of timestamp differences.
Another point to note is <copy> gives the file a timestamp of the current time.You can request that the
date of the original file is propagated to the new file, by setting preservelastmodified="true".
This may be useful, even though we have not used it ourselves.
If you want to change the names of files when copying or moving them, or change the directory layout
as you do so, you can specify a <mapper> as a nested element of the task. We introduced mappers in
chapter 3; packaging is one of the times where
you may want to make use of them.
2.2 Filtering
We introduced Ant’s filtering feature in section 3.8. Both the <move> and <copy> tasks can be set up
to act as token filters for files. When filtering, the tasks replace tokens in the file with absolute values.
This is sometimes useful in documentation; you can enter timestamps and URLs into the pages. You do
this by nesting a <filterset> element inside the <copy> task. For example, we can set a property to
current time. Then, when Ant copies our text file, the <filterset> instructs it to replace all
references to the token TIMESTAMP with the property:
<tstamp>
<format property="timestamp.isoformat"
pattern="yyyy-mm-dd'T'HH:mm:ss" locale="en"/>
</tstamp>
<copy file="${readme.file}"
tofile="${doc.dir}/readme.txt">
<filterset>
<filter token="TIMESTAMP" value="${timestamp.isoformat}"/>
</filterset>
</copy>
The task will automatically create a manifest file inside the archive, unless one is explicitly provided.
The compress attribute controls whether or not the archive is compressed. By default
compress="true", but for loading speed an uncompressed archive may be faster to load. We will opt
to compress all our files as the benefits for storage and downloads can be significant.
One good practice is to create the archive filename from a project name and a predefined
version number, with some property definitions ahead of the <jar> task to build a customized short
filename, and the full path to the soon-to-be-created file.
<property name="project.name" value="${ant.project.name}"/>
<property name="project.version" value="1.1"/>
<property name="jarfile.name"
value="${project.name}-${project.version}.jar" />
<property name="jarfile.path"
location="${dist.dir}/${jarfile.name}"/>
These declarations should all go at the top of the project. Remember that Ant automatically
defines the property ant.project.name from the <project> declaration in the build file; we
reassign this to a new property to give people (and their property files) the opportunity to pick a
different name. The targets to create output file names, and the target to generate the archive, are both
now highly reusable, provided you use a consistent naming scheme across projects. To create the JAR
file, simply use the property to specify its name:
<target name="dist" depends="compile" description="make the distribution" >
<jar destfile="${jarfile.path}"index="true">
<fileset dir="${build.dir}/classes"/>
</jar>
</target>
3. Some exemples
3 .1 Example one
Now that we have the files in the right places, and we know what we want to do, the build file needs to
be rewritten. Rather than glue all the tasks together in one long list of actions, we have broken the
separate stages —directory creation, compilation,packaging, and cleanup —into four separate targets
inside the build file.
This build file adds an init target to do initialization work, which means creating directories. We’ve
also added two other new targets, clean and archive. The archive target uses the <jar> task to
create the JAR file containing all files in and below the build/classes directory, which in this case means
all .class files created by the compile target. One target has a dependency upon another, a dependency
that Ant needs to know about. The clean target cleans up the output directories by deleting them. It
uses the <delete> task to do this. We have also changed the default target to archive, so this will be
the target that Ant executes when you run it.
3 .2 Anther example
<?xml version="1.0" encoding="UTF-8" ?>
<project name="WSTAM" default="deploy" basedir="." >
<description>Use ant to deploy WSTAM</description>
<!-- define some properties which are needed when deploying -->
<!-- the property of web container,you need to change them according to your JBOSS installing path -->
<property name="jboss.home" value="D:/java program/jboss-4.0.3"></property>
<property name="webapps.home" value="D:/java program/jboss-4.0.3/server/default/deploy"></property>
<!-- define property of application home,application name and javadoc package. -->
<!-- application name is WSTAM-->
<property name="app.home" value="."></property>
<property name="app.name" value="WSTAM"></property>
<property name="javadoc.pkg" value="help"></property>
<!-- Define a deploy folder ,and you can put the WAR file to this folder ,When you need to deploy this project,
You can copy this war file to the home of webapps}-->
<property name="deploy.home" value="${app.home}/deploy"></property>
<!-- Configure class path which are needed when compiling ,it should conclude all the jar package and zip
package under directory of lib .Besides,it should also contains jar package under the directory of Jboss lib -->
<path id="compile.classpath">
< pathelement location="${classes.home}" />
< fileset dir="${lib.home}">
<include name="*.jar" />
<include name="*.zip" />
</ fileset>
<fileset dir="${jboss.home}/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${jboss.home}/classes"/>
</path>
<delete dir="${doc.home}"></delete>
</target>
<!--Create a new release of the path,It depend on the target of “clean_all”-->
<target name="prepare" depends="clean_all">
<echo message="JBOSS Home=${jboss.home}"></echo>
<echo message="webapps Home=${webapps.home}"></echo>
<mkdir dir="${classes.home}"/>
<mkdir dir="${deploy.home}"/>
<mkdir dir="${doc.home}"/>
<mkdir dir="${doc.home}/api"/>
<mkdir dir="${build.home}"/>
<mkdir dir="${build.home}/WEB-INF"/>
<mkdir dir="${build.home}/WEB-INF/classes"/>
<mkdir dir="${build.home}/WEB-INF/lib"/>
</target>
<!-- Compile all java files under the project directory 【$ (src.home)】. Put these compiled class files to
classes home(【$ (classes.home)】) .It needs the support of class library when compling-->
<target name="compile" depends="prepare">
<javac srcdir="${src.home}" destdir="${classes.home}" debug="yes">
<classpath refid="compile.classpath"></classpath>
</javac>
</target>
<!-- To build a good structure of web application ,This target is to copy the project files to the corresponding
building directory -->
<target name="build" depends="compile">
<copy todir="${build.home}">
<fileset dir="${web.home}"></fileset>
</copy>
<copy todir="${build.classes}">
<fileset dir="${classes.home}"></fileset>
</copy>
<!--copy all the resources document to the home of building classes If there are resource files which suffix
names are different,import them using "include".-->
<copy todir="${build.classes}">
<fileset dir="${src.home}">
<include name="**/*.xml" />
</fileset>
</copy>
<copy todir="${build.lib}">
<fileset dir="${lib.home}"></fileset>
</copy>
<!-- Package all files under the directory of build into a war file -->
<war warfile="${deploy.home}/${app.name}.war" webxml="${build.home}/WEB-INF/web.xml"
basedir="${build.home}">
<lib dir="${build.lib}"/>
<classes dir = "${build.classes}"/>
</war>
</target>
<!-- Deploy,copy $ {deploy.home}/$ {app.name} .war to the deploy directory of web container directly-->
<target name="deploy" depends="build">
<copy todir="${webapps.home}" file="${deploy.home}/${app.name}.war"></copy>
</target>
</project>
After runing this build.xml,we can get messages below form console area of
Eclipse
message:
Summary:
The steps that a Ant script needs to cover when deploying a web project usually include:
1. To specify some properties. The more properties you specify, the more possibility of this Ant script
can be reused.
2. Configure class path which are needed when compiling ,it should conclude all the jar package and
zip package under directory of lib .Besides,it should also contains jar package under the directory of
Jboss lib .
3. Do some work of initialization . Usually,we need to remove traces of the history of deployment.
4. Create some directories. We need to create some directories for compiling and building . To build a
good structure of web application , In this Ant project ,We create an build directory ;Define a deploy
folder ,and you can put the WAR file to this folder ,When you need to deploy this project,You can copy
this war file to the home of Jboss deploy directory.
5. Compile all java files under the src directory . Put these compiled class files to classes home .It
needs the support of class library when compiling .
6. Copy all the resources document to the home of building classes , If there are resource files which
suffix names are different,import them using "include". Copy the project files form WebRoot to the
corresponding building directory .
7. Package all files under the directory of build into a war file
eg:
<war warfile="${deploy.home}/${app.name}.war" webxml="${build.home}/WEB-INF/web.xml"
basedir="${build.home}">
<lib dir="${build.lib}"/>
<classes dir = "${build.classes}"/>
when you use this war command, you need to specify your project lib location and where your classes
are.
8. Finally , Deploy. Copy war file to the deploy directory of web container directly.
3. 3 An example project
Figure 1.1 shows the conceptual view of an Ant build file as a graph of targets, each target containing the
tasks. When the project is built, the Ant run time determines which targets need to be executed, and
chooses an order for the execution that guarantees a target is executed after all those targets it depends
on. If a task somehow fails,it signals this to the run time, which halts the build. This lets simple rules
such as “deploy after compiling” be described, as well as more complex ones such as “deploy only after
the unit tests and JSP compilation have succeeded.”
Figure 1.2 Conceptual view of a build file. The project encompasses a collection of targets.
Inside each target are task declarations, which are statements of the actions Ant must take to
build that target. Targets can state their dependencies on other targets, producing a graph of
dependencies. When executing a target, all its dependents must execute first.
Listing 1.1 shows the build file for this typical build process.
While listing 1.1 is likely to have some confusing pieces to it, it should be mostly
comprehensible to the Java-experienced Ant newbie; for example, deployment (target
name="deploy") depends on the successful compilation and generation of documentation
(depends="compile,doc"). Perhaps the most confusing piece is the ${...} notation used in the
FTP task (<ftp>). These are Ant properties, which we introduce in chapter 3. The output of our build is
In this build, we tell Ant to place the generated documentation alongside the compiled classes,
which is not a typical distribution layout but allows this example to be abbreviated. Using the
propertyfile command-line option is also atypical and used in situations where forced override
control is desired, such as forcing a build to deploy to a server other than the default. One final note is
that a typical distributable is not a JAR file; more likely it would be a tar, Zip, WAR, or E AR. Caveats
aside, the example shows Ant’s basics well: target dependencies, use of properties, compiling,
documenting, JAR’ing, and finally deploying.
Because Ant tasks are Java classes, the overhead of invoking each task is quite small.
Ant instantiates a Java object, sets some parameters, then tells it to perform its work.
A simple task such as <mkdir> would call a Java library package to execute the function.
A more complex task such as <ftp> would invoke a third-party FTP library to talk to the remote
server, and optionally perform dependency checking to only upload files that were newer than those at
the destination. A very complex task such as <javac> not only uses dependency checking to decide
which files to compile, it supports multiple compiler back ends, calling Sun’s Java compiler in the same
V M, or executing IB M’s Jikes compiler as an external executable.These are implementation details.
Simply ask Ant to compile some files with the debug flag turned on; how Ant decides which compiler to
use and how to translate
the debug flag into a compiler specific option are issues that you rarely need to worry about. It just
works.
That is the beauty of Ant: it just works. Specify the build file correctly and Ant will work out target
dependencies and call the targets in the right order.