CHAPTER 1
Getting Started with Maven
Like other craftsmen, software developers rely on their tools to build applications. Developers integrated development environments (IDEs), bug-tracking tools, build tools, frameworks, and debug tools, such as memory analyzers, play a vital role in day-to-day development and maintenance of quality software. This book will discuss and explore the features of Maven, which we know will become an important tool in your software development arsenal.
Apache Maven is an open source, standards-based project management framework that simplifies the building, testing, reporting, and packaging of projects. Mavens initial roots were in the Apache Jakarta Alexandria project that took place in early 2000. It was subsequently used in the Apache Turbine project. Like many other Apache projects at that time, the Turbine project had several subprojects, each with its own Ant-based build system. Back then, there was a strong desire for developing a standard way to build projects and to share generated artifacts easily across projects. This desire gave birth to Maven. Maven version 1.0 was released in 2004, followed by version 2.0 in 2005. At the time of writing this book, 3.0.5 is the current version of Maven.
Maven has become one of the most widely used open source software programs in enterprises around the world. Lets look at some of the reasons why Maven is so popular.
Standardized Directory Structure
Often, when we start work on a new project, a considerable amount of time is spent deciding on the project layout and folder structure needed to store code and configuration files. These decisions can vary vastly across projects and teams, which can make it difficult for new developers to understand and adopt other teams projects. It can also make it hard for existing developers to jump between projects and find what they are seeking.
Maven addresses the above problems by standardizing the folder structure and organization of a project. Maven provides recommendations on where different parts of a project, such as source code, test code, and configuration files, should reside. For example, Maven suggests that all of the Java source code should be placed in the src\main\java folder. This makes it easier to understand and navigate any Maven project.
Additionally, these conventions make it easy to switch to and start using a new IDE. Historically, IDEs varied with project structure and folder names. A dynamic web project in Eclipse might use the WebContent folder to store web assets, whereas NetBeans might use Web Pages for the same purpose. With Maven, your projects follow a consistent structure and become IDE agnostic.
Declarative Dependency Management
Most Java projects rely on other projects and open source frameworks to function properly. It can be cumbersome to download these dependencies manually and keep track of their versions as you use them in your project.
Maven provides a convenient way to declare these project dependencies in a separate, external pom.xml file. It then automatically downloads those dependencies and allows you to use them in your project. This simplifies project dependency management greatly. It is important to note that in the pom.xml file you specify the what and not the how. The pom.xml file can also serve as a documentation tool, conveying your project dependencies and their versions.
Plug-ins
Maven follows a plug-inbased architecture, making it easy to augment and customize its functionality. These plug-ins encapsulate reusable build and task logic. Today, there are hundreds of Maven plug-ins available that can be used to carry out tasks ranging from code compilation to packaging to project documentation generation.
Maven also makes it easy to create your own plug-ins, thereby enabling you to integrate tasks and workflows that are specific to your organization.
Uniform Build Abstraction
Maven provides a uniform interface for building projects. You can build a Maven project by using just a handful of commands. Once you become familiar with Mavens build process, you can easily figure out how to build other Maven projects. This frees developers from having to learn build idiosyncrasies so they can focus more on development.
Tools Support
Maven provides a powerful command-line interface to carry out different operations. All major IDEs today provide excellent tool support for Maven. Additionally, Maven is fully integrated with todays continuous integration products such as Jenkins, Bamboo, and Hudson.
Archetypes
As we already mentioned, Maven provides a standard directory layout for its projects. When the time comes to create a new Maven project, you need to build each directory manually, and this can easily become tedious. This is where Maven archetypes come to rescue. Maven archetypes are predefined project templates that can be used to generate new projects. Projects created using archetypes will contain all of the folders and files needed to get you going.
Archetypes is also a valuable tool for bundling best practices and common assets that you will need in each of your projects. Consider a team that works heavily on Spring framework-based web applications. All Spring-based web projects share common dependencies and require a set of Spring configuration files. It is also highly possible that all of these web projects have similar Log4j/Logback configuration files, CSS/Images , and Apache Tile layouts or SiteMesh decorators. Maven lets this team bundle these common assets into an archetype. When new projects get created using this archetype, they will automatically have the common assets included. No more copy and pastes or drag and drops required.
Open Source
Maven is open source and costs nothing to download and use. It comes with rich online documentation and the support of an active community. Additionally, companies such as Sonatype offer commercial support for the Maven ecosystem.
CONVENTION OVER CONFIGURATION
Convention overconfiguration (CoC) or coding by convention is one of the key tenants of Maven. Popularized by the Ruby on Rails community, CoC emphasizes sensible defaults, thereby reducing the number of decisions to be made. It saves time and also results in a simpler end product, as the amount of configuration required is drastically reduced.
As part of its CoC adherence, Maven provides several sensible defaults for its projects. It lays out a standard directory structure and provides defaults for the generated artifacts. Imagine looking at a Maven artifact with the name log4j-1.4.3.jar . At a glance, you can easily see that you are looking at a log4j JAR file, version 1.4.3.
One drawback of Mavens CoC is the rigidness that end users experience when using it. To address this, you can customize most of Mavens defaults. For example, it is possible to change the location of the Java source code in your project. As a rule of thumb, however, such changes to defaults should be minimized.
Maven Alternatives
Although the emphasis of this book is on Maven, lets look at a couple of its alternatives: Ant + Ivy and Gradle.
Ant + Ivy
Apache Ant ( http://ant.apache.org ) is a popular open source tool for scripting builds. Ant is Java based, and it uses Extensible Markup Language (XML) for its configuration. The default configuration file for Ant is the build.xml file.
Using Ant typically involves defining tasks and targets. As the name suggests, an Ant task is a unit of work that needs to be completed. Typical tasks involve creating a directory, running a test, compiling source code, building a web application archive (WAR) file, and so forth. A
Next page