The What, Why, and How of learning maven from scratch.
The official document states that:
Apache Maven is 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.
In this article, I’ll try to explain the above statement with examples so that it becomes quite clear what maven is, and how to use it. First, we’ll see how we can manage a java project without any build tool, and then learn about maven and see how it makes the life of a java developer easy!
Building the old way
Let’s consider we have to build a web application. We might have several dependencies such as web mvc, junits, etc. We need to manage all the dependencies, build WAR files, deploy, etc. All these steps take a lot of time.
Let us consider an example:
We have two files: App.java and AppTest.java along with 2 jar files for writing JUnits.
We first need to compile App.java -> App.class
Then we need to compile AppTest.java → AppTest.class
Then we need to run the test cases and check if no tests are failing.
Then combine all classes to jar file:
As you can see, the steps are quite tedious and difficult to follow along, and if you have a huge project with a lot of dependencies to manage it becomes almost impossible. Developers used ANT before to solve and automate the above process, but for this, we had to write the long scripts. Maven came in as a solution for this.
Using Maven as a build tool
Maven is an open-source project management build tool developed by Apache. It began as an attempt to simplify the build processes in the Jakarta Turbine project.
Why use maven?
- It is a complete project management tool.
- It makes the build process very easy (No need to write long scripts).
- It provides a uniform build system. It has a standard directory structure which is followed.
- It follows Convention over Configuration.
- It has a remote maven repo which has all the dependencies in one place.
- Can be used with other programming languages too, just not java.
Let us now see how maven helps us with the above project which we created. We will create a similar project, but now we’ll use Maven as the build tool.
For that first check if you have maven installed. If not please install it.
We will now create the same project using maven:
mvn archetype:generate -DgroupId=hussain -DartifactId=sample-mvn -DarchetypeArtifactId=maven-archetype-quickstart
As Maven follows convention over configuration. Therefore it defines some defaults (can be overridden):
1. Src files should be in src/main/java
2. Test files should be in src/test/java
3. pom.xml should be in the root folder
When you run any maven command, the first thing it looks for is pom.xml. We will learn more about this later.
Maven build lifecycle
Maven build lifecycle is a set of phases that is executed when a maven build is run. There are three build lifecycles: default, clean, and site. The default lifecycle comprises of the following phases:
- validate — validate the project is correct and all necessary information is available
- compile — compile the source code of the project
- test — test the compiled source code using a suitable unit testing framework. These tests should not require the code to be packaged or deployed
- package — take the compiled code and package it in its distributable format, such as a JAR.
- verify — run any checks on results of integration tests to ensure quality criteria are met
- install — install the package into the local repository, for use as a dependency in other projects locally
- deploy — done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
These lifecycle phases are executed sequentially to complete the default lifecycle. If any specified phase is executed, all the phases before it are executed first. Let’s run a few commands to make it more clear:
Right now we do not have any compiled files. To compile src code, try:
You will see that a new target folder is created with src files compiled in it.
To compile test classes too, try:
This command compiles src files as well as test files:
To delete the compiled classes, try:
This will delete the target folder altogether.
It compiles src and test files, runs the test, and verifies that quality criteria are met.
In the install phase, maven first validates, compiles (src and test classes), packages, verifies, and then install the package into the local repository, for use as a dependency in other projects locally.
Project object model or pom is an XML file that contains information and configuration details used by maven to build the project. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.
Elements of POM:
- project: root element of the XML file
- modelVersion: should be set to 4.0.0
- groupId: It specifies the id of the project’s group
- artifactId: id of the project
- version: Version of the artifact within the group
We have several other elements which include: packaging, dependencies, dependencyManagement, repositories, scm, modules, build, etc.
All the dependencies are downloaded from a remote repository into a local maven repository. This is why, while running any new project, it takes time for the project to load, as it is downloading the dependencies from remote to local.
This article focused on making you understand the basics of maven as a build tool. Once you understand this, you can deep dive and learn more about it from different forums/articles online.
References and some excellent articles to learn more about the same: