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


Author: Meter Down

This tutorial covers following topics

  1. Creating a project with Maven.
  2. Most common errors and their resolutions that you may encounter during project creation.
  3. Introduction to some common Maven commands.
  4. Creating a web project with Maven.
  5. Creating a WAR file with Maven.
  6. Creating an Eclipse project with Maven.

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

This tutorial does not cover the introduction, usage, advantatges and disadvantages or Maven but simply a guide for dummies who want to start working in Maven for the first time. You can find a lot of detail on internet.

Also you will find notes on some steps which will notify you for any further studies.

At the end you will find some useful links from where you can learn more about Maven.

Creating a Project in Maven

1- 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”

2- First create a directory with any name. In my case it isMaven Examples” anywhere on your hard disk.

3- 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.

Note: 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

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


5- Give the following command on console

mvn archetype:create -DgroupId=com.dps.maven2example -DartifactId=maven2example_logic

Note: write the whole command in one line

6- 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.

Note: 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 (

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.

<proxies> <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username></username> <password></password> <host></host> <port>8080</port> <nonProxyHosts></nonProxyHosts> </proxy> </proxies> 


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


8- 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:

9- 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.


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.

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


Creating a Web Project

11- 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”


12- 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: 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

13- You will see something similar to the following figure


14- The above command will setup a web project.


Creating WAR File

15- 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


Creating an Eclipse Project with Maven

16- In the web project type the following command and your eclipse project will be created.

 mvn eclipse:eclipse 

Further Reading, Building Web Applications with Maven 2, Maven Getting started Guide, Maven in 5 Minutes


No Responses Yet to “Getting Started with Maven (Creating a Java/Web/Eclipse project)”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: