Maven Java and Eclipse Tutorial For Beginners

What Is Maven?

Maven is a build and management tool for Java projects. Maven was originally created to create a simple and consistent build process for java projects, so that each time a projects code base was built it would be the same everytime and that build would be easy to manage.

To make the build process simple and to ensure it follows a standard, Maven has the concept of the Project Object Model, or commonly know now as the POM. The POM is combined with various plugins to create that simplified build configuration and process.

In general, for someone to understand the build process of your project all they should have to do is look at the contents of your projects POM.

How To Run A Maven Project By Example

As a quick example, lets look at how we get a project setup, and then create an executable build that we can use to demonstrate easily the use of Maven and some of the things it can do.

In this example we will create our code in eclipse, then create junit tests to test it. We will then proceed to us Maven to create a jar file, edit the manifest of the jar file, and bundle our jar, the dependencies and other resources into a zip file.

We will assume you have setup your development environment already with Maven, and have followed steps similar to my previous development environment setup article, http://colwil.com/setting-up-your-eclipse-java-development-environment/ . Once you do that you should have a basic pom.xml in the root of your project.

So heres our basic project structure in eclipse that we will start off with

If we start off with a basic HelloWorld class to get us going which will be as below:

The screenshot of the code shows that the Logger class is in error. This is because we havent built the new project based on our POM yet, so the dependencies are not resolved. So lets look at the basic POM we need, and then run it so that we can get all our dependencies sorted out. The POM looks like this…

Our groupId, artifactid and version define our project in Maven speak. And then the dependencies show which public projects we are going to depend on in our project. So lets execute the POM so our project gets built properly. Click on Run, Run Configurations in eclipse which will show us a list of configs and we select the relevant maven build config that we copied earlier.

Maven Run Configurations

And here is our maven run config, with our Maven goals that ensure that the projects gets built based on the POM and the dependencies get downloaded.

So lets run that and update the project. We can see the log from the run below. The project gets identified with the details we specified in the POM

The Maven eclipse plugin sorts out the eclipse project structure such as resetting the project layout and classpath to ensure the classpath includes the dependencies.

Any additional dependencies are downloaded, any that are already in you repository will be added. The repository is where Maven stores all the dependencies you have ever needed in one location that you specify in the maven install config. If you are using maven that is built into eclipse, then your repository should be in your windows user folder somewhere.

Once the configuration has finished then you should see a Build Success at the end of the log

So if we look at our project layout in Eclipse now it should look a little different. And as we can see the project now shows the dependencies we had in the POM.

Leave a Comment

Your email address will not be published. Required fields are marked *