Maven Essentials - Russell E Gold - E-Book

Maven Essentials E-Book

Russell E Gold

0,0
20,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Maven is the #1 build tool used by developers and it has been around for more than a decade. Maven stands out among other build tools due to its extremely extensible architecture, which is built on of the concept of convention over configuration. It’s widely used by many open source Java projects under Apache Software Foundation, Sourceforge, Google Code, and more.
Maven Essentials is a fast-paced guide to show you the key concepts in Maven and build automation. We get started by introducing you to Maven and exploring its core concepts and architecture. Next, you will learn about and write a Project Object Model (POM) while creating your own Maven project. You will also find out how to create custom archetypes and plugins to establish the most common goals in build automation. After this, you’ll get to know how to design the build to prevent any maintenance nightmares, with proper dependency management. We then explore Maven build lifecycles and Maven assemblies. Finally, you will discover how to apply the best practices when designing a build system to improve developer productivity.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 171

Veröffentlichungsjahr: 2015

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Maven Essentials
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Apache Maven Quick Start
Installing Apache Maven
Installing Apache Maven on Ubuntu
Installing Apache Maven on Mac OS X
Installing Apache Maven on Microsoft Windows
Configuring the heap size
Hello Maven!
Convention over configuration
Maven repositories
IDE integration
NetBeans integration
IntelliJ IDEA integration
Eclipse integration
Troubleshooting
Enabling Maven debug level logs
Building a dependency tree
Viewing all the environment variables and system properties
Viewing the effective POM file
Viewing the dependency classpath
Summary
2. Understanding the Project Object Model (POM)
Project Object Model (POM)
POM hierarchy
Super POM
POM extending and overriding
Maven coordinates
The parent POM
Managing POM dependencies
Transitive dependencies
Dependency scopes
Optional dependencies
Dependency exclusion
Summary
3. Maven Archetypes
Archetype quickstart
Batch mode
Archetype catalogues
Building an archetype catalogue
Public archetype catalogues
The anatomy of archetype – catalog.xml
The archetype plugin goals
Java EE web applications with the archetype plugin
Deploying web applications to a remote Apache Tomcat server
Android mobile applications with the archetype plugin
EJB archives with the archetype plugin
JIRA plugins with the archetype plugin
Spring MVC applications with the archetype plugin
Summary
4. Maven Plugins
Common Maven plugins
The clean plugin
The compiler plugin
The install plugin
The deploy plugin
The surefire plugin
The site plugin
The jar plugin
The source plugin
The resources plugin
The release plugin
Plugin discovery and execution
Plugin management
Plugin repositories
Plugin as an extension
Summary
5. Build Lifecycles
Standard lifecycles in Maven
The clean lifecycle
The default lifecycle
The site lifecycle
Lifecycle bindings
Lifecycle extensions
Summary
6. Maven Assemblies
The assembly plugin
The assembly descriptor
Artifact/resource filtering
Assembly help
A runnable standalone Maven project
Summary
7. Best Practices
Dependency management
Defining a parent module
POM properties
Avoiding repetitive groupIds and versions, and inheriting from the parent POM
Following naming conventions
Think twice before you write your own plugin. You may not need it!
The Maven release plugin
The Maven enforcer plugin
Avoiding the use of unversioned plugins
Descriptive parent POM files
Documentation is your friend
Avoid overriding the default directory structure
Using SNAPSHOT versioning during the development
Get rid of unused dependencies
Avoiding keeping credentials in application POM files
Avoiding using deprecated references
Avoiding repetition – use archetypes
Avoiding using maven.test.skip
Summary
Index

Maven Essentials

Maven Essentials

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: November 2015

Production reference: 1251115

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78398-676-7

www.packtpub.com

Credits

Author

Prabath Siriwardena

Reviewer

Antonio Mendoza Pérez

Commissioning Editor

Amarabha Banerjee

Acquisition Editor

Shaon Basu

Content Development Editor

Samantha Gonsalves

Technical Editor

Vivek Pala

Copy Editor

Pranjali Chury

Project Coordinator

Kinjal Bari

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta

Graphics

Disha Haria

Production Coordinator

Nilesh Mohite

Cover Work

Nilesh Mohite

About the Author

Prabath Siriwardena is the director of Security Architecture at WSO2 Inc., a company that produces a wide variety of open source software from data to screen. He is a member of OASIS Identity Metasystem Interoperability (IMI) TC, OASIS eXtensible Access Control Markup Language (XACML) TC, OASIS Security Services (SAML) TC, OASIS Identity in the Cloud TC, and OASIS Cloud Authorization (CloudAuthZ) TC. Prabath is also a member of PMC Apache Axis and has spoken at numerous international conferences, including OSCON, ApacheCon, WSO2Con, EIC, IDentity Next, and OSDC. He has more than 10 years of industry experience and has worked with many Fortune 100 companies.

Acknowledgments

I would first like to thank Shaon Basu, an acquisition editor at Packt Publishing, who came up with the idea of writing a book on Apache Maven; then, Samantha Gonsalves, a content development editor at Packt Publishing, who I worked with closely throughout the project—thank you very much, Samantha, for your patience and flexibility. Also, I would like to thank all the others at Packt Publishing who helped me to make this book a reality from the initial idea. Thank you very much for all your continuous support.

Dr. Sanjiva Weerawarana, the CEO of WSO2, and Paul Fremantle, the CTO of WSO2, have always been my mentors. I am truly grateful to both Dr. Sanjiva and Paul for everything they have done for me.

I would like to thank my beloved wife, Pavithra, and my loving little daughter, Dinadi. Pavithra wanted me to write this book even more than I wanted to. If I say she is the driving force behind this book, I am not exaggerating. She simply went beyond by not only feeding me with encouragement, but also by helping immensely in reviewing the book and developing samples. She was always the first reader. Thank you very much, Pavithra. Also, thanks to little Dinadi for your patience—it was your time that I spent writing the book.

I would also like to thank all the technical reviewers of the book. All your suggestions and thoughts are extremely valuable and much appreciated.

My parents and my sister have been the driving force behind me since my birth. If not for them, I wouldn't be who I am today. I am grateful to them for everything they have done for me. Last but not least, my wife's parents were amazingly helpful in making sure that the only thing I had to do was to write this book, taking care of almost all the other things that I was supposed to do.

Although this sounds like a one-man effort, it's actually a team effort. Thanks to everyone who supported me in different ways.

About the Reviewer

Antonio Mendoza Pérez is a senior software engineer with over 9 years of experience of developing Java EE applications. He also has an increasing interest in Scala, Groovy, and other JVM programming languages. He has contributed to several projects by developing the core element in both frontend and backend, such as customizing its construction and distribution with Maven.

He also has been a reviewer of JBPM6 Developer Guide, Packt Publishing.

You can get in touch with him through his blog at http://antmendoza.com.

I would like to thank my parents and sisters who have always been there for me.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

Maven is the number one build tool used by developers, and it has been there for more than a decade. Maven stands out among other build tools due to its extremely extensible architecture, which is built on top of the concept convention over configuration. This, in fact, has made Maven the de-facto tool to manage and build Java projects. It's being used widely by many open source Java projects under the Apache Software Foundation, SourceForge, Google Code, and many more.

This book provides a step-by-step guide, showing the readers how to use Apache Maven in an optimal way to address enterprise build requirements. Following the book, readers will be able to gain a thorough understanding of the following key areas:

How to get started with Apache Maven, applying Maven best practices in order to design a build system to improve a developer's productivityHow to customize the build process to suit it exactly to your enterprise's needs by using appropriate Maven plugins, lifecycles, and archetypesHow to troubleshoot build issues with greater confidenceHow to design the build in a way that avoids any maintenance nightmares with proper dependency managementHow to optimize Maven configuration settingsHow to build your own distribution archive using Maven assemblies

What this book covers

Chapter 1, Apache Maven Quick Start, focuses on building a basic foundation around Maven to get started. It starts by explaining the basic steps to install and configure Maven on Ubuntu, Mac OS X, and Microsoft Windows operating systems. The latter part of the chapter covers some of the common useful Maven tips and tricks.

Chapter 2, Understanding the Project Object Model (POM), focuses on the Maven Project Object Model (POM) and how to adhere to the industry-wide accepted best practices to avoid maintenance nightmares. The key elements of a POM file, POM hierarchy and inheritance, managing dependencies, and related topics are covered here.

Chapter 3, Maven Archetypes, focuses on how Maven archetypes provide a way of reducing repetitive work in building Maven projects. There are thousands of archetypes out there that are available publicly to help you build different types of projects. This chapter covers a commonly used set of archetypes.

Chapter 4, Maven Plugins, covers some of the most commonly used Maven plugins and then explains how plugins are discovered and executed. Maven only provides a build framework, while the Maven plugins perform the actual tasks. Maven has a large, rich set of plugins, and the chances are very small that you will have to write your own custom plugin.

Chapter 5, Build Lifecycles, explains how the three standard lifecycles work and how we can customize them. Later in the chapter, we discuss how to develop our own lifecycle extensions.

Chapter 6, Maven Assemblies, covers real-world examples of how to use the Maven assembly plugin in detail, and finally concludes with an end-to-end sample Maven project.

Chapter 7, Best Practices, looks at and highlights some of the best practices to be followed in a large-scale development project with Maven. It is always recommended to follow the best practices since it will drastically improve developer productivity and reduce any maintenance nightmares.

What you need for this book

To follow the examples that are presented in this book, you will need the following software:

Apache Maven 3.3.x, which you can find at http://maven.apache.org/download.cgiJava 1.7+ SDK, which you can find at http://www.oracle.com/technetwork/java/javase/downloads/index.htmlOperating Systems: Windows, Linux, or Mac OS X.

Who this book is for

The book is ideal for experienced developers who are already familiar with build automation, but want to learn how to use Maven and apply its concepts to the most difficult scenarios in build automation.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "When you type mvn clean install, Maven will execute all the phases in the default lifecycle up to install (including the install phase)."

A block of code is set as follows:

<project> [...] <build> [...] <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> [...] </build> [...] </project>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<project> [...] <build> [...] <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> [...] </build> [...] </project>

Any command-line input or output is written as follows:

$ mvn install:install

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Apache Maven Quick Start

Apache Maven is popular as a build tool. However, in reality, it goes beyond being just a build tool. It provides a comprehensive build management platform. Prior to Maven, developers had to spend a lot of time in building a build system. There was no common interface. It differed from project to project, and each time a developer moved from one project to another, there was a learning curve. Maven filled this gap by introducing a common interface. It merely ended the era of the build engineer.

In this chapter, we will talk about the following topics:

Installing and configuring Maven on Ubuntu, Mac OS X, and Microsoft WindowsIDE integrationTips and tricks for using Maven effectively

Installing Apache Maven

Installing Maven on any platform is more than a straightforward task. At the time of writing this book, the latest version was 3.3.3, which is available for download at http://maven.apache.org/download.cgi. This version requires JDK 1.7.0 or above.

Tip

You should keep a note on the Java requirement for version 3.3.3, if you are planning to upgrade from versions 3.0.*, 3.1.*, or 3.2.*. Prior to Maven 3.3.x, the only requirement was JDK 1.5.0. or JDK 1.6.0 (for 3.2.*).

Apache Maven is an extremely lightweight distribution. It does not have any hard requirements on memory, disk space, or CPU. Maven itself is built on top of Java, and it would work on any operating system that runs Java virtual machine (JVM).

Installing Apache Maven on Ubuntu

Installing Maven on Ubuntu is a single line command. Proceed with the following steps:

Run the following apt-get command in the command prompt. You need to have the sudo privileges to execute this:
$ sudo apt-get install maven
This takes a few minutes to complete. Upon the completion of the installation, you can run the following command to verify the installation:
$ mvn -version
You should get an output similar to the following, if Apache Maven has been installed successfully:
$ mvn -versionApache Maven 3.3.3Maven home: /usr/share/mavenJava version: 1.7.0_60, vendor: Oracle CorporationJava home: /usr/lib/jvm/java-7-oracle/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "3.13.0-24-generic", arch: "amd64", family: "unix"
Maven is installed under the /usr/share/maven directory. To check the directory structure behind the Maven installation directory, use the following command:
$ ls /usr/share/mavenbin boot conf lib man
Maven configuration files can be found at /etc/maven, which can be listed using the following command:
$ ls /etc/mavenm2.conf settings.xml

If you don't want to work with the apt-get command, there is another way of installing Maven under any Unix-based operating system. We will discuss this in the next section. Since Mac OS X has a kernel built at the top of the Unix kernel, installing Maven on Mac OS X would be the same as installing it on any Unix-based operating system.

Installing Apache Maven on Mac OS X

Most of the OS X distributions prior to OS X Mavericks had Apache Maven preinstalled. To verify that you've got Maven installed in your system, try out the following command. If it does not result in a version, then it means you do not have it installed:

$ mvn –version

The following steps will guide you through the Maven installation process on Max OS X Yosemite:

First, we need to download the latest version of Maven. Throughout this book, we will use Maven 3.3.3, which was the latest version at the time of writing this book. Maven 3.3.3 ZIP distribution can be downloaded from http://maven.apache.org/download.cgi.Unzip the downloaded ZIP file into /usr/share/java. You need to have the sudo privileges to execute this:
$ sudo unzip apache-maven-3.3.3-bin.zip -d /usr/share/java/
In case you already have Maven installed in your system, use the following command to unlink. /usr/share/maven is only a symlink to the directory where Maven is installed:
$ sudo unlink /usr/share/maven
Use the following command to create a symlink to the latest Maven distribution that you just unzipped. You need to have the sudo privileges to execute this:
$ sudo ln -s /usr/share/java/apache-maven-3.3.3 /usr/share/maven
Use the following command to update the value of the PATH environment variable:
$ export PATH=$PATH:/usr/share/maven/bin
Use the following command to update (or set) the value of the M2_HOME environment variable:
$ export M2_HOME=/usr/share/maven
Verify the Maven installation with the following command:
$ mvn -version Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T04:57:37-07:00) Maven home: /usr/share/maven Java version: 1.7.0_75, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Hom e/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"
If you get the following error while