What is Maven ?

What is Maven ?




Maven is a project management and comprehension tool that provides developers a complete build lifecycle framework. 
Development team can automate the project's build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.
In case of multiple development teams environment, Maven can set-up the way to work as per standards in a very short time. As most of the project setups are simple and reusable, Maven makes life of developer easy while creating reports, checks, build and testing automation setups.
Maven uses Convention over Configuration, which means developers are not required to create build process themselves.
Developers do not have to mention each and every configuration detail. Maven provides sensible default behavior for projects. When a Maven project is created, Maven creates default project structure. Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml.
Maven is a Java based tool, so the very first requirement is to have JDK installed on your machine.

Project Object Model (POM)

POM is the fundamental unit of work in Maven. 
It is an XML file that resides in the base directory of the project as pom.xml.
The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM also contains the goals and plugins.
Some of the configuration that can be specified in the POM are following :

  • project dependencies
  • plugins
  • goals
  • build profiles
  • project version
  • developers
  • mailing list
All POM files require the project element and three mandatory fields: groupId, artifactId, version.
Before creating a POM, we should first decide the project group (groupId), its name (artifactId) and its version as these attributes help in uniquely identifying the project in repository.

Build Life Cycle

A Build Lifecycle is a well-defined sequence of phases, which define the order in which the goals are to be executed.
goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.
Maven has the following three standard lifecycles 

  • clean deletes the output of a build by deleting the build directory. Thus, when mvn clean command executes, Maven deletes the build directory.
  • default(or build) -  compiles the project
  • site used to create fresh documentation to create reports, deploy site, etc. 

Maven Plugins

Maven is actually a plugin execution framework where every task is actually done by plugins. Maven Plugins are generally used to −
  • create jar file
  • create war file
  • compile code files
  • unit testing of code
  • create project documentation
  • create project reports
A plugin generally provides a set of goals, which can be executed using the following syntax ,
            mvn [plugin-name]:[goal-name]
for example,
            mvn compiler:compile

Maven Repositories


In Maven terminology, a repository is a directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily.
Maven repository are of three types. The above illustration will give an idea regarding these three types.

1. LocalMaven local repository is a folder location on your machine. It gets created when you run any maven command for the first time.
2. Central
Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.
3. Remote
Sometimes, Maven does not find a mentioned dependency in central repository as well. It then stops the build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository, which is developer's own custom repository containing required libraries or other project jars.


Creating a Maven Project


Maven uses archetype plugins to create projects. To create a simple java application, we'll use maven-archetype-quickstart plugin. In example below, we'll create a maven based java application project in C:\MVN folder.

Let's open the command console, go to the C:\MVN directory and execute the following mvn command.
C:\MVN>mvn archetype:generate
-DgroupId = com.companyname.bank 
-DartifactId = consumerBanking 
-DarchetypeArtifactId = maven-archetype-quickstart 
-DinteractiveMode = false
Now go to C:/MVN directory. You'll see a java application project created, named consumer Banking (as specified in artifactId). Maven uses a standard directory layout as shown below.

consumerBankingcontains src folder and pom.xml
src/main/java - contains java code files under the package structure (com/companyName/bank)
src/main/testcontains test java code files under the package structure (com/companyName/bank)
src/main/resourcesit contains images/properties files 

Comments

Popular posts from this blog

A Start To Learn Mongo DB

Building a React CRUD application using MERN stack

Intro To ReactJS