Maven: the Complete Reference I
Total Page:16
File Type:pdf, Size:1020Kb
Maven: The Complete Reference i Maven: The Complete Reference Ed. 1.0 Maven: The Complete Reference ii Contents 1 Introducing Apache Maven1 1.1 Maven. What is it?....................................1 1.2 Convention Over Configuration...............................2 1.3 A Common Interface....................................3 1.4 Universal Reuse through Maven Plugins..........................3 1.5 Conceptual Model of a "Project"..............................4 1.6 Is Maven an alternative to XYZ?..............................5 1.7 Comparing Maven with Ant................................6 2 Installing Maven 10 2.1 Verify your Java Installation................................ 10 2.2 Downloading Maven.................................... 11 2.3 Installing Maven...................................... 11 Maven: The Complete Reference iii 2.3.1 Installing Maven on Linux, BSD or Mac OSX................... 12 2.3.2 Installing Maven on Microsoft Windows...................... 12 2.4 Testing a Maven Installation................................ 13 2.5 Maven Installation Details................................. 13 2.5.1 User-specific Configuration and Repository.................... 14 2.5.2 Upgrading a Maven Installation.......................... 15 2.6 Uninstalling Maven..................................... 15 2.7 Getting Help with Maven.................................. 15 2.8 About the Apache Software License............................ 16 3 The Project Object Model 18 3.1 Introduction......................................... 18 3.2 The POM.......................................... 18 3.2.1 The Super POM.................................. 21 3.2.2 The Simplest POM................................. 25 3.2.3 The Effective POM................................. 26 3.2.4 Real POMs..................................... 26 3.3 POM Syntax......................................... 26 3.3.1 Project Versions.................................. 27 Maven: The Complete Reference iv 3.3.1.1 Version Build Numbers......................... 27 3.3.1.2 SNAPSHOT Versions.......................... 28 3.3.2 Property References................................ 28 3.4 Project Dependencies.................................... 30 3.4.1 Dependency Scope................................. 31 3.4.2 Optional Dependencies............................... 32 3.4.3 Dependency Version Ranges............................ 34 3.4.4 Transitive Dependencies.............................. 35 3.4.4.1 Transitive Dependencies and Scope................... 35 3.4.5 Conflict Resolution................................. 36 3.5 Project Relationships.................................... 40 3.5.1 More on Coordinates................................ 40 3.5.2 Project Inheritance................................. 42 3.6 POM Best Practices..................................... 44 3.6.1 Grouping Dependencies.............................. 45 3.6.2 Multi-module vs. Inheritance........................... 47 3.6.2.1 Simple Project.............................. 47 3.6.2.2 Multi-module Enterprise Project.................... 49 Maven: The Complete Reference v 4 The Build Lifecycle 52 4.1 Introduction......................................... 52 4.1.1 Clean Lifecycle (clean)............................... 53 4.1.2 Default Lifecycle (default)............................. 56 4.1.3 Site Lifecycle (site)................................. 57 4.2 Package-specific Lifecycles................................. 58 4.2.1 JAR......................................... 59 4.2.2 POM........................................ 59 4.2.3 Maven Plugin.................................... 60 4.2.4 EJB......................................... 60 4.2.5 WAR........................................ 61 4.2.6 EAR........................................ 61 4.2.7 Other Packaging Types............................... 62 4.3 Common Lifecycle Goals.................................. 63 4.3.1 Process Resources................................. 63 4.3.2 Compile....................................... 67 4.3.3 Process Test Resources............................... 68 4.3.4 Test Compile.................................... 68 Maven: The Complete Reference vi 4.3.5 Test......................................... 69 4.3.6 Install........................................ 70 4.3.7 Deploy....................................... 70 5 Build Profiles 71 5.1 What Are They For?.................................... 71 5.1.1 What is Build Portability.............................. 71 5.1.1.1 Non-Portable Builds........................... 72 5.1.1.2 Environment Portability......................... 72 5.1.1.3 Organizational (In-House) Portability.................. 72 5.1.1.4 Wide (Universal) Portability....................... 73 5.1.2 Selecting an Appropriate Level of Portability................... 73 5.2 Portability through Maven Profiles............................. 74 5.2.1 Overriding a Project Object Model......................... 76 5.3 Profile Activation...................................... 77 5.3.1 Activation Configuration.............................. 79 5.3.2 Activation by the Absence of a Property...................... 80 5.4 Listing Active Profiles................................... 81 5.5 Tips and Tricks....................................... 81 Maven: The Complete Reference vii 5.5.1 Common Environments.............................. 82 5.5.2 Protecting Secrets.................................. 84 5.5.3 Platform Classifiers................................. 85 5.6 Summary.......................................... 87 6 Running Maven 88 6.1 Maven Command Line Options............................... 88 6.1.1 Defining Properties................................. 88 6.1.2 Getting Help.................................... 89 6.1.3 Using Build Profiles................................ 90 6.1.4 Displaying Version Information.......................... 90 6.1.5 Running in Offline Mode.............................. 91 6.1.6 Using a Custom POM or Custom Settings File.................. 91 6.1.7 Encrypting Passwords............................... 91 6.1.8 Dealing with Failure................................ 92 6.1.9 Controlling Maven’s Verbosity........................... 92 6.1.10 Running Maven in Batch Mode.......................... 93 6.1.11 Downloading and Verifying Dependencies..................... 93 6.1.12 Non-recursive Builds................................ 94 Maven: The Complete Reference viii 6.2 Using Advanced Reactor Options.............................. 94 6.2.1 Advanced Reactor Options Example Project.................... 95 6.2.2 Resuming Builds.................................. 97 6.2.3 Specifying a Subset of Projects........................... 97 6.2.4 Making a Subset of Projects............................ 98 6.2.5 Making Project Dependents............................ 98 6.2.6 Resuming a "make" build............................. 99 6.3 Using the Maven Help Plugin................................ 99 6.3.1 Describing a Maven Plugin............................. 100 7 Maven Configuration 103 7.1 Configuring Maven Plugins................................. 103 7.1.1 Plugin Configuration Parameters.......................... 103 7.1.2 Adding Plugin Dependencies............................ 107 7.1.3 Setting Global Plugin Parameters......................... 108 7.1.4 Setting Execution Specific Parameters....................... 108 7.1.5 Setting Default Command Line Execution Parameters.............. 109 7.1.6 Setting Parameters for Goals Bound to Default Lifecycle............. 110 8 Maven Assemblies 112 Maven: The Complete Reference ix 8.1 Introduction......................................... 112 8.2 Assembly Basics...................................... 113 8.2.1 Predefined Assembly Descriptors......................... 114 8.2.2 Building an Assembly............................... 114 8.2.3 Assemblies as Dependencies............................ 117 8.2.4 Assembling Assemblies via Assembly Dependencies............... 118 8.3 Overview of the Assembly Descriptor........................... 122 8.4 The Assembly Descriptor.................................. 123 8.4.1 Property References in Assembly Descriptors................... 123 8.4.2 Required Assembly Information.......................... 123 8.5 Controlling the Contents of an Assembly.......................... 125 8.5.1 Files Section................................... 125 8.5.2 FileSets Section................................ 126 8.5.3 Default Exclusion Patterns for........................... 128 8.5.4 dependencySets Section............................ 129 8.5.4.1 Customizing Dependency Output Location............... 131 8.5.4.2 Interpolation of Properties in Dependency Output........... 132 8.5.4.3 Including and Excluding Dependencies by Scope........... 133 Maven: The Complete Reference x 8.5.4.4 Fine Tuning: Dependency Includes and Excludes........... 135 8.5.4.5 Transitive Dependencies, Project Attachments, and Project...... 137 8.5.4.6 Advanced Unpacking Options...................... 138 8.5.4.7 Summarizing Dependency Sets..................... 140 8.5.5 moduleSets Sections.............................. 140 8.5.5.1 Module Selection............................ 141 8.5.5.2 Sources Section............................. 141 8.5.5.3 Interpolation of outputDirectoryMapping in.......... 143 8.5.5.4 Binaries section............................. 144 8.5.5.5 moduleSets, Parent POMs...................... 146 8.5.6 Repositories Section................................ 146 8.5.7 Managing the Assembly’s Root Directory..................... 147 8.5.8 componentDescriptors