Installation Instructions
Installation Instructions
take much of the hard work out of the build process. Maven uses a declarative
approach, where the project structure and contents are described, rather then
the task-based approach used in Ant or in traditional make files, for example. This
helps enforce company-wide development standards and reduces the time needed
to write and maintain build scripts.
Installation Instructions
Maven is a Java tool, so you must have Java installed in order to proceed. More
precisely, you need a Java Development Kit (JDK), the Java Runtime Environment
(JRE) is not sufficient.
Additional optional installation steps are listed after the platform specific
instructions.
Windows 2000/XP
Building a Project
The vast majority of Maven-built projects can be built with the following command:
This command tells Maven to build all the modules, and to install it in the local
repository. The local repository is created in your home directory (or alternative
location that you created it), and is the location that all downloaded binaries and
the projects you built are stored.
That's it! If you look in the target subdirectory, you should find the build output
and the final library or application that was being built.
Project lifecycles
Project lifecycles are central to Maven 2. Most developers are familiar with the
notion of build phases such as compile, test, and deploy. Ant has targets with
names like those. In Maven 1, corresponding plug-ins are called directly. To compile
Java source code, for instance, the java plug-in is used:
$maven java:compile
In Maven 2, this notion is standardized into a set of well-known and well-defined
lifecycle phases (see Figure 2). Instead of invoking plug-ins, the Maven 2 developer
invokes a lifecycle phase: $mvn compile.
Some of the more useful Maven 2 lifecycle phases are the following:
Purpose
Creating a Project
If you have just installed Maven, it may take a while on the first run. This is
because Maven is downloading the most recent artifacts (plugin jars and other
files) into your local repository. You may also need to execute the command a
couple of times before it succeeds. This is because the remote server may time
out before your downloads are complete. Don't worry, there are ways to fix that.
You will notice that the generate goal created a directory with the same name
given as the artifactId. Change into that directory.
cd my-app
Under this directory you will notice the following standard project structure.
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
The src/main/java directory contains the project source code, the src/test/java
directory contains the test source, and the pom.xml is the project's Project
Object Model, or POM.
The POM
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
pom.xml contains the Project Object Model (POM) for this project. The POM is
the basic unit of work in Maven. This is important to remember because Maven is
inherently project-centric in that everything revolves around the notion of a
project. In short, the POM contains every important piece of information about
your project and is essentially one-stop-shopping for finding anything related to
your project. Understanding the POM is important and new users are encouraged
to refer to the Introduction to the POM.
This is a very simple POM but still displays the key elements every POM contains,
so let's walk through each of them to familiarize you with the POM essentials:
mvn package
This command will create a target directory inside the project (in this case my-
app) This target folder contains the jar file for the project .It Also contains the
important information like test case , reports
The command line will print out various actions, and end with the following:
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Oct 05 21:16:04 CDT 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
Unlike the first command executed (archetype:generate) you may notice the
second is simply a single word - package. Rather than a goal, this is a phase. A
phase is a step in the build lifecycle, which is an ordered sequence of phases. When
a phase is given, Maven will execute every phase in the sequence up to and including
the one defined. For example, if we execute the compile phase, the phases that
actually get executed are:
1. validate
2. generate-sources
3. process-sources
4. generate-resources
5. process-resources
6. compile
You may test the newly compiled and packaged JAR with the following command:
Hello World!
====================================================================
Suppose you want to use Hibernate in your project. You would simply add a new
dependency to the dependencies section in pom.xml, as follows:
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.0.3</version>
<scope>compile</scope>
</dependency>
And that's it! You don't have to hunt around to know in which other JARs (and in
which versions) you need to run Hibernate 3.0.3; Maven will do it for you!