Maven by Example

Tim O'Brien

Sonatype, Inc.

John Casey

Sonatype, Inc.

Brian Fox

Sonatype, Inc.

Jason Van Zyl

Sonatype, Inc.

Juven Xu

Sonatype, Inc.

Thomas Locher

Dan Fabulich

Eric Redmond

Larry Shatzer

Bruce Snyder

Abstract

Maven: By Example is an introduction to Apache Maven.


Copyright
Foreword: 0.5
1. Changes in Edition 0.5
Preface
1. How to Use this Book
2. Your Feedback
3. Font Conventions
4. Maven Writing Conventions
5. Acknowledgements
1. Introducing Apache Maven
1.1. Maven... What is it?
1.2. Convention Over Configuration
1.. A Common Interface
1.4. Universal Reuse through Maven Plugins
1.5. Conceptual Model of a "Project"
1.6. Is Maven an alternative to XYZ?
1.. Comparing Maven with Ant
2. Installing Maven
2.1. Verify your Java Installation
2.2. Downloading Maven
2.3. Installing Maven
2.3.1. Installing Maven on Mac OSX
2.3.1.1. Installing Maven on OSX using MacPorts
2.3.2. Installing Maven on Microsoft Windows
2.3.3. Installing Maven on Linux
2.3.4. Installing Maven on FreeBSD or OpenBSD
2.4. Testing a Maven Installation
2.5. Maven Installation Details
2.5.1. User-specific Configuration and Repository
2.5.2. Upgrading a Maven Installation
2.5.3. Upgrading from Maven 1.x to Maven 2.x
2.6. Uninstalling Maven
2.7. Getting Help with Maven
2.8. About the Apache Software License
3. A Simple Maven Project
3.1. Introduction
3.1.1. Downloading this Chapter's Example
3.2. Creating a Simple Project
3.3. Building a Simple Project
3.4. Simple Project Object Model
3.5. Core Concepts
3.5.1. Maven Plugins and Goals
3.5.2. Maven Lifecycle
3.5.3. Maven Coordinates
3.5.4. Maven Repositories
3.5.5. Maven's Dependency Management
3.5.6. Site Generation and Reporting
3.6. Summary
4. Customizing a Maven Project
4.1. Introduction
4.1.1. Downloading this Chapter's Example
4.2. Defining the Simple Weather Project
4.2.1. Yahoo! Weather RSS
4.3. Creating the Simple Weather Project
4.4. Customize Project Information
4.5. Add New Dependencies
4.6. Simple Weather Source Code
4.7. Add Resources
4.8. Running the Simple Weather Program
4.8.1. The Maven Exec Plugin
4.8.2. Exploring Your Project Dependencies
4.9. Writing Unit Tests
4.10. Adding Test-scoped Dependencies
4.11. Adding Unit Test Resources
4.12. Executing Unit Tests
4.12.1. Ignoring Test Failures
4.12.2. Skipping Unit Tests
4.13. Building a Packaged Command Line Application
4.13.1. Attaching the Assembly Goal to the Package Phase
5. A Simple Web Application
5.1. Introduction
5.1.1. Downloading this Chapter's Example
5.2. Defining the Simple Web Application
5.3. Creating the Simple Web Project
5.4. Configuring the Jetty Plugin
5.5. Adding a Simple Servlet
5.6. Adding J2EE Dependencies
5.7. Conclusion
6. A Multi-module Project
6.1. Introduction
6.1.1. Downloading this Chapter's Example
6.2. The Simple Parent Project
6.3. The Simple Weather Module
6.4. The Simple Web Application Module
6.5. Building the Multimodule Project
6.6. Running the Web Application
7. Multi-module Enterprise Project
7.1. Introduction
7.1.1. Downloading this Chapter's Example
7.1.2. Multi-module Enterprise Project
7.1.3. Technology Used in this Example
7.2. The Simple Parent Project
7.3. The Simple Model Module
7.4. The Simple Weather Module
7.5. The Simple Persist Module
7.6. The Simple Web Application Module
7.7. Running the Web Application
7.8. The Simple Command Module
7.9. Running the Simple Command
7.10. Conclusion
7.10.1. Programming to Interface Projects
8. Optimizing and Refactoring POMs
8.1. Introduction
8.2. POM Cleanup
8.3. Optimizing Dependencies
8.4. Optimizing Plugins
8.5. Optimizing with the Maven Dependency Plugin
8.6. Final POMs
8.7. Conclusion
A. Creative Commons License
A.1. Creative Commons BY-NC-ND 3.0 US License
B. Book Revision History
B.1. Changes in Edition 0.4
B.2. Changes in Edition 0.2.1
B.3. Changes in Edition 0.2
B.4. Changes in Edition 0.1
Index

List of Examples

1.1. A Simple Ant build.xml file
1.2. A Sample Maven pom.xml
3.1. Simple project's pom.xml file
4.1. Initial POM for the simple-weather project
4.2. POM for the simple-weather project with compiler configuration
4.3. Adding Organizational, Legal, and Developer Information to the pom.xml
4.4. Adding Dom4J, Jaxen, Velocity, and Log4J as Dependencies
4.5. Simple Weather's Weather Model Object
4.6. Simple Weather's Main Class
4.7. Simple Weather's YahooRetriever Class
4.8. Simple Weather's YahooParser Class
4.9. Simple Weather's WeatherFormatter Class
4.10. Simple Weather's Log4J Configuration File
4.11. Simple Weather's Output Velocity Template
4.12. Simple Weather's YahooParserTest Unit Test
4.13. Simple Weather's WeatherFormatterTest Unit Test
4.14. Adding a Test-scoped Dependency
4.15. Simple Weather's WeatherFormatterTest Expected Output
4.16. Simple Weather's YahooParserTest XML Input
4.17. Ignoring Unit Test Failures
4.18. Plugin Parameter Expressions
4.19. Skipping Unit Tests
4.20. Configuring the Maven Assembly Descriptor
4.21. Configuring attached Goal Execution during the package Lifecycle Phase
5.1. Initial POM for the simple-webapp project
5.2. POM for the simple-webapp project with compiler configuration
5.3. Configuring the Jetty Plugin
5.4. Contents of src/main/webapp/index.jsp
5.5. Contents of src/main/webapp/WEB-INF/web.xml
5.6. SimpleServlet Class
5.7. Mapping the Simple Servlet
5.8. Add the Servlet 2.4 Specification as a Dependency
5.9. Adding the JSP 2.0 Specification as a Dependency
6.1. simple-parent Project POM
6.2. simple-weather Module POM
6.3. The WeatherService class
6.4. simple-webapp Module POM
6.5. simple-webapp WeatherServlet
6.6. simple-webapp web.xml
7.1. simple-parent Project POM
7.2. simple-model pom.xml
7.3. Annotated Weather Model Object
7.4. simple-model's Condition model object.
7.5. simple-weather Module POM
7.6. WeatherService Class
7.7. Spring Application Context for the simple-weather Module
7.8. simple-persist POM
7.9. simple-persist's WeatherDAO Class
7.10. Spring Application Context for simple-persist
7.11. simple-persist hibernate.cfg.xml
7.12. POM for simple-webapp
7.13. simple-webapp WeatherController
7.14. weather.vm template rendered by WeatherController
7.15. simple-web HistoryController
7.16. history.vm rendered by the HistoryController
7.17. Spring Controller configuration weather-servlet.xml
7.18. web.xml for simple-webapp
7.19. POM for simple-command
7.20. The Main class for simple-command
7.21. WeatherFormatter renders weather data using a Velocity template
7.22. The weather.vm Velocity template
7.23. The history.vm Velocity template
8.1. Final POM for simple-parent
8.2. Final POM for simple-command
8.3. Final POM for simple-model
8.4. Final POM for simple-persist
8.5. Final POM for simple-weather
8.6. Final POM for simple-webapp