[Java Web] – Maven – What the heck is it?

Maybe some of us have known about Maven, some of us just have heard of it and think it is something more highly technical. Nahh. It is magic, but not that magic. In today’s post, I’ll explain Maven in simple words so that you have an idea about it. Let’s get started.

What is Maven?

Imagine you are working on a web project, it requires you to install so many libraries in the type of jar.files, what would you do? You would have to download jar files from the internet, then add them to the JRE System Library, coupled with declaring ClassPath, sometimes they cause conflicts between different versions of different libraries.

Add .jar files to JRE System library


Well, you don’t have to worry about that anymore. Maven was born to handle that challenging job. It is a build automation/ project management tool for Java projects. Very simple, isn’t is? It’s just a tool to help you automatically add libraries (or dependencies) for the project. So when you create a Maven project, it simply means you are creating a Java web project with the support of Maven to add and manage dependencies for the project.

Maven Mechanism

There is a file named pom.xml in a Maven project where you can declare all the things related to the project such as dependencies, version, project name, etc.

For example, in file pom.xml of this project, I declare the dependencies for java servlet, jsp and jstl tag. But I don’t need to download .jar files of those libraries. This is how it works:

1. I declare in pom.xml that my project has the libraries java servlet, jsp, and jstl with the respective versions (or our project depends on those libraries).

Declaring dependencies in pom.xml

2. I save file pom.xml, Maven will check whether those libraries have been available on our Local Repository or not. If they haven’t, Maven will automatically download from the Repository on the Internet.
3. Maven itself will declare ClassPath for our Project to file .jar that was download. Now we can see the file in Maven Dependencies.

Maven Dependencies

This picture from 07planning can explain the mechanism of Maven as discussed above.

Maven Mechanism (o7planning)

Where is the Local repository?

As mentioned above, Maven will download .jar files from the Internet and store it in our Local repository (in case Local repository does not have them yet). So the question is? Where is the Local Repository?

In Eclipse IDE, choose Tab ‘Window’ -> ‘Preferences’ -> In the new Preferences Window, choose Maven -> User Settings. Now you can see it stores in a folder called .m2/repository.

Local Repository

That’s for today. Happy coding.

————–

References

o7planning.org

daynhauhoc.com

maven.apache.org

Leave a Reply

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