PDF Maven The Complete Reference Edition 0 7 Maven 2 2 Maven 3 0 Tim Obrien Ebook Full Chapter
PDF Maven The Complete Reference Edition 0 7 Maven 2 2 Maven 3 0 Tim Obrien Ebook Full Chapter
PDF Maven The Complete Reference Edition 0 7 Maven 2 2 Maven 3 0 Tim Obrien Ebook Full Chapter
https://textbookfull.com/product/zecri-stolen-warriors-4-1st-
edition-ella-maven-maven-ella/
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-
loucas/
https://textbookfull.com/product/introducing-maven-a-build-tool-
for-today-s-java-developers-second-edition-balaji-varanasi/
https://textbookfull.com/product/beginning-java-and-flex-
migrating-java-spring-hibernate-and-maven-developers-to-adobe-
flex-expert-s-voice-in-web-development-di-pisa-filippo/
Java: The Complete Reference, Eleventh Edition Herbert
Schildt
https://textbookfull.com/product/java-the-complete-reference-
eleventh-edition-herbert-schildt/
https://textbookfull.com/product/the-complete-rust-programming-
reference-guide-rahul-sharma/
https://textbookfull.com/product/robot-operating-system-ros-the-
complete-reference-volume-5-anis-koubaa/
https://textbookfull.com/product/mastering-delphi-programming-a-
complete-reference-guide-primoz-gabrijelcic/
https://textbookfull.com/product/the-essential-oils-complete-
reference-guide-over-250-recipes-for-natural-wholesome-
aromatherapy-k-g-stiles/
Edition 0.7 Maven 2.2
Maven 3.0
Contributing Authors:
Dan Fabulich
Eric Redmond
Bruce Snyder
Larry Shatzer
A Sonatype Open Book
Mountain View, CA
Copyright © 2010 Sonatype, Inc.
Nexus™, Nexus Professional™, and all Nexus-related logos are trademarks or registered
trademarks of Sonatype, Inc., in the United States and other countries. Java™ and all
Java-based trademarks and logos are trademarks or registered trademarks of Sun
Microsystems, Inc., in the United States and other countries. IBM® and WebSphere® are
trademarks or registered trademarks of International Business Machines, Inc., in the
United States and other countries. Eclipse™ is a trademark of the Eclipse Foundation,
Inc., in the United States and other countries. Apache and the Apache feather logo are
trademarks of The Apache Software Foundation.
Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts are claimed as trademarks. Where those designations appear in this book, and
Sonatype, Inc. was aware of a trademark claim, the designations have been printed in
caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher
and authors assume no responsibility for errors or omissions, or for damages resulting
from the use of the information contained herein.
Published by: For online information and ordering of this and other
Sonatype books, please visit www.sonatype.com. The
Sonatype, Inc. publisher offers discounts on this book when ordered in
800 W. El Camino Real quantity. For more information, please contact:
Suite 400 [email protected]
Mountain View, CA 94040.
"We have adopted Maven for all our software development projects and have
started using Nexus to better support our development processes. The support for
promotion and procurement workflows in Nexus Professional now expands Nexus
with a robust set of additional features which make it easier for us to maintain
consistency between our development, testing and production environments."
"At Intuit, we recognize that as builds grow and the teams who create them change
over time, swift, accurate repository management becomes critical. Nexus provides
a comprehensive, easy-to-use open source solution that lets teams and developers
track, search, organize and access build components."
http://www.sonatype.com/products/nexus
http://www.sonatype.com/training
Copyright ........................................................................................................................................................... xv
Foreword: 0.7 .................................................................................................................................................... xvii
1. Changes in Edition 0.7 ............................................................................................................................ xvii
Preface .............................................................................................................................................................. xix
1. How to Use this Book .............................................................................................................................. xix
2. Your Feedback ........................................................................................................................................ xix
3. Font Conventions .................................................................................................................................... xix
4. Maven Writing Conventions ....................................................................................................................... xx
5. Acknowledgements ................................................................................................................................... xx
1. Introducing Apache Maven .................................................................................................................................. 1
1.1. Maven... What is it? ................................................................................................................................. 1
1.2. Convention Over Configuration ................................................................................................................. 1
1.3. A Common Interface ................................................................................................................................ 2
1.4. Universal Reuse through Maven Plugins ...................................................................................................... 2
1.5. Conceptual Model of a "Project" ................................................................................................................ 3
1.6. Is Maven an alternative to XYZ? ............................................................................................................... 3
1.7. Comparing Maven with Ant ...................................................................................................................... 4
2. Installing Maven ................................................................................................................................................ 7
2.1. Verify your Java Installation ...................................................................................................................... 7
2.2. Downloading Maven ................................................................................................................................ 7
2.2.1. Downloading Maven 2 ................................................................................................................... 7
2.2.2. Downloading Maven 3 ................................................................................................................... 8
2.3. Installing Maven ..................................................................................................................................... 8
2.3.1. Installing Maven on Mac OSX ........................................................................................................ 8
2.3.2. Installing Maven on Microsoft Windows ........................................................................................... 9
2.3.3. Installing Maven on Linux .............................................................................................................. 9
2.3.4. Installing Maven on FreeBSD or OpenBSD ....................................................................................... 9
2.4. Testing a Maven Installation ...................................................................................................................... 9
2.5. Maven Installation Details ....................................................................................................................... 10
2.5.1. User-specific Configuration and Repository ...................................................................................... 10
2.5.2. Upgrading a Maven Installation ..................................................................................................... 11
2.5.3. Upgrading from Maven 1.x to Maven 2.x ........................................................................................ 11
2.6. Uninstalling Maven ................................................................................................................................ 11
2.7. Getting Help with Maven ........................................................................................................................ 12
2.8. About the Apache Software License .......................................................................................................... 12
3. The Project Object Model .................................................................................................................................. 15
3.1. Introduction .......................................................................................................................................... 15
3.2. The POM ............................................................................................................................................. 15
3.2.. The Super POM ........................................................................................................................... 16
3.2.2. The Simplest POM ...................................................................................................................... 19
3.2.3. The Effective POM ..................................................................................................................... 20
3.2.4. Real POMs ................................................................................................................................. 20
3.3. POM Syntax ......................................................................................................................................... 20
3.3.1. Project Versions .......................................................................................................................... 20
3.3.2. Property References ..................................................................................................................... 21
3.4. Project Dependencies .............................................................................................................................. 22
3.4.1. Dependency Scope ...................................................................................................................... 23
3.4.2. Optional Dependencies ................................................................................................................. 24
3.4.3. Dependency Version Ranges ......................................................................................................... 25
3.4.4. Transitive Dependencies ............................................................................................................... 26
3.4.5. Conflict Resolution ...................................................................................................................... 26
3.4.6. Dependency Management ............................................................................................................. 28
3.5. Project Relationships .............................................................................................................................. 29
3.5.1. More on Coordinates ................................................................................................................... 29
3.5.2. Project Inheritance ....................................................................................................................... 30
3.6. POM Best Practices ............................................................................................................................... 31
3.6.1. Grouping Dependencies ................................................................................................................ 31
3.6.2. Multi-module vs. Inheritance ......................................................................................................... 33
4. The Build Lifecycle .......................................................................................................................................... 37
4.1. Introduction .......................................................................................................................................... 37
4.1.1. Clean Lifecycle (clean) ................................................................................................................. 37
4.1.2. Default Lifecycle (default) ............................................................................................................ 39
4.1.3. Site Lifecycle (site) ...................................................................................................................... 40
4.2. Package-specific Lifecycles ..................................................................................................................... 41
4.2.1. JAR .......................................................................................................................................... 41
4.2.2. POM ......................................................................................................................................... 41
4.2.3. Maven Plugin ............................................................................................................................. 42
4.2.4. EJB ........................................................................................................................................... 42
4.2.5. WAR ........................................................................................................................................ 42
4.2.6. EAR .......................................................................................................................................... 43
4.2.7. Other Packaging Types ................................................................................................................. 43
4.3. Common Lifecycle Goals ........................................................................................................................ 44
4.3.1. Process Resources ....................................................................................................................... 44
4.3.2. Compile ..................................................................................................................................... 46
4.3.3. Process Test Resources ................................................................................................................. 47
4.3.4. Test Compile .............................................................................................................................. 47
4.3.5. Test .......................................................................................................................................... 48
4.3.6. Install ........................................................................................................................................ 48
4.3.7. Deploy ...................................................................................................................................... 49
5. Build Profiles .................................................................................................................................................. 51
5.1. What Are They For? .............................................................................................................................. 51
5.1.1. What is Build Portability .............................................................................................................. 51
5.1.2. Selecting an Appropriate Level of Portability ................................................................................... 52
5.2. Portability through Maven Profiles ............................................................................................................ 52
5.2.1. Overriding a Project Object Model ................................................................................................. 54
5.3. Profile Activation ................................................................................................................................... 54
5.3.1. Activation Configuration ............................................................................................................... 55
5.3.2. Activation by the Absence of a Property .......................................................................................... 56
5.4. Listing Active Profiles ............................................................................................................................ 57
5.5. Tips and Tricks ..................................................................................................................................... 57
5.5.1. Common Environments ................................................................................................................ 57
5.5.2. Protecting Secrets ........................................................................................................................ 58
5.5.3. Platform Classifiers ...................................................................................................................... 59
5.6. Summary .............................................................................................................................................. 61
6. Running Maven ............................................................................................................................................... 63
6.1. Maven Command Line Options ................................................................................................................ 63
6.1.1. Defining Properties ...................................................................................................................... 63
6.1.2. Getting Help ............................................................................................................................... 63
6.1.3. Using Build Profiles .................................................................................................................... 64
6.1.4. Displaying Version Information ..................................................................................................... 64
6.1.5. Running in Offline Mode .............................................................................................................. 65
6.1.6. Using a Custom POM or Custom Settings File ................................................................................. 65
6.1.7. Encrypting Passwords .................................................................................................................. 65
6.1.8. Dealing with Failure .................................................................................................................... 65
6.1.9. Controlling Maven's Verbosity ....................................................................................................... 66
6.1.10. Running Maven in Batch Mode .................................................................................................... 66
iv
6.1.11. Downloading and Verifying Dependencies ..................................................................................... 66
6.1.12. Controlling Plugin Updates .......................................................................................................... 67
6.1.13. Non-recursive Builds .................................................................................................................. 67
6.2. Using Advanced Reactor Options ............................................................................................................. 67
6.2.1. Advanced Reactor Options Example Project ..................................................................................... 67
6.2.2. Resuming Builds ......................................................................................................................... 69
6.2.3. Specifying a Subset of Projects ...................................................................................................... 69
6.2.4. Making a Subset of Projects .......................................................................................................... 69
6.2.5. Making Project Dependents ........................................................................................................... 70
6.2.6. Resuming a "make" build ............................................................................................................. 70
6.3. Using the Maven Help Plugin .................................................................................................................. 70
6.3.1. Describing a Maven Plugin ........................................................................................................... 71
7. Maven Configuration ........................................................................................................................................ 73
7.1. Configuring Maven Plugins ..................................................................................................................... 73
7.1.1. Plugin Configuration Parameters .................................................................................................... 73
7.1.2. Adding Plugin Dependencies ......................................................................................................... 75
7.1.3. Setting Global Plugin Parameters ................................................................................................... 76
7.1.4. Setting Execution Specific Parameters ............................................................................................. 76
7.1.5. Setting Default Command Line Execution Parameters ........................................................................ 77
7.1.6. Setting Parameters for Goals Bound to Default Lifecycle .................................................................... 77
8. Maven Assemblies ............................................................................................................................................ 79
8.1. Introduction .......................................................................................................................................... 79
8.2. Assembly Basics .................................................................................................................................... 79
8.2.1. Predefined Assembly Descriptors ................................................................................................... 80
8.2.2. Building an Assembly .................................................................................................................. 80
8.2.3. Assemblies as Dependencies ......................................................................................................... 82
8.2.4. Assembling Assemblies via Assembly Dependencies ......................................................................... 82
8.3. Overview of the Assembly Descriptor ....................................................................................................... 85
8.4. The Assembly Descriptor ........................................................................................................................ 86
8.4.1. Property References in Assembly Descriptors ................................................................................... 86
8.4.2. Required Assembly Information ..................................................................................................... 86
8.5. Controlling the Contents of an Assembly ................................................................................................... 87
8.5.1. Files Section ............................................................................................................................ 87
8.5.2. FileSets Section ....................................................................................................................... 88
8.5.3. Default Exclusion Patterns for fileSets ........................................................................................ 89
8.5.4. dependencySets Section ............................................................................................................ 90
8.5.5. moduleSets Sections .................................................................................................................. 97
8.5.6. Repositories Section ................................................................................................................... 101
8.5.7. Managing the Assembly’s Root Directory ...................................................................................... 101
8.5.8. componentDescriptors and containerDescriptorHandlers .................................................... 102
8.6. Best Practices ...................................................................................................................................... 102
8.6.1. Standard, Reusable Assembly Descriptors ...................................................................................... 103
8.6.2. Distribution (Aggregating) Assemblies .......................................................................................... 105
8.7. Summary ............................................................................................................................................ 108
9. Properties and Resource Filtering ...................................................................................................................... 109
9.1. Introduction ......................................................................................................................................... 109
9.2. Maven Properties ................................................................................................................................. 109
9.2.1. Maven Project Properties ............................................................................................................ 109
9.2.2. Maven Settings Properties ........................................................................................................... 111
9.2.3. Environment Variable Properties .................................................................................................. 111
9.2.4. Java System Properties ............................................................................................................... 111
9.2.5. User-defined Properties ............................................................................................................... 112
9.3. Resource Filtering ................................................................................................................................ 113
v
10. Site Generation ............................................................................................................................................. 117
10.1. Introduction ....................................................................................................................................... 117
10.2. Building a Project Site with Maven ....................................................................................................... 117
10.3. Customizing the Site Descriptor ............................................................................................................ 118
10.3.1. Customizing the Header Graphics ............................................................................................... 119
10.3.2. Customizing the Navigation Menu .............................................................................................. 120
10.4. Site Directory Structure ....................................................................................................................... 121
10.5. Writing Project Documentation ............................................................................................................. 121
10.5.1. APT Example .......................................................................................................................... 122
10.5.2. FML Example ......................................................................................................................... 122
10.6. Deploying Your Project Website ........................................................................................................... 123
10.6.1. Configuring Server Authentication .............................................................................................. 123
10.6.2. Configuring File and Directory Modes ......................................................................................... 124
10.7. Customizing Site Appearance ............................................................................................................... 124
10.7.1. Customizing the Site CSS .......................................................................................................... 124
10.7.2. Create a Custom Site Template ................................................................................................... 125
10.7.3. Reusable Website Skins ............................................................................................................ 128
10.7.4. Creating a Custom Theme CSS .................................................................................................. 129
10.8. Tips and Tricks .................................................................................................................................. 130
10.8.1. Inject XHTML into HEAD ........................................................................................................ 130
10.8.2. Add Links under Your Site Logo ................................................................................................ 130
10.8.3. Add Breadcrumbs to Your Site ................................................................................................... 131
10.8.4. Add the Project Version ............................................................................................................ 131
10.8.5. Modify the Publication Date Format and Location ......................................................................... 132
10.8.6. Using Doxia Macros ................................................................................................................. 132
11. Writing Plugins ............................................................................................................................................ 135
11.1. Introduction ....................................................................................................................................... 135
11.2. Programming Maven ........................................................................................................................... 135
11.2.1. What is Inversion of Control? .................................................................................................... 135
11.2.2. Introduction to Plexus ............................................................................................................... 136
11.2.3. Why Plexus? ........................................................................................................................... 136
11.2.4. What is a Plugin? ..................................................................................................................... 137
11.3. Plugin Descriptor ................................................................................................................................ 137
11.3.1. Top-level Plugin Descriptor Elements .......................................................................................... 138
11.3.2. Mojo Configuration .................................................................................................................. 139
11.3.3. Plugin Dependencies ................................................................................................................. 141
11.4. Writing a Custom Plugin ..................................................................................................................... 141
11.4.1. Creating a Plugin Project ........................................................................................................... 141
11.4.2. A Simple Java Mojo ................................................................................................................. 142
11.4.3. Configuring a Plugin Prefix ....................................................................................................... 143
11.4.4. Logging from a Plugin .............................................................................................................. 145
11.4.5. Mojo Class Annotations ............................................................................................................ 145
11.4.6. When a Mojo Fails ................................................................................................................... 147
11.5. Mojo Parameters ................................................................................................................................ 147
11.5.1. Supplying Values for Mojo Parameters ........................................................................................ 147
11.5.2. Multi-valued Mojo Parameters .................................................................................................... 149
11.5.3. Depending on Plexus Components .............................................................................................. 150
11.5.4. Mojo Parameter Annotations ...................................................................................................... 150
11.6. Plugins and the Maven Lifecycle ........................................................................................................... 151
11.6.1. Executing a Parallel Lifecycle .................................................................................................... 151
11.6.2. Creating a Custom Lifecycle ...................................................................................................... 152
11.6.3. Overriding the Default Lifecycle ................................................................................................. 153
12. Using Maven Archetypes ............................................................................................................................... 155
vi
12.1. Introduction to Maven Archetypes ......................................................................................................... 155
12.2. Using Archetypes ............................................................................................................................... 155
12.2.1. Using an Archetype from the Command Line ................................................................................ 155
12.2.2. Using the Interactive generate Goal ............................................................................................. 156
12.2.3. Using an Archetype from m2eclipse ............................................................................................ 157
12.3. Available Archetypes .......................................................................................................................... 157
12.3.1. Common Maven Archetypes ...................................................................................................... 158
12.3.2. Notable Third-Party Archetypes .................................................................................................. 158
12.4. Publishing Archetypes ......................................................................................................................... 160
13. Developing with Flexmojos ............................................................................................................................ 163
13.1. Introduction ....................................................................................................................................... 163
13.2. Configuring Build Environment for Flexmojos ......................................................................................... 163
13.2.1. Referencing a Repository with the Flex Framework ........................................................................ 163
13.2.2. Configuring Environment to Support Flex Unit Tests ...................................................................... 167
13.2.3. Adding FlexMojos to Your Maven Settings' Plugin Groups .............................................................. 168
13.3. Creating a Flex Mojos Project from an Archetype .................................................................................... 168
13.3.1. Creating a Flex Library ............................................................................................................. 168
13.3.2. Creating a Flex Application ....................................................................................................... 172
13.3.3. Creating a Multi-module Project: Web Application with a Flex Dependency ........................................ 175
13.4. The FlexMojos Lifecycle ..................................................................................................................... 180
13.4.1. The SWC Lifecycle .................................................................................................................. 181
13.4.2. The SWF Lifecycle .................................................................................................................. 182
13.5. FlexMojos Plugin Goals ...................................................................................................................... 182
13.5.1. Generating Actionscript Documentation ....................................................................................... 183
13.5.2. Compiling Flex Source ............................................................................................................. 184
13.5.3. Generating Flex Builder Project Files .......................................................................................... 184
13.6. FlexMojos Plugin Reports .................................................................................................................... 184
13.6.1. Generating Actionscript Documentation Report ............................................................................. 185
13.7. Developing and Customizing Flexmojos ................................................................................................. 186
13.7.1. Get the Flexmojos Source Code .................................................................................................. 186
14. Android Application Development with Maven .................................................................................................. 187
14.1. Introduction ....................................................................................................................................... 187
14.2. Configuring Build Environment for Android Development ......................................................................... 187
14.2.1. Installing the Android SDK ....................................................................................................... 187
14.2.2. Android artifact install into Maven repository ................................................................................ 187
14.2.3. Configuring Maven for usage of the Maven Android Plugin ............................................................. 188
14.3. Android Application Development with the Maven Android Plugin ............................................................. 189
14.3.1. HelloFlashlight Example ........................................................................................................... 189
14.3.2. Testing Android Application Code .............................................................................................. 190
14.3.3. Using Add Ons like the Google Maps Extension ............................................................................ 191
14.3.4. Multi Module Maven Android Projects ........................................................................................ 191
14.3.5. Using external dependencies ...................................................................................................... 191
14.4. Maven Android Plugin Details .............................................................................................................. 192
14.4.1. Maven Android Plugin Lifecycle ................................................................................................ 192
14.4.2. Maven Android Plugin Goals ..................................................................................................... 192
A. Appendix: Settings Details ............................................................................................................................... 195
A.1. Quick Overview .................................................................................................................................. 195
A.2. Settings Details ................................................................................................................................... 195
A.2.1. Simple Values .......................................................................................................................... 195
A.2.2. Servers .................................................................................................................................... 196
A.2.3. Mirrors .................................................................................................................................... 197
A.2.4. Proxies .................................................................................................................................... 197
A.2.5. Profiles .................................................................................................................................... 198
vii
A.2.6. Activation ................................................................................................................................ 198
A.2.7. Properties ................................................................................................................................ 199
A.2.8. Repositories ............................................................................................................................. 200
A.2.9. Plugin Repositories .................................................................................................................... 201
A.2.10. Active Profiles ........................................................................................................................ 201
A.2.11. Encrypting Passwords in Maven Settings ..................................................................................... 202
B. Appendix: Sun Specification Alternatives ........................................................................................................... 205
C. Creative Commons License .............................................................................................................................. 207
C.1. Creative Commons BY-NC-ND 3.0 US License ........................................................................................ 207
D. Book Revision History .................................................................................................................................... 211
D.1. Changes in Edition 0.6 ......................................................................................................................... 211
D.2. Changes in Edition 0.5 ......................................................................................................................... 211
D.3. Changes in Edition 0.4 ......................................................................................................................... 211
D.4. Changes in Edition 0.2.1 ....................................................................................................................... 212
D.5. Changes in Edition 0.2 ......................................................................................................................... 212
D.6. Changes in Edition 0.1 ......................................................................................................................... 212
Index ................................................................................................................................................................ 215
viii
List of Figures
2.1. Downloading Maven 2 from the Apache Maven web site ....................................................................................... 7
2.2. Downloading Maven 3 from the Apache Maven web site ....................................................................................... 8
3.1. The Project Object Model ............................................................................................................................... 16
3.2. The Super POM is always the base Parent ......................................................................................................... 19
3.3. Project Inheritance for a-parent and project-a ...................................................................................................... 30
3.4. maven-book Multi-module vs. Inheritance .......................................................................................................... 33
3.5. Enterprise Multi-module vs. Inheritance ............................................................................................................. 34
6.1. Directory Structure of Sample Multi-module Project ............................................................................................ 68
6.2. Dependencies within Sample Multi-module Project .............................................................................................. 68
10.1. Simple Generated Maven Site ....................................................................................................................... 118
10.2. Customized Sample Project Web Site ............................................................................................................. 119
13.1. Adding a Proxy Repository to Sonatype Nexus ................................................................................................ 164
13.2. Configuring the Sonatype Flexmojos Proxy Repository ..................................................................................... 165
13.3. Adding the Sonatype Flexmojos Proxy to the Public Repositories Group .............................................................. 166
13.4. Flexmojo Library Archetype File Structure ..................................................................................................... 169
13.5. Directory Structure for Flex Application Archetype .......................................................................................... 173
13.6. Directory Structure for Flex Multimodule Archetype ......................................................................................... 176
13.7. The FlexMojos SWC Lifecycle ..................................................................................................................... 181
13.8. The FlexMojos SWF Lifecycle ..................................................................................................................... 182
13.9. Actionscript Documentation Generated by the FlexMojos Plugin ......................................................................... 183
13.10. Actionscript Documentation Report on Maven Site ......................................................................................... 185
13.11. Flexmojos Subversion Repository ................................................................................................................ 186
A.1. Storing Unencrypted Passwords in Maven Settings ............................................................................................ 202
A.2. Storing Encrypted Passwords in Maven Settings ............................................................................................... 202
List of Examples
1.1. A Simple Ant build.xml file .............................................................................................................................. 4
1.2. A Sample Maven pom.xml ................................................................................................................................ 5
3.1. The Super POM ............................................................................................................................................ 16
3.2. The Simplest POM ........................................................................................................................................ 19
3.3. Project Dependencies ...................................................................................................................................... 22
3.4. Declaring Optional Dependencies ..................................................................................................................... 24
3.5. Specifying a Dependency Range: JUnit 3.8 - JUnit 4.0 ......................................................................................... 25
3.6. Specifying a Dependency Range: JUnit <= 3.8.1 ................................................................................................. 25
3.7. Excluding a Transitive Dependency .................................................................................................................. 26
3.8. Excluding and Replacing a Transitive Dependency .............................................................................................. 27
3.9. Defining Dependency Versions in a Top-level POM ............................................................................................ 28
3.10. Project Inheritance ........................................................................................................................................ 30
3.11. Consolidating Dependencies in a Single POM Project ......................................................................................... 32
3.12. Declaring a Dependency on a POM ................................................................................................................. 32
4.1. Triggering a Goal on pre-clean ......................................................................................................................... 37
4.2. Customizing Behavior of the Clean Plugin ......................................................................................................... 39
4.3. Custom Packaging Type for Adobe Flex (SWF) .................................................................................................. 43
4.4. Using Properties in Project Resources ............................................................................................................... 44
4.5. default.properties in src/main/filters ................................................................................................................... 45
4.6. Filter Resources (Replacing Properties) .............................................................................................................. 45
4.7. Configuring Additional Resource Directories ...................................................................................................... 45
4.8. Filtering Script Resources ................................................................................................................................ 46
4.9. Setting the Source and Target Versions for the Compiler Plugin ............................................................................. 46
4.10. Overriding the Default Source Directory .......................................................................................................... 47
4.11. Overriding the Location of Test Source and Output ............................................................................................ 48
4.12. Configuring Surefire to Ignore Test Failures ..................................................................................................... 48
5.1. Using a Maven Profile to Override Production Compiler Settings ........................................................................... 52
5.2. Elements Allowed in a Profile ......................................................................................................................... 54
5.3. Dynamic Inclusion of Submodules Using Profile Activation .................................................................................. 55
5.4. Profile Activation Parameters: JDK Version, OS Parameters, and Properties ............................................................. 56
5.5. Activating Profiles in the Absence of a Property ................................................................................................. 56
5.6. Project Profile Activated by setting environment.type to 'dev' ................................................................................ 57
5.7. Storing Secrets in a User-specific Settings Profile ................................................................................................ 59
5.8. Qualifying Artifacts with Platform Activated Project Profiles ................................................................................. 59
5.9. Qualifying Artifacts with Platform Activated Project Profiles and Variable Substitution .............................................. 60
5.10. Depending on a Qualified Artifact ................................................................................................................... 61
6.1. Maven Version Information ............................................................................................................................. 65
6.2. Order of Project Builds in Maven Reactor .......................................................................................................... 69
7.1. Adding Dependencies to a Plugin ..................................................................................................................... 75
7.2. Configuring a Maven Plugin ............................................................................................................................ 76
7.3. Setting Configuration Parameters in an Execution ................................................................................................ 76
7.4. Configuring Plugin Parameters for Command Line Execution ................................................................................ 77
7.5. Setting a Parameter for a Default Goal Execution ................................................................................................ 77
7.6. Setting Two Default Goal Plugin Configuration Parameters ................................................................................... 78
8.1. Assembly Descriptor for Executable JAR ........................................................................................................... 81
8.2. Configuring the project assembly in top-level POM ............................................................................................. 83
8.3. Activating the Assembly Plugin Configuration in Child Projects ............................................................................. 83
8.4. POM for the Assembly Bundling Project ........................................................................................................... 84
8.5. Required Assembly Descriptor Elements ............................................................................................................ 86
8.6. Including a JAR file in an Assembly using files ............................................................................................... 87
8.7. Including Files with fileSet .......................................................................................................................... 88
8.8. Definition of Default Exclusion Patterns from Plexus Utils .................................................................................... 90
8.9. Defining Dependency Sets Using Scope ............................................................................................................. 93
8.10. Using Dependency Excludes and Includes in dependencySets ........................................................................... 94
8.11. Excluding Files from a Dependency Unpack ..................................................................................................... 96
8.12. Includes and Excluding Modules with a moduleSet .......................................................................................... 98
8.13. Including JavaDoc from Modules in an Assembly ............................................................................................ 100
8.14. Including Module Artifacts and Dependencies in an Assembly ............................................................................ 100
9.1. User-defined Properties in a POM ................................................................................................................... 112
9.2. User-defined Properties in a Profile in a POM ................................................................................................... 112
9.3. Referencing Maven Properties from a Resource ................................................................................................. 113
9.4. Defining Variables and Activating Resource Filtering ......................................................................................... 114
10.1. An Initial Site Descriptor ............................................................................................................................. 118
10.2. Adding a Banner Left and Banner Right to Site Descriptor ................................................................................ 119
10.3. Creating Menu Items in a Site Descriptor ....................................................................................................... 120
10.4. Adding a Link to the Site Menu .................................................................................................................... 120
10.5. APT Document .......................................................................................................................................... 122
10.6. FAQ Markup Language Document ................................................................................................................ 122
10.7. Configuring Site Deployment ....................................................................................................................... 123
10.8. Storing Server Authentication in User-specific Settings ..................................................................................... 123
10.9. Configuring File and Directory Modes on Remote Servers ................................................................................. 124
10.10. Customizing the Page Template in a Project's POM ........................................................................................ 127
10.11. Adding a Menu Item to a Site Descriptor ...................................................................................................... 128
10.12. Configuring a Custom Site Skin in Site Descriptor .......................................................................................... 130
10.13. Injecting HTML into the HEAD element ...................................................................................................... 130
10.14. Adding Links Under Your Site Logo ............................................................................................................ 130
10.15. Configuring the Site's Breadcrumbs .............................................................................................................. 131
10.16. Positioning the Version Information ............................................................................................................. 131
10.17. Positioning the Publish Date ....................................................................................................................... 132
10.18. Configuring the Publish Date Format ........................................................................................................... 132
10.19. Output of the Snippet Macro in XHTML ...................................................................................................... 132
11.1. Plugin Descriptor ........................................................................................................................................ 137
11.2. A Plugin Project's POM .............................................................................................................................. 141
11.3. A Simple EchoMojo ................................................................................................................................... 142
11.4. Maven Metadata for the Maven Plugin Group ................................................................................................. 143
11.5. Customizing the Plugin Groups in Maven Settings ........................................................................................... 144
11.6. Configuring a Plugin Prefix .......................................................................................................................... 144
11.7. A Plugin with Multi-valued Parameters .......................................................................................................... 149
11.8. Depending on a Plexus Component ............................................................................................................... 150
11.9. Define a Custom Lifecycle in lifecycles.xml .................................................................................................... 152
11.10. Forking a Custom Lifecycle from a Mojo ..................................................................................................... 152
11.11. Overriding the Default Lifecycle ................................................................................................................. 153
11.12. Configuring a Plugin as an Extension ........................................................................................................... 153
12.1. Archetype Catalog for the Apache Cocoon Project ........................................................................................... 160
13.1. Adding a Reference to Sonatype's FlexMojos Repository in a POM ..................................................................... 163
13.2. Settings XML for Local Nexus Instance ......................................................................................................... 166
13.3. Adding Sonatype Plugins to Maven Settings ................................................................................................... 168
13.4. Project Object Model for Flex Library Archetype ............................................................................................. 169
13.5. Flex Library Archetype's Sample App Class .................................................................................................... 171
13.6. Unit Test for Library Archetype's App Class ................................................................................................... 171
13.7. POM for Flex Application Archetype ............................................................................................................. 173
13.8. Sample Application Main.mxml .................................................................................................................... 175
13.9. Unit Test for Main.mxml ............................................................................................................................. 175
xii
13.10. Top-level POM Created by Modular Web Application Archetype ...................................................................... 176
13.11. swc Module POM ..................................................................................................................................... 176
13.12. swf module POM ...................................................................................................................................... 178
13.13. war module POM ..................................................................................................................................... 179
13.14. Setting Plugin Extensions to True for Custom Flex Lifecycle ............................................................................ 181
13.15. Customizing the Compiler Plugin ................................................................................................................ 184
13.16. Configuring the Actionscript Documentation Report ........................................................................................ 185
13.17. Configuring the asdoc-report ....................................................................................................................... 185
14.1. Snippet for settings.xml for the repository server access credentials ..................................................................... 188
14.2. Snippet for settings.xml to enable short plugin name usage ................................................................................ 188
14.3. The HelloFlashlight pom.xml file .................................................................................................................. 189
14.4. Adding the test folder to the build configuration .............................................................................................. 190
14.5. The dependency to the Google Maps API ....................................................................................................... 191
A.1. Overview of top-level elements in settings.xml ................................................................................................. 195
A.2. Simple top-level elements in settings.xml ........................................................................................................ 195
A.3. Server configuration in settings.xml ................................................................................................................ 196
A.4. Mirror configuration in settings.xml ................................................................................................................ 197
A.5. Proxy configuration in settings.xml ................................................................................................................. 197
A.6. Defining Activation Parameters in settings.xml ................................................................................................. 198
A.7. Setting the ${user.install} property in settings.xml ............................................................................................. 199
A.8. Repository Configuration in settings.xml ......................................................................................................... 200
A.9. Plugin Repositories in settings.xml ................................................................................................................. 201
A.10. Setting active profiles in settings.xml ............................................................................................................ 201
A.11. settings-security.xml with Master Password .................................................................................................... 203
A.12. Storing an Encrypted Password in Maven Settings (~/.m2/settings.xml) ............................................................... 203
A.13. Configuring Relocation of the Master Password .............................................................................................. 203
B.1. Adding JTA 1.0.1B to a Maven Project ........................................................................................................... 206
xiii
Copyright
Copyright © 2010 Sonatype, Inc.
Online version published by Sonatype, Inc., 800 W. El Camino Real, Suite 400, Mountain View, CA, 94040.
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States license. For
more information about this license, see http://creativecommons.org/licenses/by-nc-nd/3.0/us/.
Nexus™, Nexus Professional™, and all Nexus-related logos are trademarks or registered trademarks of Sonatype, Inc., in the United
States and other countries.
Java™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United
States and other countries.
IBM® and WebSphere® are trademarks or registered trademarks of International Business Machines, Inc., in the United States and
other countries.
Eclipse™ is a trademark of the Eclipse Foundation, Inc., in the United States and other countries.
Apache and the Apache feather logo are trademarks of The Apache Software Foundation.
Linux® is the registered trademark of Linus Torvalds in the U.S. and other countries.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those
designations appear in this book, and Sonatype, Inc. was aware of a trademark claim, the designations have been printed in caps
or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors
or omissions, or for damages resulting from the use of the information contained herein.
Foreword: 0.7
In this edition of Maven: The Complete Reference, we are beginning our transition to Maven 3.0. Maven 3.0 is approaching the end
of a lengthy beta testing period, and it has already been in wide use by very large, mission-critical projects. We feel the Maven 3.0
is ready to for use, and this edition has added some supplemental information for developers who are looking to use Maven 3.0.
We've had some great feedback so far, please keep it coming. Your feedback is greatly appreciated, send it to [email protected].
To keep yourself informed of updates, read the book blog at: http://blogs.sonatype.com/book. Everyone at Sonatype has had a hand
in this version of the book, so the author is officially "Sonatype".
Please report any bugs or issues on this book's GetSatisfaction page, here: http://www.getsatisfaction.com/sonatype/products/
sonatype_maven_the_complete_reference.
September, 2010
• Added Maven 3.0 to the cover of the ePub and PDF. (MVNREF-1632)
• Fixed a typo in Section 3.6.2.1, “Simple Project”, "change" -> "chance" (MVNREF-1504)
• Fixed some wording issues with optional dependencies in Section 3.4.2, “Optional Dependencies”. (MVNREF-1375)
• Adding a missing label to Example 7.2, “Configuring a Maven Plugin”, Example 7.4, “Configuring Plugin Parameters
for Command Line Execution”, and Example 7.5, “Setting a Parameter for a Default Goal Execution”. (MVNREF-1656,
MVNREF-1497)
• Removed blank label attribute from Section 1.7, “Comparing Maven with Ant”. (MVNREF-1668)
• Changed the tense of a cross-reference in Section 3.2.3, “The Effective POM”. (MVNREF-14710)
• Changed the name of the property in Section 5.3.1, “Activation Configuration” for clarity. (MVNREF-12911)
• Clarified language about grouping dependencies in Section 3.6.1, “Grouping Dependencies”. (MVNREF-13412)
Thanks to Florian Brunner for reporting a typo in the book. Thanks to David M. Karr for suggesting improvements to the description
of optional dependencies. Thanks to Mark Vedder for finding run-on sentences in the book. Thanks to Cedric Girard for finding
examples with missing labels. Thanks to Benjamin Bentmann for identifying an issue in the profiles chapter.
1
mailto:[email protected]
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 3rd 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, Sonatype1, 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.
This book introduces Maven by developing some real examples and walking you through the structure of those examples providing
motivation and explanation along the way.
2. Your Feedback
We didn't write this book so we could send off a Word document to our publisher and go to a launch party to congratulate ourselves on
a job well done. This book isn't "done"; in fact, this book will never be completely "done". The subject it covers is constantly changing
and expanding, and we consider this work an ongoing conversation with the community. Publishing the book means that the real work
has just begun, and you, as a reader, play a pivotal role to helping to maintain and improve this book. If you see something in this book
that is wrong: a spelling mistake, some bad code, a blatant lie, then you should tell us, send us an email at: [email protected].
The ongoing relevance of this book depends upon your feedback. We want to know what works and what doesn't work. We want to
know if there is any information you couldn't understand. We especially want to know if you think that the book is awful. Positive
or negative comments are all welcome. Of course, we reserve the right to disagree, but all feedback will be rewarded with a gracious
response.
3. Font Conventions
This book follows certain conventions for font usage. Understanding these conventions up-front makes it easier to use this book.
Italic
Used for filenames, file extensions, URLs, application names, emphasis, and new terms when they are first introduced.
Constant width
Used for Java class names, methods, variables, properties, data types, database elements, and snippets of code that appear
in text.
1
http://www.sonatype.com
2
mailto:[email protected]
Constant width italic
Used to annotate output.
Compiler plugin
Maven plugins are capitalized.
create goal
Maven goal names are displayed in a constant width font.
"plugin"
While "plug-in" (with hyphen) would be the grammatically correct form, this book writes the term as "plugin" both because
it is easier to read and write and because it is a standard throughout the Maven community.
Maven Lifecycle, Maven Standard Directory Layout, Maven Plugin, Project Object Model
Core Maven concepts are capitalized whenever they are being referenced in the text.
goalParameter
A Maven goal parameter is displayed in a constant width font.
compile phase
Lifecycle phases are displayed in a constant width font.
5. 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, 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.
Thanks to the following contributors who reported errors either in an email or using the Get Satisfaction site: Paco Soberón, Ray
Krueger, Steinar Cook, Henning Saul, Anders Hammar, "george_007", "ksangani", Niko Mahle, Arun Kumar, Harold Shinsato,
"mimil", "-thrawn-", Matt Gumbley. 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 via
GetSatisfaction.
3
mailto:[email protected]
xx
Chapter 1. Introducing Apache Maven
Although there are a number of references for Maven online, there is no single, well-written narrative for introducing Maven that
can serve as both an authoritative reference and an introduction. What we’ve tried to do with this effort is provide such a narrative
coupled with useful reference material.
A more formal definition of Apache Maven1: Maven is a project management tool which encompasses a project object model, a
set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a
lifecycle. When you use Maven, you describe your project using a well-defined project object model, Maven can then apply cross-
cutting logic from a set of shared (or custom) plugins.
Don't let the fact that Maven is a "project management" tool scare you away. If you were just looking for a build tool, Maven will
do the job. In fact, the first few chapters of this book will deal with the most common use case: using Maven to build and distribute
your project.
Maven incorporates this concept by providing sensible default behavior for projects. Without customization, source code is assumed to
be in ${basedir}/src/main/java and resources are assumed to be in ${basedir}/src/main/resources. Tests are assumed
to be in ${basedir}/src/test, and a project is assumed to produce a JAR file. Maven assumes that you want the compile byte
code to ${basedir}/target/classes and then create a distributable JAR file in ${basedir}/target. While this might seem
trivial, consider the fact that most Ant-based builds have to define the locations of these directories. Ant doesn't ship with any built-
in idea of where source code or resources might be in a project; you have to supply this information. Maven's adoption of convention
over configuration goes farther than just simple directory locations, Maven's core plugins apply a common set of conventions for
compiling source code, packaging distributions, generating web sites, and many other processes. Maven's strength comes from the
fact that it is "opinionated", it has a defined life-cycle and a set of common plugins that know how to build and assemble software.
If you follow the conventions, Maven will require almost zero effort - just put your source in the correct directory, and Maven will
take care of the rest.
One side-effect of using systems that follow "convention over configuration" is that end-users might feel that they are forced to use a
particular methodology or approach. While it is certainly true that Maven has some core opinions that shouldn't be challenged, most
of the defaults can be customized. For example, the location of a project's source code and resources can be customized, names of
JAR files can be customized, and through the development of custom plugins, almost any behavior can be tailored to your specific
1
http://maven.apache.org
2
http://www.rubyonrails.org/
environment's requirements. If you don't care to follow convention, Maven will allow you to customize defaults in order to adapt
to your specific requirements.
Today, most open source developers have used or are currently using Maven to manage new software projects. This transition is less
about developers moving from one build tool to another and more about developers starting to adopt a common interface for project
builds. As software systems have become more modular, build systems have become more complex, and the number of projects
has sky-rocketed. Before Maven, when you wanted to check out a project like Apache ActiveMQ5 or Apache ServiceMix6 from
Subversion and build it from source, you really had to set aside about an hour to figure out the build system for each particular project.
What does the project need to build? What libraries do I need to download? Where do I put them? What goals can I execute in the
build? In the best case, it took a few minutes to figure out a new project's build, and in the worst cases (like the old Servlet API
implementation in the Jakarta Project), a project's build was so difficult it would take multiple hours just to get to the point where a
new contributor could edit source and compile the project. These days, you check it out from source, and you run mvn install.
While Maven provides an array of benefits including dependency management and reuse of common build logic through plugins,
the core reason why it has succeeded is that it has defined a common interface for building software. When you see that a project like
Apache ActiveMQ7 uses Maven, you can assume that you'll be able to check it out from source and build it with mvn install without
much hassle. You know where the ignition keys goes, you know that the gas pedal is on the right-side, and the brake is on the left.
The Maven Surefire plugin is the plugin that is responsible for running unit tests. Somewhere between version 1.0 and the version
that is in wide use today someone decided to add support for the TestNG unit testing framework in addition to the support for JUnit.
This upgrade happened in a way that didn't break backwards compatibility. If you were using the Surefire plugin to compile and
execute JUnit 3 unit tests, and you upgraded to the most recent version of the Surefire plugin, your tests continued to execute without
fail. But, you gained new functionality, if you want to execute unit tests in TestNG you now have that ability. You also gained the
ability to run annotated JUnit 4 unit tests. You gained all of these capabilities without having to upgrade your Maven installation or
3
http://turbine.apache.org/
4
http://tomcat.apache.org
5
http://activemq.apache.org
6
http://servicemix.apache.org
7
http://wicket.apache.org
2
install new software. Most importantly, nothing about your project had to change aside from a version number for a plugin a single
Maven configuration file called the Project Object Model (POM).
It is this mechanism that affects much more than the Surefire plugin. Maven has plugins for everything from compiling Java code, to
generating reports, to deploying to an application server. Maven has abstracted common build tasks into plugins which are maintained
centrally and shared universally. If the state-of-the-art changes in any area of the build, if some new unit testing framework is released
or if some new tool is made available, you don't have to be the one to hack your project's custom build system to support it. You benefit
from the fact that plugins are downloaded from a remote repository and maintained centrally. This is what is meant by universal
reuse through Maven plugins.
Dependency Management
Because a project is defined by a unique set of coordinates consisting of a group identifier, an artifact identifier, and a version,
projects can now use these coordinates to declare dependencies.
Remote Repositories
Related to dependency management, we can use the coordinates defined in the Maven Project Object Model (POM) to create
repositories of Maven artifacts.
This is the central point that makes all of the Maven vs. Ant, Maven vs. Buildr, Maven vs. Gradle arguments irrelevant. Maven isn't
totally defined by the mechanics of your build system. It isn't about scripting the various tasks in your build as much as it is about
encouraging a set of standards, a common interface, a life-cycle, a standard repository format, a standard directory layout, etc. It
certainly isn't about what format the POM happens to be in (XML vs. YAML vs. Ruby). Maven is much larger than that, and Maven
refers to much more than the tool itself. When this book talks of Maven, it is referring to the constellation of software, systems, and
8
http://ant.apache.org
3
standards that support it. Buildr, Ivy, Gradle, all of these tools interact with the repository format that Maven helped create, and you
could just as easily use a repository manager like Nexus to support a build written entirely in Ant.
While Maven is an alternative to many of these tools, the community needs to evolve beyond seeing technology as a zero-sum game
between unfriendly competitors in a competition for users and developers. This might be how large corporations relate to one another,
but it has very little relevance to the way that open source communities work. The headline "Who's winning? Ant or Maven?" isn't
very constructive. If you force us to answer this question, we're definitely going to say that Maven is a superior alternative to Ant
as a foundational technology for a build; at the same time, Maven's boundaries are constantly shifting and the Maven community is
constantly trying to seek out new ways to become more ecumenical, more inter-operable, more cooperative. The core tenets of Maven
are declarative builds, dependency management, repository managers, universal reuse through plugins, but the specific incarnation
of these ideas at any given moment is less important than the sense that the open source community is collaborating to reduce the
inefficiency of "enterprise-scale builds".
Ant excels at build process, it is a build system modeled after make with targets and dependencies. Each target consists of a set of
instructions which are coded in XML. There is a copy task and a javac task as well as a jar task. When you use Ant, you supply
Ant with specific instructions for compiling and packaging your output. Look at the following example of a simple build.xml file:
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
4
Another random document with
no related content on Scribd:
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.