Getting Started with Maven (Creating a Java/Web/Eclipse project)


This tutorial covers following topics

  • Creating a project with Maven.
  • Most common errors and their resolutions that you may encounter during project creation.
  • Introduction to some common Maven commands.
  • Creating a web project with Maven.
  • Creating a WAR file with Maven.
  • Creating an Eclipse project with Maven.

Apache Maven Installation

If you have not set up Maven environment yet, please read the following tutorial before going any further.

http://geekbridage.wordpress.com/2009/02/10/maven-installation-instructions/


  • This tutorial does not cover the introduction, usage, advantages or disadvantages of Maven but simply is a guide for dummies who want to start working in Maven for the first time. You can find a lot of detail on the Internet.
  • Also you will find notes on some steps which will notify you for any further studies.
  • You will also get familiar with common errors and you will be told the solution.
  • At the end you will find some useful links from where you can learn more about Maven.

Creating a Project in Maven


  • First check if maven is installed properly or not with the following command
  • In console window type this mvn –version. You will see something similar to this

Maven version: 2.0.9

Java version: 1.5.0

OS name: “windows xp” version: “5.1″ arch: “x86″ Family: “windows”

  • First create a directory with any name. In my case it isMaven Examples” anywhere on your hard disk.
  • To create a simple Java project, you simply execute the command as shown.
mvn archetype: create -DgroupId=[your project's group id] -DartifactId= [your project's artifact id]


a
. The mvncommand invokes the Maven 2 system
b. request to run the archetype plugin with the create command
c. The -D commands are simply setting Java system properties thereby passing configuration information to Maven 2.
d. groupID should follow the package name
e. The artifactID is specific to each artifact and by convention should be the filename, excluding extension. In this case, we would like to create two artifacts, a JAR file containing the logic and a WAR file containing the web application. First, let’s create the JAR file using the command as shown below.

If you don’t know what is archtype, groupId and artifactId, don’t worry you can find a lot of help on the web. Visit the following link if you are anxious to know about the commands http://maven.apache.org/plugins/index.html


  • Open your console and go the recently created directory i.e. Maven example


t2_1

  • Give the following command on console
mvn archetype:create -DgroupId=com.dps.maven2example -DartifactId=maven2example_logic


write the whole command in one line


  • Provide the above command and press “Enter”. This may take a lot to time. This command may download a lot of dependencies to the maven repository. So let the maven do its job.


Maven repository is mostly located in your_home_folder/.m2 folder. In my case it is C:\Documents and Settings\ali.hammad\.m2. In Linux platform it may be located in your home folder but may be hidden.


You may get following error at this point

If your machine is behind a proxy, maven will be unable to download the dependencies. In that case you may receive an error similar to this one

Reason: Unable to download the artifact from any repository

org.apache.maven.plugins:maven-resources-plugin:pom:2.2 from the specified remote repositories: central (http://repo1.maven.org/maven2)


Problem Resolution


To remove this error go to your maven home directory, you will find a directory with the name “Conf”. Under “Conf”, you will find a file “settings.xml “. Open the file and uncomment the following portion and provide your proxy IP/Port to make maven work.


optional
true
http
192.168.200.16
8080
127.0.0.1



t2_2


  • After executing the command in step 6, following project hierarchy will be automatically created.

t2_3


  • This is an empty project will do nothing as such. This file is essentially analogous to an IDE project file: it contains all of the information about a project, and is the file that Maven 2 uses to act upon to execute commands. The contents of pom.xml:
  • Now lets learn a few maven commands. Execute the lifecycle command mvn test on our pom.xml file.

  • cd” to the project folder i.e. maven2example_logic and execute the following command
mvn test

> Your output may differ from mine, because maven may download some dependencies from the web.

> You may also get an error some errors which are described after the screenshot.


t2_4


You may encounter this error


[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Cannot execute mojo: resources. It requires a project with an existing po
m.xml, but the build is not using one.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Mon Feb 09 13:47:11 GMT+05:00 2009
[INFO] Final Memory: 2M/4M
[INFO] ------------------------------------------------------------------------

Problem Resolution:

This means you are not in the right directory. Just “cd” to the inner directory. In my case I created a directory “Maven Examples” and then ran the above command. That command created a maven project “maven2example_logic”. So cd to maven examples then maven2example_logic and run the command again.


  • Executing the test command will create a “target” directory along with the “src” directory which will contain classes directory.


t2_5




Creating a Web Project


  • Move one step up in the directory structure. So you must be the main directory created at step #2. In my case its “Maven Examples”


t2_6


  • Issue the following command to create a web project

mvn archetype:create -DgroupId=com.dps.maven2example -DartifactId=maven2example_webapp -DarchetypeArtifactId=maven-archetype-webapp


You may get following error at this point

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building maven2example_logic
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: com.dps.maven2example
[INFO] artifact org.apache.maven.archetypes:maven-archetype-webapp: checking for
updates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arc
hetype-webapp/1.0/maven-archetype-webapp-1.0.jar
3K downloaded
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating OldArchetype: maven-archetype-web
app:RELEASE
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.dps.maven2example
[INFO] Parameter: packageName, Value: com.dps.maven2example
[INFO] Parameter: basedir, Value: C:\Documents and Settings\ali.hammad\Maven Exa
mples\maven2example_logic
[INFO] Parameter: package, Value: com.dps.maven2example
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: maven2example_webapp
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error creating from archetype

Embedded error: Unable to add module to the current project as it is not of pack
aging type 'pom'
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Mon Feb 09 14:29:04 GMT+05:00 2009
[INFO] Final Memory: 7M/13M
[INFO] ------------------------------------------------------------------------


Problem Resolution:

You are in the wrong directory. Move one step up in the hierarchy. Refer to step #11


  • You will see something similar to the following figure


t2_7


  • The above command will setup a web project.

t2_8



Creating WAR File


  • Go to the newly created web project and issue the following command
mvn package


This will create a war file in the “target” folder under web project.

Your output will be similar to the following


t2_9


t2_10



Creating an Eclipse Project with Maven


  • In the web project type the following command and your eclipse project will be created.
mvn eclipse:eclipse


Further Reading

http://today.java.net/pub/a/today/2007/03/01/building-web-applications-with-maven-2.html, Building Web Applications with Maven 2

http://maven.apache.org/guides/getting-started/index.html, Maven Getting started Guide

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html, Maven in 5 Minutes

0 comments for this post

Post a Comment

Blog Widget by LinkWithin