This blog will provide you with the basics of maven, its importance and its life cycle. For installation, you can refer here and then continue with this blog.
Why a Build tool?
Initially, while learning and developing small projects, we compile limited classes and include specific JAR libraries on our own which are required to build our project. This seems very difficult in large projects where we require many JAR’s. Adding them is an unnecessary overhead for the developer. To reduce these tasks, build tools are required. Maven is again a tool for performing these tasks automatically. Previously, “ant” is provided by Apache for the same. Here’s a list of Why to go for maven over ‘ant’.
What is Maven?
Maven is a built tool by Apache, also justified as a project management tool used by developers. It is a global repository which provides you with dependencies. Here dependency means the external libraries required to build a project. It provides plug-n-play stuff for maintaining the projects. Maven allows the developer to focus on developing the functionality and support them by providing the necessary libraries which will be mentioned in “pom.xml”.
Pom.xml
Project Object Model (POM) is an XML file that contains information about the project and configuration details used by maven to build the project. When executing a task, maven looks for the POM in the current directory. It reads the POM file, gets the needed configuration information, then executes the goal. POM includes a dependency using the following tag structure:
<dependencies>
<dependency>
<groupId> {{group_id_here}} </groupId>
<artifactId> {{artifact_id_here}} </artifactId>
<version> {{version_here}} </version>
</dependency>
</dependencies>
The Maven Project
Every maven project has a list of elements which are known as maven coordinates.
- GroupId: It uniquely identifies your project across all the projects. It specifies the organization or group you are a part of. It must follow Java’s package name rules. This means it must start with a reversed domain name you control. Eg: com.knoldus
- ArtifactId: It is the name of the project without version. You can choose any name with lower-case letters and no strange symbols.
- Version: This is used to provide the version number, which is “1.0-SNAPSHOT” by default. Here, Snapshot means that the version is under development.
Creating a Maven Project
A maven project can be created using an IDE as well as a terminal. I’ll prefer terminal for this session.
- To create a maven project, first, go to your working folder and open the terminal at that location.
- Enter the command
mvn archetype:generate
. This command will generate a project from an existing template. These templates are used to provide a perfect directory structure for our project. For executing this command, you must be connected to the internet as this will download the maven archetype plugin. This is a one-time command and won’t download those templates if already existed. - From the generated list of archetype, choose any archetype based upon the type of project you want to develop. You may need to select the version of the archetype as these are the directory structures which are under continuous improvement.
- Now provide the “maven coordinates” for the project which are discussed above. The project jar for the project will follow this structure as
"<artifactId-versionNo.extension>"
. - Then a package name is to be provided which can be same as the groupId.
- At last, you need to press ‘Y’ for confirming the project coordinates.
Life Cycle of Maven Project
Maven cycle mainly performs the following tasks in its execution cycle.
- Compile source files
- Compile test files
- Execute tests
- Create a jar file of the project
The life cycle of maven includes a sequence of steps which are defined in order to execute the tasks and goals of any maven project.
- Validate: Identifies if the pom.xml entries are correct and check whether dependencies are correctly mentioned. This basically verifies the dependencies mentioned in the pom.xml.
- Compile: In this stage, all our source code of our project gets compiled and a target folder is generated which includes the bytecode files of our source code.
- Test: Here, the compiled code is tested using some suitable test framework. These test cases are not considered for packaging and deploying.
- Package: Packaging the successfully compiled and tested code to some distributable format like JAR, WAR, EAR
- Verify: Performs action for a quality check on results and ensures the required criteria is met.
- Install: It installs the application package in the local repository. Any other project can use this as a dependency locally.
- Deploy: The final package will be copied to a remote repository, maybe as a formal release and also made available for sharing with other developers too.
The execution of these build phases is done in the terminal by simply adding the build phase after the mvn
command.
The above commands follow the hierarchy in which they are mentioned. This means that while executing any command, commands which are above in hierarchy will automatically be executed.
Tip:In addition to these build life cycle phases, maven also provides a clean build phase, which cleans the project and makes it ready for fresh compile and deployment. The command used for executing this process is : mvn clean
.
After going through the contents, now you’ll be familiar with the maven build tool and its life cycle. For any queries, feel free to contact me at yatharth.sharma@knoldus.in. Here are my references :
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.
Sir, I love your content. Thanks for sharing