JSP and J2EE applications

Download Report

Transcript JSP and J2EE applications

Apache Ant and XDoclet
How to build JSP and J2EE
applications
Obtain and Install
• http://ant.apache.org (currently v. 1.6.2)
• Precondition – install J2SE (or JRE) and
set variable %JAVA_HOME%
• Unpack Ant’s installation and set variable
%ANT_HOME%
• Add %ANT_HOME%\bin to your %PATH%
variable to enable running “ant.bat” utility
from any DOS window
What is Ant - I
• Ant is a “build tool” – especially useful to
compile, package, deploy Java projects. It can
do much more (file operations, version
control/CVS, XML validations/transforms, run
system commands, custom tasks, etc.). Ant is
suitable also for all kinds of Java-unrelated file
manipulations, e.g. preprocessing static stuff in
Web publishing
• Ant’s alternatives are “batch” files under
Windows, Makefile-s or shellscripts under UNIX.
Build scripts may be written in other scripting
languages as well.
What is Ant - II
• What is Ant?
– Java-based build tool
• Why use Ant?
– Cross-platform
– Java domain smart
– Fast, extensible, integrated
– Alternatives?
• Analogy
– Factory automation
Ant and IDE tools
• They are complementary: Ant build scripts
can automate all kinds of build processes.
• Use IDE (e.g. Eclipse) for editing, unittesting and version-controlling code; use
Ant for doing more complex development
tasks, e.g. deployments and system tests.
• Eclipse plugins can solve similar issues as
Ant can, but Ant is free and easier to
standardize.
Ant’s Distinctive Features
• Build-scripts (the build.xml files) are written
using a special XML syntax
• “Ant tasks” represent object-oriented approach
to build process. E.g. jar-tasks inherit from ziptasks.
• Build-file language does not have flow control
features (e.g. loops/iterators, if-constructs, trycatch constructs). Instead it is based on
recursive nature of Ant’s tasks themselves and
Ant’s datatypes like filesets, patterns, filters,
mappers, etc. (Project Ant-contrib extends Ant
with some control structures.)
Ant in Java Projects
• Even when there is IDE build environment, it is
useful to have Ant script to build and deploy
(everything is transparent there)
• In big projects build scripts are usually written by
“Technical Designer” or qualified developer
• Other developers ideally just need to remember
a few commands, e.g.
ant -projecthelp (lists targets and their
descriptions)
ant junit (rebuild and run JUnit tests)
ant deploy (build and deploy for a system
test)
Build File
•
•
•
•
XML format
Typically in project root directory
Default name: build.xml
Declarative - define steps, not scripty
details
• Defines a single project
• A project contains targets
• Targets contain tasks
Project Structure
• Directory structure
– Simpler is better
– Map to artifacts
– Customizations/personalizations
– IDE issues
– Separate source from artifacts - cleanable
Directory Structure in Eclipse
• MANIFEST.MF
and web.xml are
generated by
MyEclipse. All the
rest is provided
by the developer.
Ant Directory Structure
build.xml
<root>
build
META-INF
WEB-INF
lib
MANIFEST.MF
classes
*.class
lib
web.xml
*.jsp, *.html,...
*.jar
src
*.java
WebRoot
META-INF
WEB-INF
MANIFEST.MF
web.xml
*.jsp, *.html, *.css, *.js
dependent
stuff - to be
deployed to
JBoss as a
WAR file
Target Dependencies
clean
compile
webdoclet
package-war
deploy
build.xml - Targets
<?xml version="1.0" ?>
<project name="sample" default="deploy" basedir=".">
<property file="build.properties" />
<property file="build.default.properties" />
<target name="clean" description="Removes build artifacts"
…
</target>
<target name="deploy" description="Deploys application"
depends="compile,jar">
…
</target>
</project>
3 Ways to Define Properties
(1) In Ant file itself
(2) In one or more external properties file. E.g.
build.default.properties
# Directory for compiled classes
build.dir = ${basedir}/build
# Java compilation properties
javac.debug = true
javac.source = 1.3
javac.target = 1.1
(3) In the command line. E.g.
ant -Dappname=newstuff
jar
<property> task
• Name/value
<property name="site"
value="sample"/>
• Load from properties file
<property
file="build.properties" />
• Load environment variables
<property environment="env"/>
Properties for files/directories
• Some properties refer to paths - they are
computed relative to ${basedir} or to each
other
<property name="build.dir"
location="build"/>
<property name="build.webinf.dir"
location="${build.dir}/WEB-INF" />
Directory-related tasks
<delete dir="${build.dir}" failonerror =
"false"/>
<delete file="${name}.war" failonerror =
"false"/>
<mkdir dir="${build.dir}"/>
Filesets and Classpaths
<path id="compile.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
A more sophisticated approach - order JARs in
subfolders
lib/lib.properties - Global default library mappings
#
# Lucene - http://jakarta.apache.org/lucene
#
lucene.version
= 1.2
lucene.dir=${lib.dir}/lucene-${lucene.version}
lucene.jar=${lucene.dir}/lucene-${lucene.version}.jar
<javac>
• Façade over Java compilers
• Performs .java/.class out-of-date checking
<javac
srcdir="src"
destdir="${build.dir}/WEB-INF/classes"
debug="${javac.debug}"
classpathref="compile.classpath"
/>
JAR Task
<jar basedir="${build.dir}"
jarfile="${name}.jar"
includes="**/*.class"/>
WAR Task
<war destfile="myapp.war" webxml=
"${web.dir}/WEB-INF/web.xml">
<fileset dir="WebRoot"/>
<lib dir="${lib.dir}">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="${build.dir}/bin"/>
</war>
Copying
Copy a single file to a directory
<copy file="myfile.txt"
todir="../some/other/dir"/>
Copy a directory to another directory
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Debugging Ant Scripts
• By default, Ant’s messages for failed tasks
might be insufficient. You can easily make
Ant more verbose to see what it is doing:
ant -verbose taskname
ant -debug taskname (very verbose)
These will show each elementary action
performed by the build process
References - I
• http://ant.apache.org/manual/index.html online
manual
• Books on Ant
– Ant Developer's Handbook by Alan Williamson, Kirk
Pepperdine, Joey Gibson, Andrew Wu
– Java™ Extreme Programming Cookbook by Eric M.
Burke, Brian M. Coyner
• Some add-ons to Ant platform include
Checkstyle, Ant-contrib, XDoclet, Canoo
References - II
• Java Development with Ant
– http://www.amazon.com/exec/obidos/ASIN/1930110588
– http://www.manning.com/antbook/
• Pragmatic Programmer
– http://www.pragmaticprogrammer.com/
• Agile development
– eXtreme Programming Explained, Kent Beck
– Agile Modeling, Scott Ambler