Chapter 1. Maven Jump-Start

Let's start using Maven. By the end of this chapter you should be able to create a Maven project from scratch, use Maven to manage dependencies, and create a simple project web site with some interesting reports.

Installing Maven

Before you start in on this book, you'll need to install some prerequisites. While the examples in this book were written with Java 1.4.2, Maven is compatible with both Java 1.4.2 and Java 1.5.0. This book was written to the most recent version of Maven released at the time of this writing—Maven 1.0.2. In addition to the JDK and Maven 1.0.2, you will also need to be connected to the Internet, as Maven will download dependencies from a public web site as they are required. So, go ahead, install Maven.

Tip

Some of the plug-ins referenced in this Developer's Notebook are not bundled with Maven 1.0.2. Please refer to Chapter 6 and the comprehensive list of plug-ins in Appendix AAppendix A for detailed instructions on installing the required Maven plug-ins.

How do I do that?

Download Maven from the Apache Software Foundation (ASF). Go to http://maven.apache.org/ and select Downloading from the Getting Maven menu on the left navigation menu. This will take you to a page which will let you select a Windows Installer package, a .zip file, a tar'd .bzip file, or a tar'd .gzip file. Download the distribution appropriate for your platform.

On a Microsoft Windows platform, download the Windows Installer package (maven-1.0.2.exe) and follow the instructions during the automated installation. After Maven is installed using Windows Installer, you should have a user environment variable, MAVEN_HOME, pointing to the location of your Maven installation. You will then need to add %MAVEN_HOME%\bin to your PATH by selecting Control PanelSystemAdvanced and clicking the Environment Variables button. Prepend %MAVEN_HOME%\bin to your PATH variable, and go to the command prompt by running cmd.exe. If Maven has been installed successfully, you should see the following output on the command line:

C:\dev\mavenbook\code>maven -v
 _ _  _ _
|  \/  |_ _ _Apache_ _ _ _ _
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\_ _,_|\_/\_ _ _|_||_|  v. 1.0.2

If you prefer to install Maven in a directory other than C:\Program Files\Apache Software Foundation\Maven 1.0.2, you may also download the Maven 1.0.2 .zip file and unpack Maven in any directory. Set MAVEN_HOME to point to the directory that holds the unpacked Maven distribution, and add MAVEN_HOME\bin to your PATH.

On a Unix platform, download the tar'd .gzip file (maven-1.0.2.tar.gz) and unpack it to the directory of your choice with tar xvzf maven-1.0.2.tar.gz. For this lab, we'll assume that you unpacked Maven to the /usr/local/maven-1.0.2 directory. You will then need to set two environment variables, MAVEN_HOME and PATH. The following commands set these two variables to the appropriate values:

[tobrien@mavenbook tobrien]$ export MAVEN_HOME=/usr/local/maven-1.0.2
[tobrien@mavenbook tobrien]$ export PATH=${PATH}:${MAVEN_HOME}/bin

If Maven has been successfully installed on your machine, you should see the same output one would see on a Windows machine. Congratulations! You've installed Maven.

Tip

Some people prefer to keep local applications in a /opt directory on Unix and a c:\apps directory on Windows. You can install Maven wherever you like.

What just happened?

You just installed Maven 1.0.2, and configured some environment variables. That's it! Once MAVEN_HOME is set and maven.bat or maven.sh is available on your PATH, you should be able to complete the labs in this Developer's Notebook.

What about...

...Maven 2?

It is a good time to mention Maven 2 (sometimes referred to as "m2"). Maven 2 is a complete rewrite of Maven 1. The primary goal of the rewrite is to offer a strong Java build and project comprehension API, allowing Maven to be embedded everywhere, and especially in higher-level products such as IDEs, quality tools, reporting tools, and so on. Maven 2 formalized the concept of a build lifecycle and is even easier to extend than Maven 1.

Maven 1 and 2 share a lot of concepts, but they do have several major differences. Throughout this book, we have attempted to note the differences you can expect. For more information about Maven 2, stay tuned to the Maven web site at http://maven.apache.org/, download the pre-release versions of Maven 2, and join the Maven user and developer mailing lists. If you've heard of Continuous Integration, you might also want to take a look at a Maven subproject named Continuum, at http://maven.apache.org/continuum.

Get Maven: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.