Every application that we use today requires a set of configurations that need to be completed. These configurations were once completed manually, but after the advent of Maven and other automation tools, this tedious task became automated. Maven is a DevOps tool that automates the entire process of building and developing these applications. This introduction to maven tutorial will help you learn everything that you need to excel in Maven.
Let’s begin the introduction to maven tutorial with understanding why we need Maven in the first place.
The Need of Maven
Maven is a software project management build tool based on Project Object Model (POM). The tool is typically used for Java-based projects. Every Java project requires certain dependencies, which are automatically downloaded when running a Maven build. This simplifies everyday tasks for Java developers and helps them with their projects.
Maven helps retrieve the correct JAR files for each project, as there may be different versions of separate packages. If you want to download dependencies, you no longer need to visit each software's official website. It can be quickly done now by visiting "mvnrepository.com."
Introduction to Maven
Next up in the introduction to maven tutorial we will learn what exactly maven is. Maven is a popular open-source build tool that the Apache Group developed for building, publishing, and deploying several projects. Maven is written in Java and is used to create projects written in C#, Scala, Ruby, and so on. The tool is used to build and manage any Java-based project. It simplifies the day-to-day work of Java developers and helps them with various tasks.
Maven is based on Project Object Model (POM) and focuses on simplification and standardization of the building process.
During the process, Maven takes care of the following elements:
- Mailing list
In the next section of the introduction to maven tutorial, we will learn what a build tool is.
What Is a Build Tool?
A build tool is essential for the process of building, as these are the tools that automate the process of creating applications from the source code. The build tool compiles and packages the code into an executable form.
A build tool does the following tasks:
- Generates source code
- Generates documentation from the source code
- Compiles source code
- Packages the compiled codes into JAR files
- Installs the packaged code in the local repository, server, or central repository
What Is a Maven Repository?
In this section of the introduction to maven tutorial, we will learn about maven repositories, and their types.. Maven repositories refer to the directories of packaged JAR files that contain metadata. The metadata refers to the POM files relevant to each project. This metadata is what enables Maven to download dependencies.
There are three types of repositories:
1. Local Repository:
Local repository refers to the developer’s machine, which is where all the project material is saved. This repository contains all the dependency jars.
2. Remote Repository:
The remote repository refers to the repository present on a web server, which is used when Maven needs to download dependencies. This repository works the same as the central repository. Whenever anything is required from the remote repository, it is first downloaded to the local repository, and then used.
3. Central Repository:
Central repository refers to the Maven community that comes into action when there is a need for dependencies, and those dependencies cannot be found in the local repository. Maven downloads the dependencies from here whenever needed.
The next important aspect of the introduction to maven tutorial is to understand why Maven uses convention and not configuration.
Convention Over Configuration
Convention is used when the developers are not required to create the build processes. The users do not have to specify the configurations in detail, and once the project is created, it will automatically create a structure. With configuration, on the other hand, developers are supposed to create the build processes manually.
Maven uses convention over configuration so developers just have to create a Maven project. The rest of the structure is automatically generated. There are many conventions present in Maven for setting up a plan, building the artifacts, releasing the code, and running unit tests. The developer simply places the files, and there is no need to define any configuration in pom.xml.
The feature helps developers to freely build any Maven project without worrying about the need to know about the workings of each plugin. The Maven plugins look over the project management and build-related works.
Also Read: How to Install Maven on Windows and Ubuntu?
Introduction to Maven
In this section of the introduction to maven tutorial we will learn about the five primary and most significant concepts of Maven:
- Project Object Model
- Dependencies and repositories
- Build life cycles, phases and goals
- Build profiles
- Build plugins
1. Project Object Model (POM)
- Project Object Model (POM) refers to the XML files with all the information regarding project and configuration details
- It contains the project description, as well as details regarding the versioning and configuration management of the project
- The XML file is in the project home directory. Maven searches for the POM in the current directory when any given task needs to be executed
2. Dependencies And Repositories
- Dependencies refer to the Java libraries required for the project. Repositories refer to the directories of packaged JAR files.
- If the dependencies are not present in your local repository, then Maven downloads them from a central repository and stores them in the local repository.
3. Build Life Cycles, Phases and Goals
- This consists of a sequence of build phases, and each build phase consists of a series of goals
- Each goal is responsible for a particular task
- When a process is executed, all purposes related to that phase and its plugins are also compiled
4. Build Profiles
- This refers to the set of configuration values required to build a project using different configurations
- Different build profiles are added to the POM files when enabling different builds
- A build profile helps in customizing the build for different environments
5. Build Plugins
- A Maven plugin refers to the group of goals that may or may not be in the same phase
- The plugins are used to perform a specific goal
- Maven has its standard plugins that can be used. If desired, users can also implement their own in Java
Features of Maven
Next up in the introduction to maven tutorial, we will dig deeper into the features of maven. Apache Maven manages all the processes, like building, documentation, releasing, and distribution in project management.
- The tool simplifies the way of project building by increasing the performance of the project and the building process
- The task of downloading dependencies and JAR files is automated
- Maven provides easy access to all necessary information
- Maven makes it simple for the developer to build a project in different environments without worrying about the dependencies, processes, etc.
- In Maven, it is simple to add new dependencies—you only have to write the dependency code in the POM file
The projects created in Maven contain POM files that describe the aspect of the project essentials. Maven architecture shows the process of creating and generating a report according to the requirements and executing lifecycles, phases, goals, plugins, and so on—from the first step.
How Does the Maven Architecture Work?
- The first step refers to configuring Maven, which is stored in a pom.xml.file
- The POM file includes all of the configurations that Maven needs. The second step is to download the dependencies defined in pom.xml into the local repository from the central repository
- After the user starts working in Maven, the tool provides various default settings, so there is no need to add every configuration in the pom.xml
Maven Build Life Cycle
The Maven build life cycle is a sequence of steps that need to be followed in order to build a project, which eventually helps to execute goals.
The following are the phases of the build life cycle:
There are three types of standard life cycles:
The default or build life cycle is the primary cycle that is responsible for building the application. There are 21 different phases in this primary life cycle, starting from the process of validation to the step of deployment.
The clean life cycle handles project cleaning. The clean phase consists of the following three steps:
The mvn post-clean command is used to invoke the clean lifestyle phases in Maven.
The Maven site plugin handles the project site's documentation, which is used to create reports, deploy sites, etc.
The phase has four different stages:
Ultimately, the site that is created contains the project report.
Introduction to Maven Tutorial: Creating Your First Project Using Maven
The first step in creating a project is to go inside the terminal, and enter the following command:
The version of Maven is displayed on the screen.
Now, let’s run the following commands:
mvn archetype : generate
Maven downloads some of the plugins in order to handle specific executions.
Maven will then have the user choose a number or version.
- You can select any number:
- We have selected the number five
- Maven will prompt the user for a group:
- Maven will prompt the user for an artifactI:
- The version remains the same
- The package also remains the same
- Enter Y for yes
- Press enter
The sample project will be created with the artifactId you have provided.
GO inside that directory and verify what files are created:
When the pom.xml file is opened, you can see the attributes on the screen. It shows the groupId, artifactId, the JAR file, and the version. You can also add your own custom dependencies.
If you run the clean command, it will be classified as a Maven project.
mvn clean install
A pom.xml file is already present in the local repository. The execution of the steps will take place accordingly.
Eventually, you can see a couple of JAR files being created in the directory.
You can create a new project with this method, and based on our understanding, amendments can be made in the dependencies. Hence, a required final result can be obtained.
Interested to begin a career in DevOps? Enroll now for the DevOps Certification Course. Click to check out the course curriculum.
In our introduction to maven tutorial, we have explored why Maven is used, and all the introduction to maven, the fundamental concepts of Maven, and maven architecture, and its lifecycle. Some of the many areas we covered include build tools, convention over configuration, and POM. Finally, we reviewed the process of creating our first project in Maven, with the help of a hands-on demo.
Do you have any questions for us? Leave them in the comments section below, and we will have our experts answer them for you.
Want to Learn More About Devops Automation?
DevOps is the way of the future, and companies are actively seeking highly-skilled professionals in this exciting field. To boost your career, check out our DevOps Engineer Master’s program through which you don’t only learn introduction to maven but also learn how to leverage powerful tools like Maven, Git, Docker, Jenkins, and more. What are you waiting for?