Maven FAQ and code snippets for beginners

I have been using Maven with eclipse for two years and have found many simple and useful maven plugins and features like automatically downloading java sources, javadocs, running in an embedded container, importing the project to eclipse. Here’s a list of some useful code snippets and use case scenarios that I hope other maven beginners would find useful.

Download maven from

Install maven on your local machine

The simplest way to install is to download maven and unzip it in /usr/local/maven-X.X.X (where X.X.X denotes the appropriate version)

Follow this guide:

Create a new java project

mvn archetype:create -DartifactId=my-app

Create a new webapp project

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany -DartifactId=my-app


Go to the top level directory which contains pom.xml. Run the following command:

 mvn compile


Run unit tests

mvn test

How to make the unit tests in maven run in the integration-test phase

The following configuration:

  • In maven:test phase: runs nothing
  • In maven:integration-test phase: runs both unit tests, and (selenium) integration tests
<!-- let surefire plugin be executed during the integration-test phase,
the selenium server is started by that time -->



Create a distribution

mvn package


To install the generated output in the repository, run the following command:

 mvn install

Install a jar into your maven repository

Adjust the following command to match your jar name, path name etc.

mvn install:install-file
    -Dversion=1.0.0 -Dpackaging=jar

Generating an eclipse project from a maven project

mvn eclipse:eclipse

If you app is a Dynamic Web Project, try the following option:

mvn -Dwtpversion=1.0 eclipse:eclipse

Set a default version of eclipse 1.0


Set download sources option in generated eclipse project



Install maven plugin in Eclipse

Goto Help -> Software Update -> Find and install -> Search for new Features to install -> New remote Site
Name: Maven

Import your maven project in Eclipse

# Eclipse -> File -> Import -> General -> Existing project into Workspace -> Select root directory ->

Tell eclipse your maven2 repository’s location

Goto Window -> Preferences -> Java -> build path -> classpath variable -> New
Name: M2_REPO
Path: /path/to/your/.m2/repository



Deploying to tomcat

mvn tomcat:deploy



Reference documentation here:

Installing Jetty

   <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">

Starting jetty through cargo





Sample jetty env.xml

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <!-- Add an EnvEntry only valid for this webapp               -->
  <New id="gargle"  class="">
    <Arg type="java.lang.Double">100</Arg>
    <Arg type="boolean">true</Arg>

 <!-- Add an override for a global EnvEntry   -->
  <New id="wiggle"  class="">
    <Arg type="java.lang.Double">55.0</Arg>
    <Arg type="boolean">true</Arg>

  <!-- an XADataSource                        -->
  <New id="mydatasource99" class="">
     <New class="com.atomikos.jdbc.SimpleDataSourceBean">
      <Set name="xaDataSourceClassName">org.apache.derby.jdbc.EmbeddedXADataSource</Set>
      <Set name="xaDataSourceProperties">databaseName=testdb99;createDatabase=create</Set>
      <Set name="UniqueResourceName">mydatasource99</Set>

Sample jetty

#username: password[,rolename ...]

How to launch jetty

mvn jetty:run

Enabled Java 1.5



Here’s a good Maven Selenium reference page for beginners.

Installing Cargo

Add this plugin repository:

   <id>codehaus snapshot repository</id>

How do I document my maven plugin

See my blog post on Documenting your Maven plugin

Related posts:

  1. JAXB code snippets for beginners
  2. Maven Selenium
  3. The plugin ‘org.codehaus.mojo:selenium-maven-plugin’ does not exist or no valid version could be found
  4. Tweet your builds with Maven Twitter Plugin
  5. 3 ways to run Java main from Maven

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wordpress themes