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 apache.org

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: http://maven.apache.org/guides/mini/guide-configuring-maven.html

Create a new java project

mvn archetype:create -DgroupId=com.mycompany.app -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

Compile

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

 mvn compile

Testing

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 -->
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>

 <configuration>
  <skip>true</skip>
 </configuration>

 <executions>
  <execution>
   <phase>integration-test</phase>
   <goals>
    <goal>test</goal>
   </goals>
   <configuration>
    <skip>false</skip>
   </configuration>
  </execution>
 </executions>
</plugin>

Create a distribution

mvn package

Install

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
    -Dfile=/path/to/your/jar/your.jar
    -DgroupId=org.your.group.id
    -DartifactId=your-artifact-id
    -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

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-eclipse-plugin</artifactId>
 <configuration>
  <workspace>${basedir}</workspace>
  <wtpversion>1.0</wtpversion>
 </configuration>
</plugin>

Set download sources option in generated eclipse project

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-eclipse-plugin</artifactId>
 <configuration>
  <workspace>${basedir}</workspace>
   <downloadSources>true</downloadSources>
 </configuration>
</plugin>

Eclipse

Install maven plugin in Eclipse

Goto Help -> Software Update -> Find and install -> Search for new Features to install -> New remote Site
Name: Maven
URL: http://m2eclipse.codehaus.org/

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

Settings.xml

Reference: http://maven.apache.org/maven-settings/settings.html

Deploying to tomcat

mvn tomcat:deploy

Reference: http://mojo.codehaus.org/tomcat-maven-plugin/deployment.html

Jetty

Reference documentation here: http://docs.codehaus.org/display/JETTY/Jetty+Documentation

Installing Jetty

<plugin>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>maven-jetty-plugin</artifactId>
 <configuration>
  <jettyEnvXml>${basedir}/src/test/resources/jetty/env.xml</jettyEnvXml>
  <userRealms>
   <userRealm
     implementation="org.mortbay.jetty.security.HashUserRealm">
     <name>YourRealm</name>
     <config>${basedir}/src/test/resources/jetty/security.properties</config>
   </userRealm>
  </userRealms>
  <connectors>
   <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
    <port>8080</port>
    <maxIdleTime>60000</maxIdleTime>
   </connector>
  </connectors>
 </configuration>
</plugin>

Starting jetty through cargo

<plugin>
 <groupId>org.codehaus.cargo</groupId>
 <artifactId>cargo-maven2-plugin</artifactId>
 <version>0.3-SNAPSHOT</version>
 <configuration>
  <wait>false</wait>

  <container>
   <containerId>jetty6x</containerId>
   <type>embedded</type>
  </container>

  <configuration>
   <properties>
    <cargo.servlet.port>9090</cargo.servlet.port>
   </properties>
  </configuration>

 </configuration>
</plugin>

Sample jetty env.xml

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

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

  <!-- an XADataSource                        -->
  <New id="mydatasource99" class="org.mortbay.jetty.plus.naming.Resource">
    <Arg>jdbc/mydatasource99</Arg>
    <Arg>
     <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>
     </New>
    </Arg>
   </New>
</Configure>

Sample jetty security.properties

#username: password[,rolename ...]
admin:admin,admin

How to launch jetty

mvn jetty:run

Enabled Java 1.5

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
  <source>1.5</source>
  <target>1.5</target>
  <verbose>true</verbose>
 </configuration>
</plugin>

Selenium

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

Installing Cargo

Add this plugin repository:

<pluginRepositories>
 <pluginRepository>
   <id>codehaus snapshot repository</id>
   <url>http://snapshots.repository.codehaus.org/</url>
   <releases>
     <enabled>true</enabled>
   </releases>
 </pluginRepository>
</pluginRepositories>

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

1 comment to Maven FAQ and code snippets for beginners

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 wpburn.com wordpress themes