Using third party libraries in your Java Project? You need Maven


You’re working on a Java project that does web scrapping from the web. You’re not a ninja and you deeply believe in not re-inventing the wheel. So you decided to use a third party library to assist you instead of writing raw Java code and do all the hard work yourself. One of the most popular and easy to use Java libraries out there for web scrapping is JSoup. Let’s say you’re using JSoup. Now the question is, how can you use that in your project? Copy paste code from their site? How can you be sure that your copy paste is exactly done in the way the developer structured the library? So — Bad idea. Let’s check out the ways we can integrate JSoup or any other third party library in a Java project

2 ways to add third party libraries or “modules”

Let’s call them modules from now on. So there are two ways in Java to accomplish this feat.

  1. Download the compiled JAR of the module, from the developer website
  2. Or use a build tool that can manage module and dependencies for you.

Why I don’t prefer the “JAR JAR way”

There are many reasons. One is — JAR files don’t really help if you want to keep your dependencies (the third party modules you’re going to use, and whatever code your project depends on) updated, you’ve to download the JAR file over and over again. And it’s not a smart thing to keep track of updates every now and then. Also it may also happen that your project needs a very old version and nobody has the JAR for that. You’re screwed man! Again, what if you want to use a specific version of that module until DOOMSDAY comes? Take another case, you want somebody else to work on your project and they don’t have the JAR. They’ll have to download then add and then look for if anything breaks your project or shows errors and etc. Waste of time and potential human resource.

You need a build tool, you need help in managing your dependencies.

Translation from Gungan : You need maven young padawan!

[If you’re yet to understand the meme reference, let me movie shame you, your life’s a lie without watching Star Wars]

What is a build tool anyway?

In simple terms a build tool is something that manages the dependencies, compiles, runs unit tests for your project so you don’t have to do everything by hand. Saves time and you can focus more on your project rather than the mice under the table.

Where does Maven fit in?

Java has several build tools. Each have their own set of benefits and demerits. Popular ones are Ant, Maven, Gradle.

Ant is more suited for legacy Java Projects and configurations for Ant is difficult to write. Maven is modern and is easier to write and manage as well. Also maven has a huge repository of modules so you never have to worry about missing packages. Gradle on the other hand is used by Google for Android. If you’ve ever worked with Android Studio, you’ll know how Gradle does your job in the background.

Let’s get to work, installing Maven

Maven comes bundled with Intellij IDEA and Eclipse, also you can install additional plugins for both the IDE’s to manage Maven. If you’re on an POSIX based OS (GNU/Linux/macOS) you can install Maven using a package manager. On Windows? Just use the IDE. I’d say, just use the IDE bundled packages, no need to take the hassle of doing everything from the command line!

You can search for modules and how to integrate them in your project from the Maven Website.

Creating the project

I’ll be using Intellij IDEA for this one. You can use Eclipse, the procedure is similar, sort of.

Let’s start by creating a maven project.

Make sure you select “Create from archetype” and from the options select “maven-archetype-quickstart”. An archetype provides a template with proper directory structures so you don’t have to worry about setting them up.

GroupId is your module name and ArtifactId is your project name. Add a version number if you want to!

I’m using the bundled version of maven with Intellij IDEA, so my project window would look like this. Yours may look different.

Select your project directory and click finish.

Let Maven download and build a few things

Maven will need to download and compile a few things to get working, so give it a few minutes to set up your project and sources accordingly. Make sure you click on enable auto import of modules when maven asks so. While maven is busy, let’s have a look at the project directory. It looks like this.

What’s a pom.xml?

Glad you asked. pom.xml contains the information for maven to build and run your project. So it’s a configuration file for maven, and the modules you want to add, their information goes here as well.

A look inside the pom.xml after maven is finished building things

In case you want to rename your module name/ project name you can still do it from here. Now what we’re interested here is the dependencies block. This is where we’ll be telling maven that we need to add a module to our project.

Let’s get in and add the dependency!

Now If you look closely, the dependencies block has a well defined syntax to add any dependency. Let’s have a look. We’ve JUnit added here by maven, so it has a groupId, artifactId, version and scope. These sound familiar? Well your project also has these properties. Means if you publish your project to maven repositories, people will just need these properties to add your code in their project. Awesome!

So roughly speaking we need these to add a dependency in the dependencies block. You can ignore the scope or just add test.

Time to add JSoup properties here. But wait a moment, where will you find these properties? Well Maven Indexes! Link :

Searching for jsoup will give you the module you’re looking for.

Click on the link and click on the version you want to use in your project. I’ve picked 1.11.3. So inside the page for 1.11.3 I have this section —

Copy the code under Maven section and add it inside <dependencies></dependecncies> block.

Now the block looks like this -

Dependencies added! Let’s check!

And that’s it, all you need to do is write some code. I won’t be going through all this because Sabbir Mollah has already written a comprehensive article on web scrapping here (click on the link to go!). However let’s check if we can import jsoup without any hassle.

Head over to — src/main/java/mavendemo/ and add the following code in there, Right click, then select run App.main()

The output should be like this —

And it works! Congrats!

We’re done. Let’s wrap it up

That’s how you can easily add third party modules to your java project. Sure you could’ve done it using a JAR as Sabbir Mollah did, but I’d say, stick with maven. The benefits — you can see for yourself! Okay, so in future or present if someone wants to work on your project, all they’ll be needing to get up and running with your code is your pom.xml file. As long as your project contains that, you just pass code along and people can use it without any problem.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store