0% found this document useful (0 votes)
120 views8 pages

Installation Instructions

Maven is a popular open source build tool for Java projects that takes much of the work out of the build process using a declarative approach. It helps enforce standards and reduces build script maintenance. Maven manages a project's build, reporting and documentation from a central Project Object Model (POM) file. Its goals are to allow developers to understand a project quickly and provide guidelines for best practices. Maven is installed by setting environment variables to point to Java and Maven directories and can build projects with the "mvn clean install" command which cleans, compiles and installs the project.

Uploaded by

tanny1234
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views8 pages

Installation Instructions

Maven is a popular open source build tool for Java projects that takes much of the work out of the build process using a declarative approach. It helps enforce standards and reduces build script maintenance. Maven manages a project's build, reporting and documentation from a central Project Object Model (POM) file. Its goals are to allow developers to understand a project quickly and provide guidelines for best practices. Maven is installed by setting environment variables to point to Java and Maven directories and can build projects with the "mvn clean install" command which cleans, compiles and installs the project.

Uploaded by

tanny1234
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Maven is a popular open source build tool for enterprise Java projects, designed to

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.

Apache Maven is a software project management and comprehension tool. Based on


the concept of a project object model (POM), Maven can manage a project's build,
reporting and documentation from a central piece of information

Maven's primary goal is to allow a developer to comprehend the complete state of a


development effort in the shortest period of time. In order to attain this goal
there are several areas of concern that Maven attempts to deal with:

* Making the build process easy

* Providing a uniform build system

* Providing quality project information

* Providing guidelines for best practices development

* Allowing transparent migration to new features

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

1. Unzip the distribution archive, i.e. apache-maven-3.0-bin.zip to the directory


you wish to install Maven 3.0. These instructions assume you chose
C:\Program Files\Apache Software Foundation. The subdirectory apache-
maven-3.0 will be created from the archive.
2. Add the M2_HOME environment variable by opening up the system
properties (WinKey + Pause), selecting the "Advanced" tab, and the
"Environment Variables" button, then adding the M2_HOME variable in the
user variables with the value C:\Program Files\Apache Software
Foundation\apache-maven-3.0. Be sure to omit any quotation marks around
the path even if it contains spaces. Note: For Maven < 2.0.9, also be sure
that the M2_HOME doesn't have a '\' as last character.
3. In the same dialog, make sure that JAVA_HOME exists in your user
variables or in the system variables and it is set to the location of your JDK,
e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in
your Path environment variable.
4. Open a new command prompt (Winkey + R then type cmd) and run mvn
--version to verify that it is correctly installed.

Building a Project

The vast majority of Maven-built projects can be built with the following command:

mvn clean install

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.

Figure 2. Maven 2 lifecycle phases

Some of the more useful Maven 2 lifecycle phases are the following:

 generate-sources: Generates any extra source code needed for the


application, which is generally accomplished using the appropriate plug-ins
 compile: Compiles the project source code
 test-compile: Compiles the project unit tests
 test: Runs the unit tests (typically using JUnit) in the src/test directory
 package: Packages the compiled code in its distributable format (JAR, WAR,
etc.)
 integration-test: Processes and deploys the package if necessary into an
environment where integration tests can be run
 install: Installs the package into the local repository for use as a dependency
in other projects on your local machine
 deploy: Done in an integration or release environment, copies the final
package to the remote repository for sharing with other developers and
projects
Project Template (Archetype)

Purpose

maven-archetype-archetype:Create your own project template (archetype).

maven-archetype-j2ee-simple:Creates a J2EE project (EAR), with directories and


subprojects for the EJBs, servlets, etc.

maven-archetype-mojo:Create your own Maven 2 plugins.

maven-archetype-quickstart:Simple Java project, suitable for JAR generation.


Maven 2 default.

maven-archetype-site:Documentation-only site, with examples in several formats.


You can run this archetype on top of an existing Maven 2 project to add integrated
documentation.

maven-archetype-webapp:Creates a web application project (WAR), with a simple


Hello World JSP.

Creating a Project

On your command line, execute the following Maven goal:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app


-DarchetypeArtifactId= maven-archetype-quickstart
-DinteractiveMode=false

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

The pom.xml file is the core of a project's configuration in Maven. It is a single


configuration file that contains the majority of information required to build a
project in just the way you want. The POM is huge and can be daunting in its
complexity, but it is not necessary to understand all of the intricacies just yet to
use it effectively. This project's POM is:

<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:

 project This is the top-level element in all Maven pom.xml files.


 modelVersion This element indicates what version of the object model this
POM is using. The version of the model itself changes very infrequently but
it is mandatory in order to ensure stability of use if and when the Maven
developers deem it necessary to change the model.
 groupId This element indicates the unique identifier of the organization or
group that created the project. The groupId is one of the key identifiers of
a project and is typically based on the fully qualified domain name of your
organization. For example org.apache.maven.plugins is the designated
groupId for all Maven plug-ins.
 artifactId This element indicates the unique base name of the primary
artifact being generated by this project. The primary artifact for a project
is typically a JAR file. Secondary artifacts like source bundles also use the
artifactId as part of their final name. A typical artifact produced by Maven
would have the form <artifactId>-<version>.<extension> (for example, myapp-
1.0.jar).
 packaging This element indicates the package type to be used by this
artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact
produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to
use as part of the build process. (The lifecycle is a topic we will deal with
further on in the guide. For now, just keep in mind that the indicated
packaging of a project can play a part in customizing the build lifecycle.) The
default value for the packaging element is JAR so you do not have to specify
this for most projects.
 version This element indicates the version of the artifact generated by the
project. Maven goes a long way to help you with version management and you
will often see the SNAPSHOT designator in a version, which indicates that a
project is in a state of development. We will discuss the use of snapshots
and how they work further on in this guide.
 name This element indicates the display name used for the project. This is
often used in Maven's generated documentation.
 url This element indicates where the project's site can be found. This is
often used in Maven's generated documentation.
 description This element provides a basic description of your project. This is
often used in Maven's generated documentation.

Build the Project

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:

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

Which will print the quintessential:

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!

You might also like