What is maven?
Maven is a project management and comprehension tool that provide developers with a way to manage a project’s build, dependencies, and documentation.
In simple words it automates the process of compiling code, packing it into distributable formats( like JAR files), and managing internal libraries(dependencies) that the project might need.
Maven provide developer way to manage the following.
- Builds
- Documentation
- Reporting
- dependencies
- SCMs
- Releases
- Distribution
- Mailing list
Maven evolution
Maven was originally designed to simplify building process in Jakarta turbine process. There were several projects and each project contained slightly different ANT build file. JARs were Checked into CVS.
Apache group then developed maven which can build multiple projects together, publish projects information, deploy projects, share JAR across several projects and help in collaboration of teams.
Objective
The primary goal of maven is to provide developer with the following –
- A comprehensive model for projects, which is reusable, maintainable, and easy to comprehend.
- Plugins or tools that interact with this declarative model.
Maven project structure and contents are declared in an XML file, pom.xml, referred as Project Object Model (POM), which is the fundamental unit of the entire maven system.
Convention over configuration
Maven uses convention over configuration, which means developers are not required to create build process themselves.
Maven is a tool that helps developers belied java projects. It automatically set up the
Project structure and handles many tasks, so developers don’t have to do everything manually. This means developers con focus on writing code, and maven take care of the rest.
Following table shows the de fault values for project source code files, resource files and
Other configurations amusing ${basedir} denotes the project location.
Item | Default |
Source code | ${basedir}/src/main/java |
Resources | ${basedir}/src/main/resources |
Tests | ${basedir}/src/test |
Complied Byte code | ${basedir}/target |
Distributed JAR | ${basedir}/target/classes |
Maven con build your project automatically, you just need to tell it what to do, and it
Will use plugins to handle the technical details.
Features of maven
- Easy project setup – quick and easy project setup with best practices.
- Consistent Structure – Ensure a consistent project structure across all projects
- Dependency Management – Handles Dependencies and update automatically.
- Vast Library Repository- access to large number of libraries.
- Extensibility -Customise Maven with plugins.
- Quick features Adoption – Easily adopt new features with minimal configuration.
- Flexible Build Process – build various project types (JAR,WAR,etc.).
- Automated documentation – generates project documentation (website,PDF).
- Release management – Manages project releases and distribution.
- Backward compatibility -supports older maven versions.
- Simplified Parent Versioning – Easy Management of parent-child relationships.
- Parallel builds – improve build performance
- Improved error reporting- Provide clear and helpful error message.
Understanding problem without maven
- We need to add jar files and dependencies in each and every project.
- We have to create right project structure, otherwise project will not work.
- We have to build and deploy project manually.
Setting up maven
Prerequisites
- Java development Kit (JDK) – Make sure you have installed java in your System.
To check weather java is installed, go to terminal or command prompt. TypeJava –version.Install if you have not installed java in your system.click here to install java https://www.oracle.com/java/technologies/downloads/?er=221886
2. Maven installation-
Download maven from Apache Maven’S website –https://maven.apache.org/download.cgi
Extract the downloaded folder and set the environment variable to your maven folder.
Add Maven’S bin directory to your systems path.
Verify installation by running mvn -version in your terminal.
Maven Repository
Maven repository is a directory of packaged jar files with Pom.XML file. Maven searches for
Dependencies in the repository.
There are 3 types of repository.
- Local repository
- Central repository
- Remote repository
Maven searches for the dependency in the following order:
Local repository than central repository than remote repository
If dependency is not found in these repositories, maven will stop and throw an error.
- Maven Local Repository –
Maven local repository is located in your local system.it is created by maven when you run any maven command.
The default path of maven local repository is. C:\Users\(your name folder)\.m2.
2. Maven Central Repository-
Maven central Repository is located on the web. It has been created by Apache maven community itself.
The path of central repository is – http://repo1.maven.org/maven2/
Central repository contains a lot of common libraries that con be viewed by this URL –https://search.maven.org/#browse
3. Maven remote repository
May when remote repository is located on the web. Most of the libraries can be missing from the central repository, so we need to define more repository in pom.XML
Lets see how to add the junit in Pom. XML
You can search any repository from Maven official website mvnrepository.com
Maven Pom.XML file
POM stands for Project Object Model. It’s a file in more that contains all the information and
Settings needed to build, manage and configure a project.
Maven reads the pom.xml file, than executes the goal.
Before maven 2, it was named as project.xml file, but since maven 2, it is renamed as pom.xml
Elements of maven pom.xml file
To Create simple Pom.XML file you need the following elements.
Element | Description |
Project | It is root element of pom.xml file. |
modelVersion | It is a subelement of project. It specifies the modelVersion. It should be set to 4.0.0 |
GroupId | It is the element of project. it specifies the id for the project group. |
artifactId | It is sub element of project. It specifies they id for the artifact (project). An artifact is some thing that is either produced or used by a project. Examples of artifacts produced by maven for a project includes: JARs, source and binary distributions, and WARs. |
Version | It is a sub element of project. It specifies the version of the artifact under given group. |
Check out more Blogs – Click Here