Maven by Example I
Total Page:16
File Type:pdf, Size:1020Kb
Maven by Example i Maven by Example Ed. 0.7 Maven by Example ii Contents 1 Introducing Apache Maven1 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..........................3 1.5 Conceptual Model of a “Project”..............................4 1.6 Is Maven an alternative to XYZ?..............................5 1.7 Comparing Maven with Ant................................6 2 Installing Maven 10 2.1 Verify your Java Installation................................ 10 2.2 Downloading Maven.................................... 11 2.3 Installing Maven...................................... 11 Maven by Example iii 2.3.1 Installing Maven on Linux, BSD and Mac OS X................. 11 2.3.2 Installing Maven on Microsoft Windows...................... 12 2.3.2.1 Setting Environment Variables..................... 12 2.4 Testing a Maven Installation................................ 13 2.5 Maven Installation Details................................. 13 2.5.1 User-Specific Configuration and Repository.................... 14 2.5.2 Upgrading a Maven Installation.......................... 15 2.6 Uninstalling Maven..................................... 15 2.7 Getting Help with Maven.................................. 15 2.8 About the Apache Software License............................ 16 3 A Simple Maven Project 17 3.1 Introduction......................................... 17 3.1.1 Downloading this Chapter’s Example....................... 17 3.2 Creating a Simple Project.................................. 18 3.3 Building a Simple Project.................................. 21 3.4 Simple Project Object Model................................ 22 3.5 Core Concepts........................................ 23 3.5.1 Maven Plugins and Goals............................. 23 Maven by Example iv 3.5.2 Maven Lifecycle.................................. 25 3.5.3 Maven Coordinates................................. 28 3.5.4 Maven Repositories................................. 31 3.5.5 Maven’s Dependency Management........................ 33 3.5.6 Site Generation and Reporting........................... 35 3.6 Summary.......................................... 35 4 Customizing a Maven Project 36 4.1 Introduction......................................... 36 4.1.1 Downloading this Chapter’s Example....................... 36 4.2 Defining the Simple Weather Project............................ 37 4.2.1 Yahoo Weather RSS................................ 37 4.3 Creating the Simple Weather Project............................ 37 4.4 Customize Project Information............................... 40 4.5 Add New Dependencies................................... 41 4.6 Simple Weather Source Code................................ 43 4.7 Add Resources....................................... 49 4.8 Running the Simple Weather Program........................... 50 4.8.1 The Maven Exec Plugin.............................. 52 Maven by Example v 4.8.2 Exploring Your Project Dependencies....................... 52 4.9 Writing Unit Tests...................................... 54 4.10 Adding Test-scoped Dependencies............................. 56 4.11 Adding Unit Test Resources................................ 57 4.12 Executing Unit Tests.................................... 59 4.12.1 Ignoring Test Failures............................... 60 4.12.2 Skipping Unit Tests................................. 61 4.13 Building a Packaged Command Line Application..................... 62 4.13.1 Attaching the Assembly Goal to the Package Phase................ 64 5 A Simple Web Application 66 5.1 Introduction......................................... 66 5.1.1 Downloading this Chapter’s Example....................... 66 5.2 Defining the Simple Web Application........................... 67 5.3 Creating the Simple Web Project.............................. 67 5.4 Configuring the Jetty Plugin................................ 69 5.5 Adding a Simple Servlet.................................. 71 5.6 Adding J2EE Dependencies................................ 73 5.7 Conclusion......................................... 75 Maven by Example vi 6 A Multi-Module Project 76 6.1 Introduction......................................... 76 6.1.1 Downloading this Chapter’s Example....................... 76 6.2 The Simple Parent Project................................. 77 6.3 The Simple Weather Module................................ 78 6.4 The Simple Web Application Module........................... 81 6.5 Building the Multimodule Project............................. 83 6.6 Running the Web Application................................ 85 7 Multi-Module Enterprise Project 86 7.1 Introduction......................................... 86 7.1.1 Downloading this Chapter’s Example....................... 86 7.1.2 Multi-Module Enterprise Project.......................... 87 7.1.3 Technology Used in this Example......................... 89 7.2 The Simple Parent Project................................. 90 7.3 The Simple Model Module................................. 91 7.4 The Simple Weather Module................................ 96 7.5 The Simple Persist Module................................. 100 7.6 The Simple Web Application Module........................... 107 Maven by Example vii 7.7 Running the Web Application................................ 118 7.8 The Simple Command Module............................... 120 7.9 Running the Simple Command............................... 127 7.10 Conclusion......................................... 130 7.10.1 Programming to Interface Projects......................... 130 8 Optimizing and Refactoring POMs 132 8.1 Introduction......................................... 132 8.2 POM Cleanup........................................ 133 8.3 Optimizing Dependencies.................................. 133 8.4 Optimizing Plugins..................................... 138 8.5 Optimizing with the Maven Dependency Plugin...................... 139 8.6 Final POMs......................................... 142 8.7 Conclusion......................................... 151 9 Creative Commons License 152 10 Copyright 154 Maven by Example viii Preface Maven is a build tool, a project management tool, an abstract container for running build tasks. It is a tool that has shown itself indispensable for projects that graduate beyond the simple and need to start finding consistent ways to manage and build large collections of interdependent modules and libraries which make use of tens or hundreds of third-party components. It is a tool that has removed much of the burden of third-party dependency management from the daily work schedule of millions of engineers, and it has enabled many organizations to evolve beyond the toil and struggle of build management into a new phase where the effort required to build and maintain software is no longer a limiting factor in software design. This work is the first attempt at a comprehensive title on Maven. It builds upon the combined experience and work of the authors of all previous Maven titles, and you should view it not as a finished work but as the first edition in a long line of updates to follow. While Maven has been around for a few years, the authors of this book believe that it has just begun to deliver on the audacious promises it makes. The authors, and company behind this book, Sonatype, believe that the publishing of this book marks the beginning of a new phase of innovation and development surrounding Maven and the software ecosystem that surrounds it. Acknowledgements Sonatype would like to thank the following contributors. The people listed below have provided feedback which has helped improve the quality of this book. Thanks to Raymond Toal, Steve Daly, Paul Strack, Paul Reinerfelt, Chad Gorshing, Marcus Biel, Brian Dols, Mangalaganesh Balasubramanian, Marius Kruger, Chris Maki, Matthew McCollough, Matt Raible, and Mark Stewart. Special thanks to Joel Costigliola for helping to debug and correct the Spring web chapter. Stan Guillory was practically a contributing author given the number of corrections he posted to the book’s Get Satisfaction. Thank you Stan. Special thanks to Richard Coasby of Bamboo for acting as the provisional grammar consultant. Maven by Example ix Thanks to our contributing authors including Eric Redmond. Thanks to the following contributors who reported errors or even contributed fixes: Paco Soberón, Ray Krueger, Steinar Cook, Henning Saul, Anders Hammar, “george_007”, “ksangani”, Niko Mahle, Arun Kumar, Harold Shinsato, “mimil”, “-thrawn-”, Matt Gumbley, Andrew Janke. If you see your Get Satisfaction username in this list, and you would like it replaced with your real name, send an email to [email protected]. Special thanks to Grant Birchmeier for taking the time to proofread portions of the book and file extremely detailed feedback. How to Contribute The source code for this book can be found on the official GitHub repository and we accept pull requests with improvements. Maven by Example 1 / 155 Chapter 1 Introducing Apache Maven This book is an introduction to Apache Maven which uses a set of examples to demonstrate core concepts. Starting with a simple Maven project which contains a single class and a single unit test, this book slowly develops an enterprise multi-module project which interacts with a database, interacts with a remote API, and presents a simple web application. 1.1 Maven. What is it? The answer to this