ZOFTINO.COM android and web dev tutorials

Using Maven in Eclipse

In this tutorial, you can learn how to use Maven Eclipse plug-in to create, build and package java projects.

Installation of Eclipse

Download and install latest version of JDK.

Set JAVA_HOME environment variable to point to the folder where JDK is installed.

Download the latest version of eclipse from eclipse.org.

After downloading the eclipse exe, click it to start eclipse installer.

eclipse installer

Choose eclipse IDE for java developers options. In the next window, select folder where you want eclipse to be installed and then click install button.

eclipse installation folder

Then accept terms to start eclipse installation. After installation, start eclipse IDE and select the workspace folder.

By default Maven Eclipse plug-in is installed with eclipse. If it is not installed, go to Help menu, click Eclipse Marketplace, search for Maven and install the plug-in.

eclipse marketplace maven plug-in

Configuring Maven

Eclipse maven plug-in comes with maven, but if you want to use the latest version of maven, you can configure the plug-in to use different version than the embedded maven runtime. To do that, you need to go to window menu, click preferences, expand maven, click installation and then click add.

setting maven runtime

New runtime window will be displayed where you need to select maven installation directory that you want to use and click finish.

select maven runtime in eclipse

Then check the newly added maven runtime and click apply.

Similarly, you can import archetype and templates and change user settings.

Creating Maven Project in Eclipse

To create maven project in eclipse, go to file, new and click other.

create maven project in eclipse

In the next window, select maven project option.

select maven project option

In the next window, if you want to create the project in different location than eclipse workspace, you can select the location here. Then click next.

create maven project eclipse project name location

In the next window, you can select maven archetype. Since we are going to create simple Java project (jar project), we will select quickstart archetype and then click next.

create maven project eclipse select archetype

In the next window, enter group id, artifact id and package and then click finish.

create maven project eclipse enter group id artifact id package

It will create maven jar project with project structure shown in the following image.

maven jar project structure

Adding Dependencies

If you want to use other jar files in your project, you will have to add them as dependencies in Maven pom.xml. To do that in eclipse, you need to open your project’s pom.xml, click dependencies tab and then click add.

eclipse adding maven dependencies

In the select dependencies window, enter group id, artifact id, and version. These details of jar can be obtained from maven central repository web site. After entering dependency details click ok and save pom.

eclipse maven select dependencies

Build and Package

To do a build of the project, right click the project, click run as, select maven build option.

eclipse maven build project

In the edit configuration window, enter maven goals that you want to execute such as clean, compile, test, package, etc. and click run.

eclipse maven project build

After executing package goal, you can find the package, in our case it is a jar, in the target directory. You need to refresh the project if you don’t see it. Following image shows the package and downloaded dependencies after the build.

eclipse maven package in target directory and downloaded dependencies