Learn about Apache Maven- A Build Automation Tool
What’s up, everyone! Today we will be going to talk about Maven.
We will start with the quick intro and then list down the advantages of using it. After all this, we will see its installation process and then a few technical terms which are essential for a beginner. So, let’s get started!
What is Maven?
Apache Maven is a project management software, and we can say it is a comprehension tool. It is based on the concept of the project object model (POM) [will be discussed later in the articles], Maven can manage a project’s build, reporting, and documentation from a central piece of information.
Apache Maven is a build tool, and it does the task just like Ant, which is again an extraordinary build Tool. This is a software project management tool that gives a new concept of the project object model (POM). Maven allows the developer to automate the handling of the creation of the original folder format, performing the assortment and testing and the packaging and deployment of the final output. It cuts down the considerable number of steps in the base process, and it makes it jus one-step process to do a build.
Why is Maven Used?
To sum up, Maven simplifies and standardizes the project build process. It handles team collaboration, compilation, distribution, documentation, and separate tasks seamlessly. Maven increases reusability, and it also takes care of most of the build-related tasks. It works in helps many steps such as adding jars to the project library, building reports, and executing Junits test cases, creating Jar, War, Ear files for the project, and even many more things.
A highly significant aspect of Maven is the purpose of repositories to manage jar files.
The Maven can further be used in building & managing the projects written using languages like C#, ruby, and other programming languages.
Let’s take a look at the following Maven advantages.
Convention over configuration
- Its configuration is very minimal.
- It has managing dependencies.
Multiple/Repeated builds can be achieved
- Automation makes it easy.
Plugin management, testing, and development
- It has the ability to run JUnit and other integration tests.
- It makes the development process clear.
Provisions to check the status of every build
- It avoids inconsistent setups.
- Standard and uniformed infrastructure among projects.
Architecture of Maven
Understanding Maven’s architecture, based on Project Object Model (POM), will highlight the conceptual understanding that offers a complete understanding of the build framework.
The Maven architecture is as shown below:
Its architecture displays the entire build lifecycle framework, including lifecycles, stages, goals, plugins, and other tasks right from the start.
The following steps are listed in Maven’s architecture:
- First Step: Requires setting up Maven, which uses a pom.xml file. All of Maven’s configurations are present in the POM file.
- Second Step: Download the pom.xml dependencies from the central repository into the local repository.
- Third Step: After the user starts working in Maven, the tool offers various default settings, so there is no need to add every configuration in the pom.xml.
Maven Build Lifecycle
The Maven build life cycle is a sequence of steps that need to be followed in order to build a project.
Below is the outline of the phases of the build life cycle:
Some of the important phases are as follows:
- Validate: It confirms that all the data necessary for the build is available.
- Compile: The source code is compiled.
- Test-compile: It builds the source code for the tests.
- Test: Unit tests are run by it.
- Package: It transforms compiled source code into an accessible format (jar, war, etc.).
- Install: The software is installed into a local repository.
- Deploy Copies of the package to the remote repository.
There are three built-in lifecycles in Maven:-
- Default: From the validation stage until the deployment stage, it has 21 phases.
- Clean: The project is properly cleaned up, and any files created by the previous build are deleted. In this, there are three phases.
- Site: The project’s website documentation is created at this step and has four phases to it.
Repository in Maven
There are three types of maven repositories:-
- Local Repository: It uses a local computer to store repositories.
- Central Repository: Maven community is a central repository, and Maven downloads the dependencies from this location whenever required.
- Remote Repository: Maven downloads dependencies located on a web server.
Advantages of Maven
Maven is one of the most popular open-source build automation and management tools developed by the Apache Group. It offers numerous advantages to software developers and corporations, such as:
- Administer all project management processes, including building, documenting, releasing, and distribution.
- Reduces the complexity of project development through a standardized project structure.
- Enhances the projects and the development process performance.
- Its various plugin integration helps optimize the code’s quality, tasks’ automation, productivity improvement, etc.
- Jar files and other dependencies’ downloading is carried out automatically.
- Facilitates the developer’s ability to build a project in many contexts without worrying about dependencies, workflows, etc.
- Its dependency management system streamlines the incorporation of external libraries, reducing the likelihood of version conflicts and ensuring consistent and reliable builds.
- It streamlines the deployment process that reduces human errors resulting in improved efficiency.
By leveraging the benefits of Maven, development teams can improve the productivity and quality of the code and deliver high-quality software while reducing the overall development community.
Setting up Maven Environment
The installation of Maven includes the following steps:
- Check whether the system has Java installed or not. if not then install java
- Check whether java Environment variable is set or not. If not, then set java environmental variable.
- Download Maven
- Unzip the maven download at one place in the system.
- Now, add the bin directory of the created directory apache-maven-3.6.2 to the PATH environment variable and system variable.
- Open cmd and run
mvn -vcommand to confirm the installation.
To get the detailed steps of the installation, follow the below YouTube guide to setup Maven Environment as we don’t want to make this article simple reading stuff and boring too.
As this is a beginner guide so it must include technical terms related to MAVEN. So here are few, which are very important:
Maven Local Repository
Maven Local Repository is the set where Maven stores all the project jars files or libraries or dependencies. By default, the folder name set to ‘.m2 ‘, and by default, the location is ‘Libraries\Documents\.m2 ‘.
Maven Central Repository
Maven central repository is the default location for Maven to download all the project dependency libraries for the use. For any library involved in the project, Maven first looks into the .m2 folder of Local Repository, and if it does not find the needed library, then it seeks in Central Repository and loads the library into the local repository.
POM is a Project Object Model XML file that has information about the project and configuration details needed by Maven to develop the project. It contains default values for most projects. Some of the structures that can be defined in the POM are the project dependencies, plugins that can be executed, and of course, the build profiles.
Elements used in Creating pom.xml file
- project- Project is the root element of the pom.xml file.
- modelVersion- It means the version of the POM model you are working with.
- groupId- It implies the id for the project group. It is unique, and Most often, you will apply a group ID that is related to the root Java package name.
- artifactId- This is used to provide the name of the project you are building.
- Version- This element consists of the version number of the project. If your project has been released in various versions, then it is convenient to present the version of your project.
Dependencies are the libraries, which are needed by the project. Like Log4j jars, Apache Poi jars, Selenium Jars are few libraries that are required for the project. Dependencies, in the Maven pom.xml, are mentioned like this:
<dependencies> <dependency> <groupId>com.companyname.groupname</groupId> <artifactId>App-Core-lib</artifactId> <version>1.0</version> </dependency> </dependencies>
The Surefire Plugin is needed during the test phase of the build lifecycle to implement the unit tests of an application. It makes reports in 2 different file formats like plain text file, XML files, and in HTML files also. Even if you are using Junits framework or TestNG for reporting, this plugin is a must for use, as it helps Maven to find tests.
Practical Application Of Maven
When working on a specific java project, and that project has a lot of dependencies, builds, requirement, then working with all those things manually is hugely complex and laborious. Thus using some tools that can work out these works is truly helpful.
And Maven is such a build management tool which can perform all the things like adding dependencies, using the classpath to project, making war and jar file automatically and many new things.
This tool is beneficial when it comes to building projects. I wish you success in your JAVA project. If you are looking for a video tutorial, then you may check out the Maven crash course.