Getting started with Apache Maven and Eclipse

Apache Maven is a popular build automation tool, for large Java Projects.

All major Java IDEs (IntelliJ IDEA, Eclipse and Netbeans) fully support Maven integration, by default or via plugins.

In order to get started with Maven you will need to:

1. Download Apache Maven from the original site, which is: http://maven.apache.org/ . Check for the Download section:

Image 1

2. Extract the .zip file in your desired location (Eg. D:javatoolsapache-maven-3.1.0 )

Image 23. The next step will be to setup the following system variables M2, M2_HOME, JAVA_HOME and also add the Maven bin directory to System Path. For managing the System Variables on my station I am using a dedicated software called Rapid Environment Editor:

Image 3

 

If you don’t want to install Rapid Environment Editor, you can use the Windows built-in feature to edit System Variables: Control Panel -> System and Security -> System -> Advanced System Settings -> Advanced / Environment Variables .

Image 4

 

4. Modify the location of Maven repository – this is the location where Maven installs all its artifacts (jars, etc.) . By default the location for this folder is ${user.home}/.m2/repository . On Windows machines this may raise issues, as most users keep their C: partition small enough and dedicated only for their Operation System. In order to modify this behavior edit %M2_HOME%\\confsettings.xml . In my case this location is D:\\java\\tools\\apache-maven-3.1.0\\conf\\settings.xml. The property you need to modify is called: localRepository :

Image 5

 

In my case the new value for this property is: D:\\java\\tools\\apache-maven-3.1.0-repo .

5. The next step will be to test if maven was successfully installed. Open up a Command Prompt and type:

If you’ve followed all the above mentioned steps the output should look like:

Image 6

 

6. One of the cool features of Maven are the so-called Archetypes . Those are project structure / architecture templates that can save you a lot of time when you are defining boilerplate-like code. Right now Maven offers am impressive list of archetypes for defining all kinds of projects: JDBC Projects, Spring Projects etc. But in this tutorial we will use this feature in order to create a simple Java Project that are we going to import later into Eclipse. 

In order to create a simple project use this command:

  • groupId is be the package name ;
  • artifactId is the name of the project ;
  • archetypeArtifactId is the type of the archetype (in our case a simple Java Project).

If everything was successful (after some downloads in your repository) you will probably see something similar to :

Image 7

 

If you analyze your folder structure you will see something like this:

Image 2

 

7. The last step will be to generate the necessary descriptors for Eclipse so that our newly generated project skeleton will be recognized as an Eclipse project. In the project root folder:

Image 3

 

 

8. The next step will be to import the project into Eclipse (File -> Import -> Existing Projects into Workspace):

Image 4

 

 

And everything is done.