How to Use Maven on Your Project

Install Maven

  1. Obtain Maven (version2.0.5) from http://maven.apache.org/download.html
  2. Unzip maven-2.0.5-bin.zip to the directory you wish to install Maven.
  3. Add the bin directory to your path, eg. SET PATH=”C:Program Filesmaven-2.0.5bin”;%PATH%
  4. Make sure that JAVA_HOME is set to the location of your JDK.
  5. Run mvn –version to verify that it is correctly installed.

Modify maven’s settings.xml to reference internal Intelliware repositories.

  1. Overwrite a standard settings.xml in <maven install directory>/conf with this one: settings.xml.

Use Maven to create an application

Maven provides an archetype mechanism to allow you to quickly create different kinds of projects. Some common archetypes are:

  • maven-archetype-webapp for web applications
  • maven-archetype-simple – quick start archetype to generate simple project
  • maven-archetype-site archetype – to create a site docummentation for your project

For a more comprehensive list see Codehaus’s Archetypes List
You can also create your own archetypes. To do so, refer to maven’s Guide to Creating Archetypes

Example: Creating a web application

Run a following command in the directory where you want your project to be created:

mvn archetype:create
     -DgroupId=ca.intelliware
     -DartifactId=simpleWebApp
     -DarchetypeArtifactId=maven-archetype-webapp

The result is:

simpleWebApp
+-- pom.xml
+-- src/
	+-- main/
	 	+-- resources/
	 	+-- webapp/
			+-- index.jsp
			+-- WEB-INF/
				+-- web.xml

The first time you run this (or any other) command, Maven will need to download all the plugins and related dependencies it needs to fulfill the command. Therefore it might take some time when you run it for the first time.

Maven uses the concept of repositories to manage your dependencies. When you download maven and install it, it creates your local repository. When we say “maven downloads dependencies” we mean it gets them from the maven repo and copies them to your local repository.

Example: Creating a simple application:

mvn archetype:create -DgroupId=ca.intelliware -DartifactId=simpleWebApp

Making sense of pom.xml

When you’ve used maven to create your project, notice that it created a pom.xml file in your main project directory. This file (arcronym of Project Object Model) contains every important piece of information about your project and is essentially one-stop-shopping for finding anything related to your project. To learn more about it, visit Introduction to the POM.

For an example of a POM file that includes configuration for the Intelliware Maven infrastructure see: Sample POM for Intelliware.

Maven on a roll

Now that we have our project created, we can add in our code and utilize a whole new bag of Maven tricks.

Noteall Maven commands must be run in the same directory as the project’s pom.xml file.

  • mvn test: Runs the JUnit tests for the application.
  • mvn package: Creates a .war file from our project.
  • mvn install: Adds our project .war to the repository for use as a dependency if needed.
  • mvn site: Generates the project website.
  • mvn clean: Cleans the output created in the target directory.
  • mvn eclipse:eclipse: Generates an Eclipse project file.

Where, “test”, “package”, and “install” are Maven lifecycle phases, and “site” and “eclipse” are Maven plugins.

Deploy to WebServer and Run

The Quick and Easy Way

We can now deploy the application to the Jetty webserver and run it by entering:

mvn org.mortbay.jetty:maven-jetty-plugin:run

We can then hit the page and see our application in action:

http://localhost:8080/simpleWebApp/

Defining the Webserver in your pom.xml

Specify the plugin for your web server in the project’s pom.xml (the example below is for Jetty):

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
</plugin>

We can then startup the web app by running:

mvn jetty:run

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

One thought on “How to Use Maven on Your Project

  1. Pingback: Maven FAQ | i-Proving –

Leave a Reply