Brian Fox (Sonatype, Inc.), Bruce Snyder (Sonatype, Inc.), Jason Van Zyl (Sonatype, Inc.), Eric Redmond ()
Total Page:16
File Type:pdf, Size:1020Kb
Tim O'Brien (Sonatype, Inc.), John Casey (Sonatype, Inc.), Brian Fox (Sonatype, Inc.), Bruce Snyder (Sonatype, Inc.), Jason Van Zyl (Sonatype, Inc.), Eric Redmond () Copyright © 2006-2008 Copyright .......................................................................................................xii 1. Creative Commons BY-ND-NC .........................................................xii Foreword: 0.3 ................................................................................................ xiv Preface ........................................................................................................... xvi 1. How to Use this Book ........................................................................ xvi 2. Your Feedback ..................................................................................xvii 3. Font Conventions .............................................................................xviii 4. Maven Writing Conventions ............................................................xviii 5. Acknowledgements ............................................................................ xix 1. Introducing Apache Maven .......................................................................... 1 1.1. Maven... What is it? ........................................................................... 1 1.2. Convention Over Configuration ......................................................... 2 1.3. A Common Interface .......................................................................... 3 1.4. Universal Reuse through Maven Plugins ........................................... 4 1.5. Conceptual Model of a "Project" ....................................................... 5 1.6. Is Maven an alternative to XYZ? ....................................................... 6 1.7. Comparing Maven with Ant .............................................................. 8 1.8. Summary .......................................................................................... 12 2. Installing and Running Maven ................................................................... 13 2.1. Verify your Java Installation ............................................................ 13 2.2. Downloading Maven ........................................................................ 14 2.3. Installing Maven .............................................................................. 14 2.3.1. Installing Maven on Mac OSX .............................................. 14 2.3.2. Installing Maven on Microsoft Windows .............................. 16 2.3.3. Installing Maven on Linux ..................................................... 16 2.3.4. Installing Maven on FreeBSD or OpenBSD .......................... 16 2.4. Testing a Maven Installation ............................................................ 17 2.5. Maven Installation Details ............................................................... 17 2.5.1. User-specific Configuration and Repository .......................... 18 2.5.2. Upgrading a Maven Installation ............................................. 19 2.5.3. Upgrading from Maven 1.x to Maven 2.x .............................. 20 2.6. Uninstalling Maven .......................................................................... 21 2.7. Getting Help with Maven ................................................................. 21 ii Maven: The Definitive Guide 2.8. Using the Maven Help Plugin .......................................................... 22 2.8.1. Describing a Maven Plugin .................................................... 23 2.9. About the Apache Software License ................................................ 25 I. Maven by Example ..................................................................................... 28 3. A Simple Maven Project ..................................................................... 30 3.1. Introduction ............................................................................... 30 3.1.1. Downloading this Chapter's Example ............................. 30 3.2. Creating a Simple Project .......................................................... 31 3.3. Building a Simple Project ......................................................... 33 3.4. Simple Project Object Model .................................................... 34 3.5. Core Concepts ........................................................................... 35 3.5.1. Maven Plugins and Goals ................................................ 36 3.5.2. Maven Lifecycle .............................................................. 38 3.5.3. Maven Coordinates ......................................................... 42 3.5.4. Maven Repositories ......................................................... 45 3.5.5. Maven's Dependency Management ................................. 47 3.5.6. Site Generation and Reporting ........................................ 50 3.6. Summary ................................................................................... 51 4. Customizing a Maven Project ............................................................. 52 4.1. Introduction ............................................................................... 52 4.1.1. Downloading this Chapter's Example ............................. 52 4.2. Defining the Simple Weather Project ....................................... 52 4.2.1. Yahoo! Weather RSS ...................................................... 53 4.3. Creating the Simple Weather Project ........................................ 53 4.4. Customize Project Information ................................................. 55 4.5. Add New Dependencies ............................................................ 56 4.6. Simple Weather Source Code ................................................... 58 4.7. Add Resources .......................................................................... 64 4.8. Running the Simple Weather Program ..................................... 66 4.8.1. The Maven Exec Plugin .................................................. 67 4.8.2. Exploring Your Project Dependencies ............................ 68 4.9. Writing Unit Tests ..................................................................... 70 4.10. Adding Test-scoped Dependencies ......................................... 73 4.11. Adding Unit Test Resources ................................................... 74 iii Maven: The Definitive Guide 4.12. Executing Unit Tests ............................................................... 76 4.12.1. Ignoring Test Failures ................................................... 77 4.12.2. Skipping Unit Tests ....................................................... 78 4.13. Building a Packaged Command Line Application .................. 79 4.13.1. Attaching the Assembly Goal to the Package Phase ..... 81 5. A Simple Web Application ................................................................. 83 5.1. Introduction ............................................................................... 83 5.1.1. Downloading this Chapter's Example ............................. 83 5.2. Defining the Simple Web Application ...................................... 83 5.3. Creating the Simple Web Project .............................................. 84 5.4. Configuring the Jetty Plugin ..................................................... 85 5.5. Adding a Simple Servlet ........................................................... 87 5.6. Adding J2EE Dependencies ...................................................... 90 5.7. Conclusion ................................................................................. 91 6. A Multi-module Project ...................................................................... 92 6.1. Introduction ............................................................................... 92 6.1.1. Downloading this Chapter's Example ............................. 92 6.2. The Simple Parent Project ......................................................... 92 6.3. The Simple Weather Module .................................................... 94 6.4. The Simple Web Application Module ...................................... 97 6.5. Building the Multimodule Project ............................................. 99 6.6. Running the Web Application ................................................. 101 7. Multi-module Enterprise Project ....................................................... 102 7.1. Introduction ............................................................................. 102 7.1.1. Downloading this Chapter's Example ........................... 102 7.1.2. Multi-module Enterprise Project ................................... 103 7.1.3. Technology Used in this Example ................................ 106 7.2. The Simple Parent Project ....................................................... 107 7.3. The Simple Model Module ..................................................... 108 7.4. The Simple Weather Module .................................................. 113 7.5. The Simple Persist Module ..................................................... 118 7.6. The Simple Web Application Module .................................... 126 7.7. Running the Web Application ................................................. 138 7.8. The Simple Command Module ............................................... 139 iv Maven: The Definitive Guide 7.9. Running the Simple Command ............................................... 146 7.10. Conclusion ............................................................................