Why Maven? (Java Why? Article Series 4)

Hüseyin K.
5 min readNov 25, 2022

--

Hi Java people. Ready for analyzing why we need Maven?

I am Huseyin. Have been a .Net developer for years and Java backend and Android developer for 6 years as well as a QA Automation Engineer team member for several projects for 3 years.

Have been observing that some of my friends , especially junior developers, are not completely aware of the point of using a structure a tool. As a developer, by experiencing the reusability, abstraction and the maintainability issues for years, I decided to implement the same structure in my life and abstract myself from answering similar questions 🙂. Thus, put my effort into creating this “Why Java? Article Series”. I just wanna to help developers to code consciously.

The target audience of “Why Java? Article Series” is the ones who already know coding with Java, but also want to understand the underlying language and the system.

Main purpose of these articles is to understand why we need some structures or tools in our framework in order to be conscious while using them. Of course there are a lot of sources about Java on the internet. What I experienced is that some different approaches while explaining the same thing helps people to understand the topics. What I am trying to do is actually that. A different kind of explanation from WHY point.

Articles are prepared to achieve the understanding of the topic from a step by step approach.

If these articles help you to understand a concept, I definitely feel very happy. Feel free to share and comment.

Sharing is learning at the same time! Enjoy.

I need a ready code

I have a lot of work to do.

At some point, I needed to check if a number is power of two.

Since I have a lot to do, I do not want to spent time on that issue. Rather than that, I decided to search if there is a pre-written code snippet.

If I wrote a method to find whether a number is power of two or not, I would name it as “isPowerOfTwo”. So I decided to search these words on Google; “isPowerOfTwo java method” is searched;

I clicked in the first search result

The explanation was mentioning Guava. What is this Guava?

I searched the net and found that this was a library made by Google, that includes a lot of ready methods for facilitating developers work.

In order to use the “isPowerOfTwo” method of Guava Library, first, I should add it to my project. That is not like adding a regular file.

Guava in mvnrepository.com

I knew that MvnRepository.com is hosting such kind of ready libraries. So, I checked if Guava is included in the mvnrepository.com.

Bundle is downloaded

Yes, it is. Then I downloaded the latest version jar file of Guava Library.

Manually Adding A Library To Java Project

Since I do not use any build tool/dependency manager (like Maven), I need to download the jar file and add it to my project as a dependency.

In IntelliJ, navigated to File>Project Settings>Modules>Dependency;

Then I added the jar file that I downloaded from mvnrepository.com;

I checked the website that explains my requirement. And, I found this code snippet that uses isPowerOfTwo method of the IntMath class which belongs to Guava Library;

Let’s use this isPowerOfTwo method in my project like the example above;

Voila! Works well. I checked the External Libraries. Guava was added to my project as a jar file;

Not enough!

You know what? I really bored with downloading jar files and uploading to my project as dependency.

Rather than downloading, I decided to create a new Maven project and let the maven do all these things with a couple of lines.

Moreover, I really do not know if there is a new version of the Guava Library is published. In order to check this I should manually search Guava on mvnrepository.com and I should compare the versions. If there is a new one I have to repeat the same process when I did for the first jar file addition.

Maven power!

In order to learn how to create a Maven project you can check this link.

After creating a Maven project I checked the mvnrepository.com for Guava, and copied the Maven content (red rectangle) for adding it to my POM.XML file which is the heart of my Maven Project for build and dependency management.

Adding dependency to POM

After pasting the copied content between “dependencies” tags, then I need to click the refresh button to let the maven download and add the Guava Library to my project.

Guava is added through Maven

After refreshing Maven, I checked the External Libraries, I saw a couple of new libraries were added to my project which are somehow related to Guava and Guava it is.

Ultimate Voila!

I was able to use Guava classes and methods that bring me a lot time and efficient code;

This is not the only way but the most effective way, since it is much easier than manual jar file addition.

Thanks for reading. Hope this helps! See you in the next articles.

PS: Really benefited from especially Baeldung and other Java Tutorial Websites.

Dedicated to Dirty Java Rangers

--

--