Java Development with Ant Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Java Development with Ant ERIK HATCHER STEVE LOUGHRAN MANNING Greenwich (74° w. long.) Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com For online information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: orders@manning.com ©2003 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Maarten Reilingh 209 Bruce Park Avenue Typesetter: Martine Maguire-Weltecke Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1930110588 Printed in the United States of America 12345678910–VHG–0605040302 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com To my wife Carole and our two sons, Jakob and Ethan. Thank you for taking care of me while I took care of this book. Erik To Bina and Alexander. Thank you for being so patient during the long hours of this project. Steve Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com vii brief contents Part 1 Learning Ant 1 1 Introducing Ant 3 2 Getting started with Ant 23 3 Understanding Ant datatypes and properties 47 4 Testing with JUnit 85 5 Executing programs 111 6 Packaging projects 134 7 Deployment 163 8 Putting it all together 188 Part 2 Applying Ant 203 9 Using Ant in your development projects 205 10 Beyond Ant’s core tasks 234 11 XDoclet 260 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com viii BRIEF CONTENTS 12 Developing for the web 278 13 Working with XML 317 14 Enterprise JavaBeans 333 15 Working with web services 355 16 Continuous integration 386 17 Developing native code 407 18 Production deployment 431 Part 3 Extending Ant 465 19 Writing Ant tasks 467 20 Extending Ant further 498 Appendices AInstallation523 B XML primer as it applies to Ant 532 C IDE integration 536 D The elements of Ant style 544 E Ant task reference 561 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ix contents foreword xxv preface xxvii acknowledgments xxix about this book xxxi about the authors xxxvi about the cover illustration xxxvii Part 1 Learning Ant 1 1 Introducing Ant 3 1.1 What is Ant? 3 What is a build process and why do you need one? 4 Why do we think Ant makes a great build tool? 4 1.2 The core concepts of Ant 5 An example project 7 1.3 Why use Ant? 10 Integrated development environments 10 Make 11 ✦ Other build tools 13 Up and running, in no time 14 1.4 The evolution of Ant 14 1.5 Ant and software development methodologies 16 eXtreme Programming 16 Rational Unified Process 17 1.6 Our example project 17 Documentation search engine—example Ant project 18 1.7 Yeah, but can Ant… 19 1.8 Beyond Java development 21 Web publishing engine 21 ✦ Simple workflow engine 21 Microsoft .NET and other languages 21 1.9 Summary 22 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com x CONTENTS 2 Getting started with Ant 23 2.1 Defining our first project 23 2.2 Step one: verifying the tools are in place 24 2.3 Step two: writing your first Ant build file 24 Examining the build file 25 2.4 Step three: running your first build 26 If the build fails 27 ✦ Looking at the build in more detail 29 2.5 Step four: imposing structure 31 Laying out the source directories 32 ✦ Laying out the build directories 33 ✦ Laying out the dist directories 34 Creating the build file 35 ✦ Target dependencies 35 Running the new build file 36 ✦ Rerunning the build 37 How Ant handles multiple targets on the command line 38 2.6 Step five: running our program 39 Why execute from inside Ant 39 Adding an execute target 40 ✦ Running the new target 40 2.7 Ant command line options 41 Specifying which build file to run 42 Controlling the amount of information provided 42 Getting information about a project 44 2.8 The final build file 44 2.9 Summary 46 3 Understanding Ant datatypes and properties 47 3.1 Preliminaries 48 Datatype overview 48 ✦ Property overview 48 3.2 Introducing datatypes and properties with <javac> 49 3.3 Paths 51 3.4 Filesets 52 Fileset examples 53 ✦ Default excludes 53 3.5 Patternsets 54 3.6 Selectors 56 3.7 Datatype element naming 57 3.8 Filterset 58 Inserting date stamps in files at build-time 58 3.9 FilterChains and FilterReaders 59 3.10 Mappers 61 Identity mapper 61 ✦ Flatten mapper 62 Merge mapper 62 ✦ Glob mapper 63 Regexp mapper 63 ✦ Package mapper 64 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... 11 1 5 .1 Why you need to run external programs 5.2 Running Java programs 11 2 11 1 Introducing the task 11 3 ✦ Setting the classpath 11 4 Arguments 11 5 ✦ Defining system properties 11 6 Running the program in a new JVM 11 7 Setting environment variables 11 8 ✦ Controlling the new JVM 11 8 ✦ Handling errors with failonerror 11 9 Executing JAR files 12 0 ✦ Calling third-party programs 12 1 Probing for a Java. .. with WAR files 16 0 Testing packaging 16 1 Summary 16 2 7 Deployment 16 3 7 .1 Example deployment problems 16 4 Reviewing the tasks 16 4 ✦ Tools for deployment 16 4 7.2 Tasks for deployment 16 5 File transfer with 16 6 ✦ Probing for server availability 16 6 Inserting pauses into the build with 16 8 Ant s email task 16 9 ✦ Fetching remote files with 17 0 Using the tasks to deploy 17 1 7.3 FTP-based... information with the task 17 1 17 2 7.4 Email-based distribution of a packaged application 7.5 Local deployment to Tomcat 4.x 17 4 17 3 The Tomcat management servlet API 17 5 Deploying to Tomcat with Ant 17 6 7.6 Remote deployment to Tomcat 18 1 Interlude: calling targets with Using in deployment 18 5 7.7 Testing deployment 7.8 Summary 18 7 18 7 8 Putting it all together 18 2 18 8 8 .1 8.2... it 12 3 Setting a timeout 12 4 5.3 Starting native programs with 12 4 Setting environment variables 12 6 ✦ Handling errors 12 6 Handling timeouts 12 7 ✦ Making and executing shell commands 12 7 ✦ Probing for a program before calling it 5.4 5.5 5.6 5.7 5.8 12 9 Bulk execution with 13 0 Processing output 13 1 Limitations on execution 13 2 Best practices 13 2 Summary 13 3 6 Packaging projects 13 4 6 .1. .. Split Unregistered Version - http://www.simpopdf.com 10 .6 10 .7 10 .8 10 .9 The ant- contrib tasks 253 Sharing task definitions among projects 258 Best practices 258 Summary 259 11 XDoclet 260 11 .1 Installing XDoclet 2 61 11. 2 To-do list generation 2 61 11. 3 XDoclet architecture 262 XDoclet’s Ant tasks 263 ✦ Templating How XDoclet works 265 11 .4 Writing your own XDoclet template 264 265 Code generation 267... Filtering classes processed 273 11 .5 Advanced XDoclet 273 Custom subtasks 274 Creating a custom tag handler 11 .6 The direction of XDoclet 274 275 XDoclet versus C# 275 Looking into Java s future: JSR 17 5 and 18 1 11 .7 XDoclet best practices Dependency checking 11 .8 Summary 276 276 276 277 12 Developing for the web 278 12 .1 How are web applications different? 279 12 .2 Working with tag libraries 280 Creating... process 3 81 380 15 .8 The rigorous way to build a web service 3 81 15.9 Reviewing web service development 382 15 .10 Calling Ant via SOAP 383 15 .11 Summary 384 16 Continuous integration 386 16 .1 Scheduling Ant builds with the operating system The Windows way 387 ✦ The Unix version Making use of scripting 388 CONTEN TS 387 388 xvii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 16 .2... log 330 13 .5 Loading XML data into Ant properties 13 .6 Next steps in XML processing 332 13 .7 Summary 332 14 Enterprise JavaBeans 3 31 333 14 .1 EJB overview 333 The many types of Enterprise JavaBeans 334 EJB JAR 334 ✦ Vendor-specific situations 335 14 .2 A simple EJB build 335 14 .3 Using Ant s EJB tasks 336 14 .4 Using 337 Vendor-specific processing 339 14 .5 Using XDoclet for EJB development. .. files 13 5 How to delete files 13 5 ✦ How to copy files How to move files 13 7 ✦ Filtering 13 8 6.2 Preparing to package 13 6 13 9 Building and documenting release code 13 9 Adding data files 14 1 ✦ Preparing documentation Preparing install scripts and documents 14 3 Preparing libraries for redistribution 14 5 14 2 6.3 Creating archive files 14 6 JAR files 14 8 ✦ Creating a JAR file 14 8 Testing the JAR file 14 9 ✦... IDE 408 ✦ Using Make 17 .3 Introducing the task 409 410 Installing the tasks 410 ✦ Adding a compiler A quick introduction to the task 411 17 .4 Building a JNI library in Ant 411 412 Steps to building a JNI library 413 ✦ Writing the Java stub 414 Writing the C++ class 415 ✦ Compiling the C++ source 416 Deploying and testing the library 419 17 .5 Going cross-platform 422 Migrating the C++ source . Summary 11 0 5 Executing programs 11 1 5 .1 Why you need to run external programs 11 1 5.2 Running Java programs 11 2 Introducing the < ;java& gt; task 11 3 ✦ Setting the classpath 11 4 Arguments 11 5 ✦. 306 12 .7 Server-side testing with Cactus 310 Cactus from Ant s perspective 311 ✦ How Cactus works 313 And now our test case 314 ✦ Cactus summary 314 12 .8 Summary 315 13 Working with XML 317 13 .1. example project 7 1. 3 Why use Ant? 10 Integrated development environments 10 Make 11 ✦ Other build tools 13 Up and running, in no time 14 1. 4 The evolution of Ant 14 1. 5 Ant and software development