Adding an external library into a project, why use a build tool like Maven?
In this article, I will describe how to add an external library into a project. First I will start with the most common way, adding an external library manually, but unfortunately this method is not the best one. Then I will move to build automation tool Maven, this method is the preferred one for automatically adding external libraries into a project. All presented in my favorite IDE which is IntelliJ IDEA.
When we learn Java basics, there is no need to add any external library. But when moving forward, we want to add external libraries to help us to solve some common programming problems or just to speed up the development process, because someone else did it a long time ago. Maven is not the only tool that automates the process of adding external libraries into a project. I was working with tools like Ant and Gradle.
First I will explain what is an external library or just library. It's a collection of classes packed into a single file called JAR (Java Archive). Even for the smallest Java project we don't share its single class, instead we would like to share the whole project structure with packages, classes etc.. In a JAR file, Java classes are divided into packages which separates classes physically on computers hard drive and separates logically, e.g.: classes that operate on texts or numbers. JAR file may contains non source code files, e.g.: images, text files, etc.
Where to look for a Java external library? If you know which library you want to use, you can search it on https://mvnrepository.com. But if you don't know the name of library, simply just search it on the internet using keywords e.g.: Java string utils, you will probably get the https://commons.apache.org/proper/commons-lang/ - "Apache Commons is an Apache project focused on all aspects of reusable Java components.". The Apache Commons Software Foundation is a great place to find libraries.
When talking about libraries, in the context of build automation tools e.g.: Maven, we use the term dependency which is simply the library in the JAR file. In this article I will not dive into Maven tool, more you can find on https://maven.apache.org or https://en.wikipedia.org/wiki/Apache_Maven.
The below table shows comparison adding external libraries, manual work vs automated with Maven.
Lp. |
Manual work |
Automated with Maven |
1. |
Create Java project |
Create Maven project |
1. a) |
Create folder e.g.: lib |
N/A |
2. |
Find library in the Maven Repository |
Find library in the Maven Repository |
3. |
Download JAR file |
Add Maven <dependency> |
3. a) |
Place JAR file in the lib folder |
N/A |
3. b) |
Add library to the project |
N/A |
4. |
Start using the external library |
Build Maven project to start using the external library |
We can see that adding an external library consists of four steps. The first steps Create ... we do only once when we create a project. Steps from 2. to 4. we repeat each time we add a new external library, or we just want to change the version of the library (which is a common use case). The first approach, adding an external library manually, requires two additional, time-consuming steps, and they are tedious and error-prone. Not mentioning maintaining version changes over time when we have, let's say, about 10 external libraries.
Comparison adding an external library, manual work vs automated with Maven
Even when looking at the below screenshots, with a practical step-by-step tutorial showing both approaches, we can see that the first one has twice as many screenshots as the second one. Adding an external library manually consists of around 16 screenshots that were automated with Maven has around only 7 screenshots.
The first approach - adding an external library manually
Now, I can describe the first approach, which is adding an external library manually. Why do we even bother with manual work when now everything is automated? It's good to know how it works, and what exactly Maven automates, also we will see the work that needs to be done using this approach.
The below screenshots shows in great detail step-by-step how to add an external library, each screenshot has a description. There is no need to explain the whole process separately.
Create Java project - external library, manual work.
Created, empty Java project overview - external library, manual work.
Add sample code with external class usage, error missing class - external library, manual work.
w folder - external library, manual work.
Create folder e.g.: lib - external library, manual work.
Find library in the Maven Repository - external library, manual work.
Search 'commons-lang' library at www.mvnrepository.com - external library, manual work.
Download JAR file - external library, manual work.
Place the JAR file in the lib folder - external library, manual work.
Java project with added external library - external library, manual work.
Add library, menu option "Project Structure..." - external library, manual work.
Add library, menu option "+ New Project Library -> Java" - external library, manual work.
Add library, search for JAR file - external library, manual work.
Add library, choose modules - external library, manual work.
Added external library overview - external library, manual work.
External library class 'StringUtils' usage overview - external library, manual work.
As we can see in the first approach, adding an external library manually isn't easy and time-efficient. There is room for improvement, and this was noticed by developers, and they wrote some code that automates this process. When I started working with Java projects, manual work was not a big problem. I was skeptical about tools such as Maven, because it was something new that I had to learn, and I was so busy with manual work that I didn't have time for improvement. But finally, I switched to build automation tools, and it was such a productivity booster!
The second approach - adding an external library automatic with Maven tool
It's time to show the second approach, which is adding an external library automatic with Maven. This process is fully automated, once we create a Maven project, the only thing we need to do is to add Maven dependency, and "Load Maven Changes".
The below screenshots shows in great detail step-by-step how to add an external library, each screenshot has a description. There is no need to explain the whole process separately.
Create Maven project - external library, automated with Maven.
Creating Maven project, properties - external library, automated with Maven.
Search 'commons-lang' library at www.mvnrepository.com - external library, automated with Maven.
Copy 'dependency' for 'commons-lang' library for Maven use - external library, automated with Maven.
External class usage, without added Maven 'dependency', error missing class - external library, automated with Maven.
Add Maven 'dependency' and 'Load Maven Changes' to use library classes - external library, automated with Maven.
External library class 'StringUtils' usage with Maven 'dependency', overview - external library, automated with Maven.
Developers should always follow the DRY (don't repeat yourself), a principle of software development, which in this case fits perfectly. In conclusion, I can say that it's always good to "work smart, not hard", but at the very beginning in Java programming, manual work is still perfectly fine. As Junior Java Developers we may use manual work, but later on, we definitely have to use automation build tools like Maven.