Maven FAQ

From Foochal

Jump to: navigation, search

Contents

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

Enter:

Name
Maven
URL
http://m2eclipse.codehaus.org/

Import your maven project in Eclipse

  1. Eclipse -> File -> Import -> General -> Existing project into Workspace -> Select root directory -> <your root diretory containing generated .settings, .classpath and .project)

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

http://wiki.foochal.org/index.php/Maven_Selenium

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>

Add this plugin

<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>8080</cargo.servlet.port>
    </properties>
  </configuration>
 </configuration>
</plugin>

Personal tools