MAVEN Build Tool
What is Maven
Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software:
- It describes how software is built
- It describes its dependencies
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> {{sample group_id_here}} </groupId>
<artifactId> {{sample artifact_id_here}} </artifactId>
<version> {{sample version_here}} </version>
</dependency>
</dependencies>
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.
Prerequisites
Maven is a Java tool, so you must have Java installed in order to proceed.
First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:
mvn --version
A maven project can be created using an IDE as well as a terminal. I’ll prefer the 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 the 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
Why a Build Tool
Initially, while 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 need 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.
Maven is superior to Ant when it comes to dependency management
- More powerful builds
- Better debugging
- Better collaboration
- Reduced duplication
- More consistent project structure
Comments
Post a Comment