Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Professional Java.JDK.5.Edition (Wrox)

.pdf
Скачиваний:
31
Добавлен:
29.02.2016
Размер:
12.07 Mб
Скачать

Chapter 14

need to contain a project and a target element. Here is a quick example of the syntax of a very basic build.xml file that just displays a “Hello World!” message:

<project name=”antTest” default=”Hello” basedir=”.”> <description>A very simple build.xml file</description>

<target name=”Hello”>

<echo message=”Hello World!”/> </target>

</project>

In order to run this example, you would change directory to the directory that contains the build.xml file from a console window and simply type ant. ANT will automatically look for the file named build.xml as a default. Once ANT finds the file, it executes it based on the default target supplied in the project element of the build file. In this case, the default target and only target is Hello. The output is shown in the following example:

C:\btest>ant

Buildfile: build.xml

Hello:

[echo] Hello World!

BUILD SUCCESSFUL

Total time: 0 seconds

The ANT manual does a terrific job of explaining the different XML elements such as project, target, classpath, filesets, and so forth, so there isn’t a need to explain them in-depth here. What is needed is to show you how to glue them all together. This next example will show you how to create a complete Web Archive (WAR) file using ANT. This example contains two files: a mybuild.properties file to contain the properties you will read in for Ant to use, and the staple build.xml file that is the main build file that Ant will execute. The following is the content of the mybuild.properties file:

#Xerces home directory xerces.home = C:\\xerces-2_6_2

#The name of the .jar file to create

jar.name = myantwebapp.jar

# The name of the .war file to create war.name = myantwebapp.war

The first property shows a third-party tool location that you will need for compiling and packaging the source code. The next two properties list the name that you want the JAR and the final WAR file to be called. It’s time now to dissect the complex build.xml file. This file is made up of four targets, three of which are dependent upon another target. When a dependency occurs in an ANT target, ANT must execute the dependency first. So, if target D is dependent on target C, and target C is dependent on target B, and target B is dependent on target A, ANT would execute the targets in the following order: A, B, C, then D:

<project name=”MYANTWEBAPP” default=”createWAR” basedir=”.”>

<description>This a real world example of using ANT.</description>

656

Packaging and Deploying Your Java Applications

The <project> tag defines a name for the project and requires you to supply a default target to execute. In this case, you want ANT to run the createWAR target first. The createWAR target has a dependency chain, as I explained in the A, B, C, and D target example. The basedir attribute is asking which directory it should use as a base for execution. The . signifies the current directory:

<property file=”mybuild.properties”/>

<!-- set global properties for this build --> <property name=”src” location=”src”/> <property name=”jsps” location=”jsp”/> <property name=”build” location=”build”/> <property name=”dist” location=”dist”/>

Now, you are telling ANT to read in the properties from mybuild.properties and to also create four additional properties: src, jsps, build, and dist. These can now all be accessed by their property name with the following syntax — ${propertyname} — in the ANT build file:

<path id=”everything”>

<fileset dir=”${xerces.home}”> <include name=”xercesImpl.jar”/> <include name=”xml-apis.jar”/>

</fileset>

<pathelement location=”${build}”/> </path>

The <path> tag will be used by the build file to incorporate the files in the path into a classpath that will be used to compile source code. Here, two Xerces jar files are being built into a path element named everything:

<target name=”clean” description=”Deletes the build and dist directories” > <delete dir=”${build}”/>

<delete dir=”${dist}”/> </target>

The first target, clean, gets executed first and simply deletes the build and distribution directories. The <delete> tag is an ANT task. ANT has a multitude of tasks that can perform many operations. Refer to the Ant manual for more information:

<target name=”init” depends=”clean”> <mkdir dir=”${build}”/>

<mkdir dir=”${dist}”/> </target>

The second target, init, depends on clean. Once clean deletes the build and dist directories, the init target recreates them. These two targets ensure that the build and dist directories will be empty before you start compiling your source code:

<target name=”createJAR” depends=”init” description=”Compiles source and creates new JAR” >

<javac classpathref=”everything” classpath=”${src}” srcdir=”${src}”

657

Chapter 14

destdir=”${build}”/>

<mkdir dir=”${dist}/lib”/>

<echo message=”Creating jar: ${dist}\lib\${jar.name}”/>

<jar destfile=”${dist}/lib/${jar.name}” includes=”**/*.class” basedir=”${build}” compress=”true” index=”true” update=”true”/>

</target>

The third target, createJAR, depends on init and uses the ANT task <javac> to compile any source code that is in the SRC directory. You should also take note that the classpathref references the path that was built earlier called everything. The <javac> task will use the everything path in its classpath for compiling the source files. After the files are compiled, a very handy Ant task called <jar> is used to create a JAR file into the lib directory that was created:

<target name=”createWAR” depends=”createJAR”>

<copy preservelastmodified=”true” overwrite=”true” todir=”${jsps}/WEB-INF/lib”>

<fileset dir=”${dist}/lib”> <include name=”${jar.name}”/>

</fileset>

</copy>

<mkdir dir=”${dist}/war”/>

<war destfile=”${dist}/war/${war.name}” webxml=”${jsps}/WEB-INF/web.xml”

update=”true”>

<fileset dir=”${jsps}” includes=”*.html,*.jsp,*.doc” excludes=”*.jar,*.war”/>

<webinf dir=”${jsps}/WEB-INF” includes=”*.wsdd,*.lst”/>

<lib dir=”${jsps}/WEB-INF/lib” includes=”*.jar,*.war,*.zip”/> <zipfileset dir=”${jsps}/images” prefix=”images” excludes=”*.psd”/>

</war>

</target>

</project>

The final target, createWAR, depends on createJAR and is used to create a WAR file. The JAR file was created and moved to the WAR files WEB-INF/lib directory because it has utilities that the WAR file needs. The other files, which you can see in the fileset, are then moved into position to create the WAR file. The WAR file is created using another handy ANT task called <WAR>.

This ANT build file example can now be run over and over every time you need to recompile and package your program. This example shows just how useful and easy it is to use ANT. If you need to replace Xerces with a new version, all that is required is a property change to mybuild.properties. However, this example barely touches on all the different ANT tasks that are available to you. The ANT manual that comes with the Ant distribution should explain all the tasks in great detail.

658

Packaging and Deploying Your Java Applications

Summar y

Packaging and deploying Java applications vary depending on the program you are currently working on. This chapter touched on the most popular types of Java applications that you will come across. It took you through the intricacies of the different Java archive files — JAR, WAR, and EAR — and kept going right into applet land. It also supplied you with a few helpful tools for managing your classpath and an explanation of already existing Java tools that can aid you in your packaging efforts such as the jarsigner and keytool tools.

This chapter discussed the great innovations of Java Web Start and how it can be the technology of the future for deploying thick, rich clients to users over browser-based technologies. Finally, this chapter examined the usefulness of ANT and how it can make a developer’s building and configuration management woes a thing of the past.

659

References

[AMBLER] Ambler, Scott M. Agile Modeling: Effective Practices for Extreme Programming and the Unified Process. Indianapolis, IN: John Wiley & Sons, 2002.

[BECK] Beck, Kent. Extreme Programming Explained. Boston, MA: Addison Wesley, 1999.

[FOWLER] Fowler, Martin. Refactoring. Boston, MA: Addison Wesley, 1999.

[LARMAN] Larman, Craig. Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and the Unified Process, second edition, Upper Saddle River, NJ: Prentice Hall PTR, 2002.

SYMBOLS

& (ampersand), in extends clause, 5 <> (angle braces)

in generic class definitions, 3, 5 in generic method definitions, 6

* (asterisk), in regular expressions, 53, 57, 58 \ (backslash), meta-character for, 55

{} (braces), in regular expressions, 56–58 ^ (caret), in regular expressions, 55, 56

: (colon)

in for loop, 7–8

in manifest file, 629

$ (dollar sign), in regular expressions, 55

... (ellipsis), for variable arguments, 9–10 = (equal sign), in property file, 629

/ (forward slash) directory separator, 42 in preference nodes, 63

( ) (parentheses), in regular expressions, 58

% (percent sign), prefixing filename patterns, 42–43

. (period), in regular expressions, 55, 56

+ (plus sign), in regular expressions, 57, 58

? (question mark), in regular expressions, 57, 58 [] (square brackets), in regular expressions, 56

A

absolute() method, ResultSet class, 300 absolutePath() method, Preference class, 65 Abstract Windowing Toolkit (AWT) classes, 143 abstraction, 114

Index

Index

Accessibility classes, 143

actions, Swing, 233–234, 239–242

Activatable class, 450 activatable remote objects, 450

Adaptee class, Adapter pattern, 121

Adapter class, Adapter pattern, 121 Adapter pattern, 119–122 addFolder() method, 440

addHandler() method, Logger class, 31 addListener() method, MessageConsumer

class, 548

addLogger() method, LogManager class, 29 addMessage() method, 439 addNodeChangeListener() method, Preference

class, 67 addPreferenceChangeListener() method,

Preference class, 67 addPropertyChangeListener() method,

LogManager class, 30 addRowSetListener() method, RowSet, 308

AfterLast() method, ResultSet class, 300 agile methodologies

UP (Unified Process), 83–85, 86–87

XP (eXtreme Programming), 81, 85–87

Agile Modeling: Effective Practices for Extreme Programming and the Unified Process (Ambler, Scott), 75, 661

algorithm() method, Key interface, 592–593

AlgorithmParameterGenerator class, JCA, 585, 598

AlgorithmParameters class, JCA, 585, 597–598

algorithms, replacing on the fly

algorithms, replacing on the fly, 134–138, 192 aliases() method, KeyStore class, 596

Ambler, Scott (Agile Modeling: Effective Practices for Extreme Programming and the Unified Process), 75, 661

ampersand (&), in extends clause, 5 angle braces (<>)

in generic class definitions, 3, 5 in generic method definitions, 6

Annotation Editor example

AnnotationEditor class, 198–200

ComboListener class, 213–214

ComponentListener class, 202–204

ExcelAction class, 207–209

HighlightPainter class, 204–205

OpenAction class, 209–212

PopupListener class, 201–202

PrintAction class, 205–207

XmlAction class, 207

Annotation interface, 18 annotation package, 17

AnnotationDesc interface, 20–21

AnnotationDesc.ElementValuePair interface, 21

AnnotationEditor class, Annotation Editor example, 198–200

annotations definition of, 17–18

examples of, 19–20, 22–26 interfaces in doclet API for, 20–22 types of, 18–19

annotations() method, 20 annotationType() method, AnnotationDesc

interface, 21

AnnotationTypeDoc interface, 21

AnnotationTypeElementDoc interface, 21

AnnotationValue interface, 22

Ant application (Apache)

building projects with, 655–658 definition of, 654

development scenarios using, 87–95 installing, 655

learning, using patterns for, 113 running TCPMon with, 496

Apache AXIS

definition of, 533–534

deploying a Web service, 535–537 setting up, 534–535

TCPMon included in, 496 version of, returning, 528–529

writing Web service client, 537–539

Apache Jakarta Project, 499

Apache TCPMon, 496–498, 534, 539

Apache Tomcat server, 321, 534

The Apache XML Project, 499

APIs, learning using patterns, 112–113. See also specific APIs

appendReplacement() method, Matcher class, 59 appendTail() method, Matcher class, 59

applets

definition of, 636

in JAR files, 629–630 packaging for execution, 638 RMI for, 446

security of, 639 structure of, 636–638

appletviewer command, 638 application data

definition of, 224–225

persisting in Swing application, 232–235 persisting (saving), 225–227, 230–232

application layer, 479–480

Application scope, WebWork framework, 374 applications. See also database, persisting applications

with; serialization; software design and development

deploying

Ant application for, 654–658 applets, 638–639

classpaths, managing, 619–624 EAR (Enterprise Archive), 644–646 EJBs (Enterprise JavaBeans), 643 endorsed directory and, 624

JAR files for, 625–636

Java Web Start, 647–654

Web applications, 639–643

time-based license for definition of, 235–236

implementing license, 236–238 implementing timeserver, 238–239

applicationScope implicit object, 340 application.xml file, 644–645

Applying UML and Patterns: An Introduction to ObjectOriented Analysis and Design and the Unified Process (Larman, Craig), 83, 111, 661

664

CardLayout manager

arguments, variable, 2, 9–10 arrays

of generic types, 6 iterators for, 8

in JNI, 410–416 of type variables, 6

asResult() method, Matcher class, 59 Assertions, JMeter, 108

assignment, boxing and unboxing conversions in, 12–13 associations between classes, creating, 115–116 asterisk (*), in regular expressions, 53, 57, 58 attributes, static, importing, 13–15

authentication definition of, 612

JAAS (Java Authentication and Authorization Service) authenticating a subject, 615, 616–617 authorization, 617–618

AuthPermission class, 617–618 configurations for authentication, 615–616 credentials, 613, 615

definition of, 612

Destroyable interface, 615

executing code with security checks, 613–617

LoginContext class, 615

policy file for authorization, 617–618 principals, 614

PrivilegedAction interface, 613

Refreshable interface, 615

Subject class, 612–613 user identification, 612–613

MAC (Message Authentication Code), 583, 602,

611–612

authorization, 617–618

AuthPermission class, JAAS, 617–618

AWT (Abstract Windowing Toolkit) classes, 143 AXIS (Apache)

definition of, 533–534

deploying a Web service, 535–537 setting up, 534–535

TCPMon included in, 496 version of, returning, 528–529

writing Web service client, 537–539

B

backreferences, in regular expressions, 58 backslash (\), meta-character for, 55 bank application example, 478

Beck, Kent (eXtreme Programming Explained), 85, 661 beep character, meta-character for, 55

BeforeFirst() method, ResultSet class, 300 beginTransaction() method, Session class, 319 bell character, meta-character for, 55

Berners-Lee, Tim (visionary for World Wide Web), 523

BorderLayout manager, 144–151

BorderLayout() method, BorderLayout class, 145 boxing and unboxing conversions, 2, 11–13

BoxLayout manager definition of, 151–152 example of

CondimentPanel class, 156–157

Decorator pattern in, 152–153

DropTargetListener interface, 154–156

Food class, 157–158

FoodCourt interface, 153, 156

FoodGraphic class, 153–154

FoodItems class, 158–161 model of, 152

BoxLayout() method, BoxLayout class, 152 braces ({}), in regular expressions, 56–58

bug reporting and tracking, 81

build() method, CertPathBuilder class, 601 business logic, separating from User Interface logic,

122–130 business tier, J2EE, 87

Buyer class, Strategy pattern, 136

C

CA (certificate authority), 600 CachedRowSetImpl RowSet implementation, 309 Call Level Interface (CLI), X/Open SQL, 282

CallableStatement interface, 292–294, 297

CallbackHandler class, JAAS, 616

CallNonvirtual[Type]Method() function, 421–423

CallNonvirtual[Type]MethodA() function, 421–423

CallNonvirtual[Type]MethodV() function, 421–423

Call[Type]Method() function, 421–423

Call[Type]MethodA() function, 421–423

Call[Type]MethodV() function, 421–423

CardLayout manager alternative to, 214 definition of, 191

Index

665

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]