Build Project using Maven
Hey Readers,
Welcome to Techie's Publications. In this blog, we are to see what is Maven, why maven is being used, a simple maven project, and maven.
Table of Content :
Introduction:
As a DevOps engineer or a developer, one needs to know how to build/package a project and deliver it with proper structure. In this blog, we are going to see about a build tool called Maven, why it's been used, the life cycle of maven, and its features.
What is Maven and why Maven?:
Maven is a popular open-source build tool developed by the Apache Group to build, publish, and deploy several projects at once for better project management. Maven facilitates the proper structure of the project.
Maven is written in Java and is used to build projects written in C#, Scala, Ruby, etc. Based on the Project Object Model (POM), this tool has made the lives of developers easier while developing reports, checks build, and testing automation setups.
Maven focuses on simplification and standardization of the building process, taking care of the following,
- Builds
- Documentation
- Dependencies
- Reports
Why Maven....consider a developer working on an application. The application may contain a number of modules to execute or to run. It's really hard for the developer or operations person to compile each and every module separately, keep track of dependencies for each module and build/package it together.
Maven takes care of downloading the required dependencies for the project and packaging it with proper project structure.
During the build, maven enforces the standard directory structure and manages and downloads the dependencies. Maven builds the project and stores the artifact in the target directory. Based on the plugin and project, packaging may be JAR, WAR, or EAR.
Maven Lifecycles:
There are three lifecycles in Maven,- Default/Build Lifecycle
- Clean Lifecycle
- Site Lifecycle
1. Default/Build Lifecycle:
Validate - mvn validate to validate that the project is correct and all the necessary information is available.
Compile - mvn compile to compile the source code of the project.
Test - mvn test to test the compiled source code using a suitable unit test framework. These tests should not require the code to be packaged or deployed.
Package - mvn package to take the compiled code and package it in its distributable format, such as a JAR.
Integration Test - the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.
Verify - mvn verify run any checks on the results of the integration tests to ensure quality criteria are met.
Install - mvn install to install the package into the local repository, for use as a dependency in other projects locally.
Deploy - mvn install done in the built environment copies the final package to the remote repository for sharing with other developers and projects.
2. Clean Lifestyle:
Running mvn clean invokes the clean lifecycle which consists of three lifecycle phases;
- pre-clean
- clean
- post-clean
3. Site Lifecycle:
Running the mvn site-deploy command invokes the site lifecycle which consists of four lifecycle phases;
- Pre-site
- Site
- Post-site
- Site-deploy
During the pre-site phase, maven executes processes needed prior to the actual project site generation. In the site phase project site documentation is generated. During the post-site phase, maven executes processes needed to finalize the site generation and prepare for site deployment. Finally, in the site-deploy phase, maven deploys the generated site documentation to the specified web server.
Sequential execution of site commands:
- mvn pre-site – it will execute the pre-site command.
- mvn site –it will invoke pre-site and site both.
- mvn post-site –it will invoke pre-site, site, and post-site altogether.
- mvn site-deploy –this command executes pre-site, site, post-site and deploy-site.
- Maven site: site goal is usually referred to as the ‘site’ phase.
- Mavens site: deploy goal is referred to as the 'site: deploy’ goal.
- Typically mvn clean site – is used to clean and generate new documents.
- mvn clean site-deploy –is usually used to clean the project site and deploy it.
What is pom.xml?:
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples of this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/test/java; and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.
The minimum requirements of the pom file are;
- project root
- modelVersion - should be set to 4.0.0
- groupId - the id of the project's group.
- artifactId - the id of the artifact (project)
- version - the version of the artifact under the specified group
Here is an example...
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
Simple Maven project:
Let's see a simple maven project of a java based application to get a better understanding.
For this, I have made a simple Java code that checks the username and password. I developed this maven project from Eclipse IDE and pushed it to the git-hub repository. In Eclipse IDE it is easy to maintain the project directory structure.
Prerequisites:
- An EC2 instance(here I have used Ubuntu) with maven, tree, jdk8 and git been installed. Use the following command to install the above-mentioned applications and tools.
- sudo apt-get install openjdk-8-jdk
- sudo apt install maven
- sudo apt install tree
- sudo apt install git
- A sample Java code to build.
Let's get into the execution,
- Clone the project repository from git-hub using the below command.
git clone <Link of the repository>
src/main/java/com/sample/App.java - this is the actual source code of the project where the functionality is coded.
src/main/resources/config.properties - in this file, the properties for the App.java are defined.
src/test/java/com/sample/AppTest.java - this AppTest.java is written with code for test cases using testng.
Let's have a look at the pom.xml file,
In this project, I have used testng to implement the test cases. Here I have added the dependency details of testng in order for maven to pick the dependencies list from pom.xml and to include that to the project.
- Now run the maven package mvn package command to start the build inside the directory where the pom.xml file is present.
Once the execution starts maven will start implementing from compile phase to the package phase one by one.
- After successful build maven artifacts the project and store that in the target directory.
- Maven also provides test reports and build reports.
Inside /target/surefire-reports/ we can able to test results reports etc...
Explore the target directory and get to know more about the maven build.
Happy Learning....😀





Comments
Post a Comment