Chapter 2. Installing and Running Maven

This chapter contains very detailed instructions for installing Maven on a number of different platforms. Instead of assuming a level of familiarity with installing software and setting environment variables, we’ve opted to be as thorough as possible to minimize any problems that might arise due to a partial installation. The only thing this chapter assumes is that you’ve already installed a suitable Java Development Kit (JDK). If you are just interested in installation, you can move on to the rest of the book after reading through the “Downloading Maven” and “Installing Maven” sections. If you are interested in the details of your Maven installation, this entire chapter will give you an overview of what you’ve installed and the Apache Software License.

Verify Your Java Installation

Although Maven can run on Java 1.4, this book assumes that you are running at least Java 5. Go with the most recent stable JDK available for your operating system. Either Java 5 or Java 6 will work with all of the examples in this book:

% java -version
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)

Maven works with all certified Java-compatible development kits, and a few noncertified implementations of Java. The examples in this book were written and tested against the official Java Development Kit releases downloaded from the Sun Microsystems web site. If you’re working with a Linux distribution, you may need to download Sun’s JDK yourself and make sure it’s the version you’re invoking (by running java -version, as shown earlier). Now that Sun has open sourced Java, this will hopefully improve in the future, and we’ll get the Sun Java Runtime Environment (JRE) and JDK by default even in purist distributions. Until that day, you may need to do some of your own downloading.

Downloading Maven

You can download Maven from the Apache Maven project web site by going to http://maven.apache.org/download.html.

When downloading Maven, make sure you choose the latest version of Apache Maven from the web site. The latest version of Maven at the time of this writing is Maven 2.0.9. If you are not familiar with the Apache Software License, you should get acquainted with the terms of the license before you start using the product. More information on the Apache Software License can be found in About the Apache Software License,” later in this chapter.

Installing Maven

There are wide differences between operating systems such as Mac OS X and Microsoft Windows, and there are subtle differences between different versions of Windows. Luckily, the process of installing Maven on all of these operating systems is relatively painless and straightforward. The following sections outline the recommended best-practice for installing Maven on a variety of operating systems.

Installing Maven on Mac OS X

You can download a binary release of Maven from http://maven.apache.org/download.html. Download the current release of Maven in a format that is convenient for you to work with. Pick an appropriate place for it to live, and expand the archive there. If you expanded the archive into the directory /usr/local/maven-2.0.9, you may want to create a symbolic link to make it easier to work with and to avoid the need to change any environment configuration when you upgrade to a newer version:

/usr/local % ln -s maven-2.0.9 maven
/usr/local % export M2_HOME=/usr/local/maven
/usr/local % export PATH=${M2_HOME}/bin:${PATH}

Once Maven is installed, you need to do a couple of things to make it work correctly. You need to add its bin directory in the distribution (in this example, /usr/local/maven/bin) to your command path. You also need to set the environment variable M2_HOME to the top-level directory you installed (in this example, /usr/local/maven).

Note

Installation instructions are the same for both OS X Tiger and Leopard. It has been reported that Maven 2.0.6 is shipping with a preview release of Xcode. If you have installed XCode, run mvn from the command line to check availability. XCode installs Maven in /usr/share/maven. We recommend installing the most recent version of Maven 2.0.9, as there have been a number of bug fixes and improvements since Maven 2.0.9 was released.

You’ll need to add both M2_HOME and PATH to a script that will run every time you log in. To do this, add the following lines to .bash_login:

export M2_HOME=/usr/local/maven
export PATH=${M2_HOME}/bin:${PATH}

Once you’ve added these lines to your own environment, you will be able to run Maven from the command line.

Note

These installation instructions assume that you are running bash.

Installing Maven on Microsoft Windows

Installing Maven on Windows is very similar to installing Maven on Mac OS X, the main differences being the installation location and the setting of an environment variable. This book assumes a Maven installation directory located at c:Program Filesmaven-2.0.9, but it won’t make a difference if you install Maven in another directory as long as you configure the proper environment variables. Once you’ve unpacked Maven in the installation directory, you will need to set two environment variables—PATH and M2_HOME. To set these environment variables from the command line, type in the following commands:

C:Users	obrien > set M2_HOME=c:Program Filesmaven-2.0.9
C:Users	obrien > set PATH=%PATH%;%M2_HOME%in

Setting these environment variables on the command line will allow you to run Maven in your current session, but unless you add them to the system environment variables through the control panel, you’ll have to execute these two lines every time you log into your system. Set both M2_HOME and PATH to point to your Maven installation.

Installing Maven on Linux

To install Maven on a Linux machine, follow the exact procedure outlined in Installing Maven on Mac OS X,” earlier in this chapter.

Installing Maven on FreeBSD or OpenBSD

To install Maven on a FreeBSD or OpenBSD machine, follow the exact procedure outlined in Installing Maven on Mac OS X,” earlier in this chapter.

Testing a Maven Installation

Once Maven is installed, you can see if it is installed properly by running mvn -v from the command line. If Maven has been installed, you should see something resembling the following output:

~/examples $ mvn -v
Maven 2.0.9

If you see this output, you know that Maven has been successfully installed. If you do not see this output and your operating system cannot find the mvn command, make sure that your PATH and M2_HOME environment variables have been properly set.

Maven Installation Details

Maven’s download measures in at roughly 1.5 MiB.[1] It has attained such a slim download size because the core of Maven has been designed to retrieve plugins and dependencies from a remote repository on demand. When you start using Maven, it will start to download plugins to a local repository as described in the section User-Specific Configuration and Repository,” later in this chapter. In case you are curious, let’s take a quick look at what is in Maven’s installation directory:

/usr/local/maven $ ls -p1
LICENSE.txt
NOTICE.txt
README.txt
bin/
boot/
conf/
lib/

LICENSE.txt contains the software license for Apache Maven. This license is described in some detail later in the section About the Apache Software License.” NOTICE.txt contains some notices and attributions required by libraries that Maven depends on. README.txt contains some installation instructions. bin/ contains the mvn script that executes Maven. boot/ contains a JAR file (classwords-1.1.jar) that is responsible for creating the Class Loader in which Maven executes. conf/ contains a global settings.xml that can be used to customize the behavior of your Maven installation. If you need to customize Maven, it is customary to override any settings in a settings.xml file stored in ~/.m2. lib/ contains a single JAR file (maven-core-2.0.9-uber.jar) that contains the core of Maven.

User-Specific Configuration and Repository

Once you start using Maven extensively, you’ll notice that Maven has created some local user-specific configuration files and a local repository in your home directory. In ~/.m2, there will be:

settings.xml

A file containing user-specific configuration for authentication, repositories, and other information to customize the behavior of Maven.

repository/

This directory contains your local Maven repository. When you download a dependency from a remote Maven repository, Maven stores a copy of the dependency in your local repository.

Note

In Unix (and OS X), your home directory will be referred to using a tilde (i.e., ~/bin refers to /home/tobrien/bin). In Windows, we will also be using ~ to refer to your home directory. In Windows XP, your home directory is C:Documents and Settings obrien, and in Windows Vista, your home directory is C:Users obrien. From this point forward, you should translate paths such as ~/m2 to your operating system’s equivalent.

Upgrading a Maven Installation

If you’ve installed Maven on a Mac OS X or Unix machine according to the details given in Installing Maven on Mac OS X” and Installing Maven on Linux,” it should be easy to upgrade to newer versions of Maven when they become available. Simply install the newer version of Maven (/usr/local/maven-2.future) next to the existing version of Maven (/usr/local/maven-2.0.9). Then, switch the symbolic link /usr/local/maven from /usr/local/maven-2.0.9 to /usr/local/maven-2.future. Since you’ve already set your M2_HOME variable to point to /usr/local/maven, you won’t need to change any environment variables.

If you’ve installed Maven on a Windows machine, simply unpack Maven to c:Program Filesmaven-2.future and update your M2_HOME variable.

Getting Help with Maven

Although this book aims to be a comprehensive reference, there are going to be topics we miss and special situations and tips that are not covered. The core of Maven is very simple, but the real work in Maven happens in the plugins, and there are too many plugins available to cover them all in one book. When you encounter problems and features that are not covered in this book, we suggest searching for answers at the following locations:

http://maven.apache.org

This is the first place you should look; the Maven web site contains a wealth of information and documentation. Every plugin has a few pages of documentation, and it provides a series of “quick start” documents that will be helpful in addition to the contents of this book. Although the Maven site contains plenty of information, it can also be a frustrating, confusing, and overwhelming. A custom Google search box on the main Maven page will search known Maven sites for information. This provides better results than a generic Google search.

Maven user mailing list

The Maven user mailing list is the place for users to ask questions. Before you ask a question on the user mailing list, you will want to search for any previous discussion that might relate to your question. It is bad form to ask a question that has already been asked without first checking to see whether an answer already exists in the archives. There are a number of useful mailing list archive browsers; we’ve found Nabble to be the most useful. You can browse the user mailing list archives here: http://www.nabble.com/Maven---Users-f178.html. You can join the user mailing list by following the instructions available here: http://maven.apache.org/mail-lists.html.

http://www.sonatype.com

Sonatype maintains an online copy of this book and other tutorials related to Apache Maven.

Note

Despite the best efforts of some very dedicated Maven contributors, the Maven web site is poorly organized and full of incomplete (and sometimes misleading) snippets of documentation. Throughout the Maven community there is a lack of a common standards for plugin documentation. Some plugins are heavily documented, whereas others lack even the most basic instructions for usage. Often your best bet is to search for a solution in the archives of the user mailing list. If you really want to help, submit a patch to the Maven site (or this book).

Using the Maven Help Plugin

Throughout the book, we will be introducing Maven plugins and talking about Maven Project Object Model (POM) files, settings files, and profiles. There are going to be times when you need a tool to help you make sense of some of the models that Maven is using and what goals are available on a specific plugin. The Maven Help plugin allows you to list active Maven profiles, display an effective POM, print the effective settings, or list the attributes of a Maven plugin.

Note

For a conceptual overview of the POM and plugins, see Chapter 3.

The Maven Help plugin has four goals. The first three goals—active-profiles, effective-pom, and effective-settings—describe a particular project and must be run in the base directory of a project. The last goal—describe—is slightly more complex, showing you information about a plugin or a plugin goal. The following commands provide some general information about the four goals:

help:active-profiles

Lists the profiles (project, user, global) that are active for the build.

help:effective-pom

Displays the effective POM for the current build, with the active profiles factored in.

help:effective-settings

Prints out the calculated settings for the project, given any profile enhancement and the inheritance of the global settings into the user-level settings.

help:describe

Describes the attributes of a plugin. This need not run under an existing project directory. You must give at least the groupId and artifactId of the plugin you wish to describe.

Describing a Maven Plugin

Once you start using Maven, you’ll spend most of your time trying to get more information about plugins. You’ll want to know: How do plugins work? What are the configuration parameters? What are the goals? The help:describe goal is something you’ll use very frequently to retrieve this information. With the plugin parameter, you can specify a plugin you wish to investigate, passing in either the plugin prefix (e.g., maven-help-plugin as help) or the groupId:artifact[:version], where version is optional. For example, the following command uses the Help plugin’s describe goal to print out information about the Maven Help plugin:

$ mvn help:describe -Dplugin=help
...
Group Id:  org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version:     2.0.1
Goal Prefix: help
Description:

The Maven Help plugin provides goals aimed at helping to make sense out of
    the build environment. It includes the ability to view the effective
    POM and settings files, after inheritance and active profiles
    have been applied, as well as a describe a particular plugin goal to give 
    usage information.
...

Executing the describe goal with the plugin parameter prints out the Maven coordinates for the plugin, the goal prefix, and a brief description of the plugin. Although this information is helpful, you’ll usually be looking for more detail than this. If you want the Help plugin to print a full list of goals with parameters, execute the help:describe goal with the parameter full as follows:

$ mvn help:describe -Dplugin=help -Dfull
...
Group Id:  org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version:     2.0.1
Goal Prefix: help
Description:

The Maven Help plugin provides goals aimed at helping to make sense out of
    the build environment. It includes the ability to view the effective
    POM and settings files, after inheritance and active profiles have been 
    applied, as well as a describe a particular plugin goal to give usage 
    information.

Mojos:

===============================================
Goal: 'active-profiles'
===============================================
Description:

Lists the profiles which are currently active for this build.

Implementation: org.apache.maven.plugins.help.ActiveProfilesMojo
Language: java

Parameters:
-----------------------------------------------

[0] Name: output
Type: java.io.File
Required: false
Directly editable: true
Description:

This is an optional parameter for a file destination for the output of 
this mojo...the listing of active profiles per project.

-----------------------------------------------

[1] Name: projects
Type: java.util.List
Required: true
Directly editable: false
Description:

This is the list of projects currently slated to be built by Maven.

-----------------------------------------------

This mojo doesn't have any component requirements.
===============================================

... remove the other goals ...

This option is great for discovering all of a plugin’s goals as well as their parameters. But sometimes this gives you far more information than you need. To get information about a single goal, set the mojo parameter as well as the plugin parameter. The following command lists all of the information about the Compiler plugin’s compile goal:

$ mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull

Note

What’s a Mojo? In Maven, a plugin goal is known as a Mojo.

About the Apache Software License

Apache Maven is released under the Apache License, version 2.0. If you want to read this license, you can look at ${M2_HOME}/LICENSE.txt or read it on the Open Source Initiative’s web site at http://www.opensource.org/licenses/apache2.0.php.

Chances are good that, if you are reading this book, you are not a lawyer. If you are wondering what the Apache License, version 2.0 means, the Apache Software Foundation has assembled a very helpful Frequently Asked Questions (FAQ) page about the license, available here: http://www.apache.org/foundation/licence-FAQ.html. Here’s the answer to the frequently asked question “I am not a lawyer. What does it all mean?”:

[This license] allows you to:

  • Freely download and use Apache software, in whole or in part, for personal, company internal, or commercial purposes;

  • Use Apache software in packages or distributions that you create.

It forbids you to:

  • Redistribute any piece of Apache-originated software without proper attribution;

  • Use any marks owned by the Apache Software Foundation in any way that might state or imply that the Foundation endorses your distribution;

  • Use any marks owned by the Apache Software Foundation in any way that might state or imply that you created the Apache software in question.

It requires you to:

  • Include a copy of the license in any redistribution you may make that includes Apache software;

  • Provide clear attribution to the Apache Software Foundation for any distributions that include Apache software.

It does not require you to:

  • Include the source of the Apache software itself, or of any modifications you may have made to it, in any redistribution you may assemble that includes it;

  • Submit changes that you make to the software back to the Apache Software Foundation (though such feedback is encouraged).

This ends the installation information. The next part of the book contains Maven examples.



[1] Ever purchased a 200 GB hard drive only to realize that it showed up as less than 200 GiB when you installed it? Computers understand Gibibytes, but retailers sell products using Gigabytes. MiB stands for Mebibyte, which is defined as 220 or 10242. These binary prefix standards are endorsed by the Institute of Electrical and Electronics Engineers (IEEE), the International Committee for Weights and Measures (CIPM), and the International Electrotechnical Commission (IEC). For more information about Kibibytes, Mebibytes, Gibibytes, and Tebibytes, see http://en.wikipedia.org/wiki/Mebibyte.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243