What is Maven?
Answer: Maven is a build automation tool used primarily for manage and build Java projects. Maven can also be used to build and manage projects written in other languages like C#, Ruby, Scala, etc. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.
What are the advantages of using Maven in a Project development?
Answer: Below are some of key benefits of using maven,
- Maven can add all the dependencies required for the project automatically by reading the pom file.
- We can easily build our project to jar, war etc. as per our requirements using Maven.
- Maven makes easy to start any project in different environments and we don’t need to handle the dependency injection, build, packaging, etc.
- Adding a new dependency is very easy. We have to just write the dependency code in the pom file.
What are the tasks which are performed by Maven?
Answer: Below are the tasks performed by Maven,
- It builds a project.
- We can add jars and other dependencies of the project easily using the help of maven.
- It makes it easy for a project while updating central repository of JAR’s and other dependencies.
- It help us build any number of projects into output types like the JAR, WAR etc without doing any scripting.
- It enable us to easily integrate our project with source control repository (such as Subversion or Git).
- Maven can also provide project information (log document, dependency list, unit test reports etc.) which will be handy while managing the project.
What is the Role of a build tool in Maven?
Answer: Build tool/plugin in Maven executes during the build process, it executes the tasks based on its configuration in <build/> element of pom.xml.
What are the difference between Maven and ANT?
Answer: Both Maven and ANT are build tools and both of them are developed and maintained by Apache. Below are some their differences.
|Maven is a framework.||ANT is a tool.|
|It can be used to manage the entire Project like building, managing dependencies, documentation etc.||It is only a build tool.|
|Maven is Plugin based.||ANT is script based.|
|Maven is declarative, we need to define everything in the pom.xml||ANT is procedural, meaning we need to write script to perform build.|
How to install Maven in windows?
Answer: Follow the below steps to Install Maven in Windows OS:
- Download Maven from maven.apache.org.
- Extract the zip file content into a Folder.
- Make sure Java SDK is installed as it is a pre-requisite.
- Add MAVEN_HOME in environment variable and update the Maven Path.
- Verify Maven – using the command mvn -version in command prompt.
Refer to this article for more detailed steps.
What is Maven repository and what are its types?
Answer: Maven repository is nothing but a directory where all the project Jars, dependency library jars, plugins or any other project specific artifacts are stored and can be used by Maven. Below are three types of repositories,
- Local Repository
- Central Repository
- Remote Repository
What is a pom.xml file in Maven?
Answer: Project Object Model (POM) file is an XML file that contains configuration information related to the project such as dependencies, source directory, plugin, and goals etc. This file is used by Maven to build the project. When we should execute a maven command we give maven a POM file to execute the commands. Maven reads that pom.xml file to accomplish its tasks.
What are all the dependency scope in Maven?
Answer: Maven provides six dependency scopes i.e. compile, provided, runtime, test, system, and import. These dependency scope attributes are used to specify the visibility of a dependency, relative to the different maven life-cycle phases (build, test, runtime etc).
What are the build life cycle of Maven? list them and explain.
Answer: A build life-cycle in Maven is a well-defined sequence of phases, these sequence define the order in which the goals have to be executed. Here phase represents a particular stage in the Maven life cycle. For an example, a typical Maven Build Life-cycle may consist the following sequence of phases.
- Prepare-resources – this phase handles resource copying – Resource copying can be customized in this phase.
- Validate – this phase validates the information – for example It validates if the project is correct and if all necessary information is available.
- Compile – this phase handles compilation – Source code compilation is done in this phase.
- Test – this phase performs Testing – Tests the compiled source code suitable for testing framework.
- Package – this phase performs packaging – basically it creates the JAR/WAR package as mentioned in the packaging in POM.xml.
- Install – this phase performs the installation – basically this installs the package in local/remote maven repository.
- Deploy – this phase takes care of Deploying – It Copies the final package to the remote repository.