The Maven Definitive Guide
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 ......................................................................................................xiii 1. Creative Commons BY-ND-NC ........................................................xiii Foreword: Beta 0.13 ....................................................................................... xv 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 .............................. 15 2.3.3. Installing Maven on Linux ..................................................... 16 2.3.4. Installing Maven on FreeBSD or OpenBSD .......................... 16 2.4. Testing a Maven Installation ............................................................ 16 2.5. Maven Installation Details ............................................................... 17 2.5.1. User-specific Configuration and Repository .......................... 17 2.5.2. Upgrading a Maven Installation ............................................. 18 2.6. Getting Help with Maven ................................................................. 19 2.7. Using the Maven Help Plugin .......................................................... 20 2.7.1. Describing a Maven Plugin .................................................... 21 ii Maven: The Definitive Guide 2.8. About the Apache Software License ................................................ 23 I. Maven by Example ..................................................................................... 25 3. A Simple Maven Project ..................................................................... 27 3.1. Introduction ............................................................................... 27 3.1.1. Downloading this Chapter's Example ............................. 27 3.2. Creating a Simple Project .......................................................... 28 3.3. Building a Simple Project ......................................................... 30 3.4. Simple Project Object Model .................................................... 31 3.5. Core Concepts ........................................................................... 32 3.5.1. Maven Plugins and Goals ................................................ 33 3.5.2. Maven Lifecycle .............................................................. 35 3.5.3. Maven Coordinates ......................................................... 41 3.5.4. Maven Repositories ......................................................... 44 3.5.5. Maven's Dependency Management ................................. 46 3.5.6. Site Generation and Reporting ........................................ 49 3.6. Summary ................................................................................... 50 4. Customizing a Maven Project ............................................................. 51 4.1. Introduction ............................................................................... 51 4.1.1. Downloading this Chapter's Example ............................. 51 4.2. Defining the Simple Weather Project ....................................... 51 4.2.1. Yahoo! Weather RSS ...................................................... 52 4.3. Creating the Simple Weather Project ........................................ 53 4.4. Customize Project Information ................................................. 54 4.5. Add New Dependencies ............................................................ 55 4.6. Simple Weather Source Code ................................................... 57 4.7. Add Resources .......................................................................... 63 4.8. Running the Simple Weather Program ..................................... 65 4.8.1. The Maven Exec Plugin .................................................. 66 4.8.2. Exploring Your Project Dependencies ............................ 66 4.9. Writing Unit Tests ..................................................................... 69 4.10. Adding Test-scoped Dependencies ......................................... 71 4.11. Adding Unit Test Resources ................................................... 72 4.12. Executing Unit Tests ............................................................... 74 4.12.1. Ignoring Test Failures ................................................... 75 iii Maven: The Definitive Guide 4.12.2. Skipping Unit Tests ....................................................... 77 4.13. Building a Packaged Command Line Application .................. 78 5. A Simple Web Application ................................................................. 81 5.1. Introduction ............................................................................... 81 5.1.1. Downloading this Chapter's Example ............................. 81 5.2. Defining the Simple Web Application ...................................... 81 5.3. Creating the Simple Web Project .............................................. 82 5.4. Configuring the Jetty Plugin ..................................................... 83 5.5. Adding a Simple Servlet ........................................................... 85 5.6. Adding J2EE Dependencies ...................................................... 87 5.7. Conclusion ................................................................................. 90 6. A Multi-module Project ...................................................................... 91 6.1. Introduction ............................................................................... 91 6.1.1. Downloading this Chapter's Example ............................. 91 6.2. The Simple Parent Project ......................................................... 92 6.3. The Simple Weather Module .................................................... 93 6.4. The Simple Web Application Module ...................................... 96 6.5. Building the Multi-module Project ........................................... 98 6.6. Running the Web Application ................................................. 100 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 ..................................................... 117 7.6. The Simple Web Application Module .................................... 125 7.7. Running the Web Application ................................................. 137 7.8. The Simple Command Module ............................................... 139 7.9. Running the Simple Command ............................................... 144 7.10. Conclusion ............................................................................. 144 7.10.1. Programming to Interface Projects .............................. 145 iv Maven: The Definitive Guide 8. Optimizing and Refactoring POMs ................................................... 147 8.1. Introduction ............................................................................