What is Maven
Let’s first understand what Maven is actually and what the purpose of it is. That would help you to know why to use it and when to use it. Maven is basically a Java project management framework and a build tool. It is based on the concept of XML file named Project Object Model (POM). It defines a standard life cycle for building, testing and deploying project artifacts. So it is a set of standards, a repository and software used to manage and build the projects.
You can call Maven a declarative project management tool as well that decreases the overall time a developer needs to spend in building and deploying the applications by effectively leveraging the synergies. It simultaneously leads to remarkable operational efficiencies as well which makes it more popular.
What does Maven provide
Maven’s primary goal is to allow a developer to understand the complete state of a project in the shortest span of time and hence providing a standard platform for build and release. Using Maven:
- All the information required for the build is kept under single file name POM.xml
- Developer needs to provide information on ‘What’ to build, not ‘How’ to build
- Process for build and release gets standardized
Maven’s Build Life Cycle and Phases
Software projects generally follow similar build paths: preparation, compilation, testing, packaging, installation, etc. Maven is based around the central concept of a build life cycle – the process for building and distributing a particular artifact.
There are three built-in build life cycles: default, clean and site. The default life cycle handles the project deployment; the clean life cycle handles project cleaning, while the site life cycle handles the creation of your project’s site documentation.
Each of these build life cycle is defined by a different list of build phases, wherein a build phase represents a stage in the life cycle.
Maven’s life cycle consists of a series of ‘phases’ where each phase can perform one or more actions, or goals, related to that phase. Ex. Compile phase invokes a certain set of goals to compile set of classes.
Standard build life cycle consists of many phases and these can be thought of as extension points. When you need to add some functionality to the build life cycle you do so with the plugin.
Maven Plugins provide reusable build logic that can be slotted into the standard build life cycle. Any time you need to customize the way your projects builds you mostly use of an existing plugins or create custom plugin for your requirement (which is not very usual).
These are the most common default life cycle phases executed:
• 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.
• package: take the compiled code and package it in its distributable format, such as a WAR.
• integration-test: process and deploy the package if necessary into an environment where integration tests can be run
• verify: run any checks to verify the package is valid and meets quality criteria
• install: install the package into the local repository, for use as a dependency in other projects locally
• deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
Now this is very interesting and important to know that these phases get executed in the sequence they have been mentioned above. For example: If you use the command
This would execute all the build phases prior to that.
There are two other Maven life cycles to note beyond the default list above. They are
• clean: cleans up artifacts created by prior builds
• site: generates site documentation for this project
All the phases are mapped to underlying goals. The specific goals executed per phase are further dependent upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war is the project type is a WAR.
We will see the list and usage of all maven commands in the next post.
Installation of Maven