Create a simple Java project with Maven and Eclipse

Posted on 19-02-2013 20:26 by graham
This tutorial shows you how to create a simple console application using Maven and integrate it with the Eclipse IDE.

Install Maven


If you haven't done it yet, install Maven as described on the Maven home page.

Create your project


Choose a directory that will be used as the Eclipse workspace and that will contain your project. For example, if you are planning to create a project called my-pet-shop, it's a good practice to put it into a folder called my-pet-shop-workspace. This is a pattern I follow to make sure I can always identify the location of my projects.

Go to the folder my-pet-shop-workspace and type the following command (for Maven version earlier than 3.0.5)
mvn archetype:create -DgroupId=com.your.domain -DartifactId=my-pet-shop

For Maven 3.0.5+ use:
mvn archetype:create -DgroupId=com.your.domain -DartifactId=my-pet-shop


The output of the command will look more or less like this:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-archetype-plugin:2.2:create (default-cli) @ standalone-pom ---
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: com.your.domain
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/maven-metadata.xml
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/maven-metadata.xml (531
B at 1.7 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.your.domain
[INFO] Parameter: packageName, Value: com.your.domain
[INFO] Parameter: package, Value: com.your.domain
[INFO] Parameter: artifactId, Value: your-project-name
[INFO] Parameter: basedir, Value: D:\dev\rk-workspace\test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\dev\my-pet-shop-workspace\my-pet-shop
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.615s
[INFO] Finished at: Tue Feb 19 20:33:58 CET 2013
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------

This will create a folder called my-pet-shop in the current directory. The folder will contain the basic file structure for your new project.

Prepare an Eclipse project


For the project to be used with Eclipse, do the following. Change your working directory to my-pet-shop:
cd my-pet-shop

and then run
mvn eclipse:eclipse

This will generate some configuration files that are used by Eclipse and identify your application as a valid Eclipse project.

Build your project


To build your project, run:
mvn package

Import the project into Eclipse


Open Eclipse IDE and select my-pet-shop-workspace as the workspace when the IDE starts. Then go to File | Import | General | Existing Projects into Workspace and click Next. Select the option "Select root directory", browse through your files and select the folder my-pet-shop. Once you select it, it should appear in the "Projects" box. Click Finish.

After this you should be able to see your project imported into Eclipse.

Attach Maven sources


Although the project is already attached, and the build you ran before was executed properly, you may see some missing libraries on your project. This is because Eclipse is not aware of the local Maven repository into which Maven stores all the downloaded libraries.

What we need to do is to point Eclipse to the location of the Maven repository on your computer. It is usually located under <your-user-home>/.m2/repository.

To configure it, go to Project | Properties | Java build path. Open the Libraries tab, click Add Variable | Configure Variables | New. Once you are about to create a new variable, give it the name M2_REPO and point it to the Maven repository location on your computer, i.e. to <your-user-home>/.m2/repository. Save with OK, refresh your project by right-clicking on it and clicking "Refresh". The errors caused by missing libraries should be gone now.
Comments
Im grateful for the blog article.Thanks Again. Awesome.
Added on 06-06-2013 19:24 by anonymous

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems