Introduction to Apache Maven
What is maven?
Maven is a project management tool
which encompasses a project object model, a set of standards, a project
life cycle, a dependency management system, and logic for executing plugin goals
at defined phases in a life cycle. 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.
The great majority of Maven users are going
to call Maven a “build tool”: a tool used to build deployable artifacts from
source code. Build engineers and project managers might refer to Maven as
something more comprehensive: a project management tool. What is the
difference? A build tool such as Ant is focused solely on preprocessing, compilation,
packaging, testing, and distribution. A project management tool such as Maven
provides a super set of features found in a build tool. In addition to providing
build capabilities, Maven can also run reports, generate a web site, and facilitate
communication among members of a working team.
Why use maven?
1.1 Convention
over configuration.
Convention over configuration is a simple concept. Systems,
libraries, and frameworks should assume reasonable defaults. Without requiring
unnecessary configuration, systems should "just work". Popular frameworks
such as Ruby on Rails and EJB3 have started to adhere to these principles in
reaction to the configuration complexity of frameworks such as the initial EJB
2.1 specifications. An illustration of convention over configuration is
something like EJB3 persistence: all you need to do to make a bean persistent
is to annotate that class with @Entity. The framework assumes table and column
names based on the name of the class and the names of the properties. Hooks are
provided for you to override these default, assumed names if the need arises,
but, in most cases, you will find that using the framework supplied defaults
results in a faster project execution.
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.
1.2 A
Common Interface
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 ActiveMQ or Apache ServiceMix from Subversion and
build it from source, you really had to set aside about an hour to figure out
the build system for each 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.
Since 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 ActiveMQ 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.
1.3 Reuse
through plugins
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.
1.4 Conceptual
Model of a "Project"
Maven maintains a model of a project. You are not just compiling
source code into bytecode, you are developing a description of a software
project and assigning a unique set of coordinates to a project. You are
describing the attributes of the project. What is the project’s license? Who
develops and contributes to the project? What other projects does this project
depend upon? Maven is more than just a "build tool", it is more than
just an improvement on tools like make and Ant, it is a platform that
encompasses a new semantics related to software projects and software
development. This definition of a model for every project enables such features
as:
·
Dependency
Management
Maven: The Complete Reference 5 / 316 Because a project is defined by 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.
·
Universal
Reuse of Build Logic
Plugins contain logic that works with the descriptive data and
configuration parameters defined in Project Object Model (POM); they are not
designed to operate upon specific files in known locations.
·
Tool
Portability / Integration
Tools like Eclipse, NetBeans, and IntelliJ now have a common place to
find information about a project. Before the advent of Maven, every IDE had a
different way to store what was essentially a custom Project Object Model
(POM). Maven has standardized this description, and while each IDE continues to
maintain custom project files, they can be easily generated from the model.
·
Easy
Searching and Filtering of Project Artifacts
Tools
like Nexus allow you to index and search the contents of a repository using the
information stored in the POM.
Comments
Post a Comment