Introduction to Maven
This article gives you a beginners Introduction to Apache Maven.
You can watch the Introduction in this video.
What is Apache Maven
Apache Maven or in short just Maven, is popular for its build capabilities. That is why, most people refer to Maven, as a Build Automation tool, or in short - just a Build Tool.
Yet, Maven is not just a build tool; but it is more than that. The project team responsible for the development and maintenance of Maven, describes Maven as a software project management and comprehension tool.
Based on the concept of a Project Object Model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
Do remember this term, Project Object Model or in short POM, because you will hear and use it all the time when talking about Maven. We will discuss POM in detail in our later videos.
So, as you can see, Maven aims to be, more than, just a Build Automation Tool.
More than a Build Tool
But, you might ask, what do we mean by “More than a Build Tool”?
Well, apart from providing the build capabilities, which comprise of:
- Pre-processing - where-in you initialize your build which could mean setting up environment variables, processing some configuration files for the build, copying files if you need, and then
- Compile - where you take the actual source code and compile to ensure there are no errors in your code, and then you
- Test - the code by running the automation tests (be it unit test or integration test) and once, your code is tested, you
- Package - your code where-in, you will be creating a jar file or zip file or a docker image or whatever your packaging need is. And finally, you
- Distribute it - So, you would be publishing to your company’s repository of artifacts or deploy the packaged artifact on a server.
But apart from all of this (the build tooling), Maven can do things like generating reports. These reports could be a Unit Test report, or you have a tool that does some Performance testing of your application. Maven can take the performance test data and generate a report and publish them.
You can also generate a documentation website from Maven. People who are familiar with Javadoc, Maven provides support to generate Javadoc, which is a very common documentation format for Java source code.
What else - you can also use it to communicate within your team. Maven can act as a team member and can send you notification on your Slack or Hangouts or any other messenger.
And it can do a lot - a lot more things.
Dependencies
All applications built today have dependencies.
The below XKCD diagram depicts the nature of modern day applications and their interdependence.
As much as using dependencies allows us to quickly build the applications, dependency management becomes important. One of the biggest benefits or features that Maven brings with it, is Dependency Management. If you have ever worked with Apache ANT, you will know how much things got better, when you started using Maven.
A typical Java application these days has multiple dependencies. For example, here I have an application which has 2 dependencies. A Web Server that is going to handle requests(Tomcat or Jetty), and I have a database driver that allows me to connect to the database.
Without Maven, I will need to download the jars from the internet, copy them to the library folder, and then add these in the application classpath. Now, when I run the application, after compilation and building, the application works.
Now, that’s a lot of manual work.
Instead of manually managing these dependencies, I can configure these dependencies in Maven and let Maven do the hard work. Maven will download the jar files from the internet, use them for compiling, testing and then package it along with the final packaged artifact.
You might think, these are just two files and I can manage that. But these days, a typical java application has easily more than 10 dependencies/jar files. If you start managing these dependencies manually, trust me, it is going to be a nightmare.
Managing Dependencies Manually (an example)
In fact, let me give you an example how managing dependency manually can easily go out of control. So taking the previous example, we have WebServer version 1.0 and DB Driver version 1.0 in the application. The application is running fine and there are no issues.
Consider, there is a critical security bug reported in Web Server 1.0 (and this happens all the time). Developers managing Web Server, fix the security bug and release a new version. This new version is Web Server 2.0. Since we do not want the security bug to affect our application, we will need to update the Web Server dependency from 1.0 to 2.0. So, we will download the latest Web Server 2.0 jar, and add it to the application.
But, when we try to run the application, it fails. Why?
Because, in a hurry to fix the security bug, WebServer 2.0 became incompatible with DB Driver 1.0. Developers of DB Driver, on realizing this, fixes the compatibility issue and releases their version DB Driver version 2.0.
So, what will we have to do now?
Again the same process, update DB Driver to version 2.0. We download the jar file, add it to our application and then now, the application starts to run again.
This is a very simple example with just 2 dependencies. Manual dependency management gets really, really complicated once you start to deal with dependency over 10.
Worse, you update a single dependency, and you see multiple other dependencies becoming incompatible with each other. There is a cascading update of the dependencies required, until all incompatibilities are resolved, to eventually have the application work fine.
This is where Dependency Management comes into picture and the power of Maven.
How will Maven help here?
We will configure both Web Server and DB Driver dependency in the Maven configuration file. Whenever we update any of the dependencies, all related compatible dependencies are also fetched by Maven.
In our example, when we update Web Server 2.0 in the Maven configuration file, Maven automatically fetches the compatible DB Driver 2.0.
And all this happens magically, under the hood. All you do is update the Maven configuration file, and Maven does the magic for you.
How Maven does its Magic?
But, you might wonder - how does Maven do all this Magic? The answer to that question is - Plugins.
Maven makes use of Plugins. Whatever activity you can think of, there is a plugin for that activity.
You want to compile your source code, there is a plugin to compile.
You want to run tests that you wrote in the application, there is a plugin for testing.
You want to create a Docker image so that you can deploy your application, there is a plugin to build a docker image.
In fact, there is even a plugin to create the Maven project structure called archetype. It allows you to create your starting project structure - your folders for source code, folder for test classes, and then create the Maven configuration file, so that you can adjust settings as per your need.
Whatever Maven does, it does it with the help of plugins. These plugins contain the code to perform the activity. Maven just triggers the plugin code as per user’s needs, via the Maven configuration file.
Is Maven multi-platform?
Maven works on all three platforms, Windows, Mac and Linux. All, you need is Java installed on your system to run Maven on your system.
And that is a very brief introduction to Apache Maven.