Google News
logo
Java Maven Interview Questions
Maven is a project management tool (Introduced by Apache Software Foundation) that provides an entire framework for the build cycle. It is open-source and is mainly used for the project developed in Java.
 
Maven is driven by a Project Object Model popularly known as POM. It is the central repository for all the dependencies. It maintains the same folder convention across organizations and can be easily integrated with continuous integration tools like Jenkins.
 
Besides, it takes care of the reporting and documentation of the project. It is developed in Java language.
 
Current version of Maven is 3.8.4.
 
POM stands for Project Object Model. In Maven, it is a fundamental Unit of Work and it is an XML file. You can find it in the base directory of the project. It consists of information about the project and various configuration details used by Maven to build the project(s).
POM contains the following configuration information :
 
* project dependencies
* plugins
* goals
* build profiles
* project version
* developers
* mailing list
The features or advantages of Maven are as follows :
 
* Not required to manually add jars for the project. The updates to the project dependencies and transitive dependencies are carried out automatically by Maven.
* Maven maintains a uniform directory structure across the organization.
* Both the deployment and build activities are taken care by Maven.
* Maven is simple, easy to grasp, to set up and utilize in the projects as it is driven by the POM file.
* Rapidly expanding repository of Maven contains a large number of libraries that can be used for multiple projects at a time.
* Maven encourages the use of extensible code design.
A Maven build life cycle comprises of certain phases. It determines the order in which the Maven goals need to be performed. If a particular phase is to be accomplished, then all the previous phases prior to that particular phase need to be run successfully in sequence.
 
Maven build life cycles are listed below as the followings :
 
* Clean : Removes the artifact produced from the preceding build processes.
* Default : Deployment and build process is taken care of.
* Site : Project documentation is carried out in this cycle.
Maven helps with the following :
 
* Apache Maven helps manage all the processes, such as building, documentation, releasing, and distribution in project management.
* The tool simplifies the process of project building. It increases the performance of the project and the building process.
* The task of downloading JAR files and other dependencies is done automatically.
* Maven provides easy access to all the required information.
* Maven makes it easy for the developer to build a project in different environments, without worrying about the dependencies, processes, etc.
* In Maven, it is simple to add new dependencies, all you need to do is to write the dependency code in the pom file.
Ant and Maven both are build tools provided by Apache. The main purpose of these technologies is to ease the build process of a project.

ANT: 

* ANT has no formal conventions, so information is to be provided in the build.xml file.
* ANT is procedural.
* ANT is a tool box.
* ANT has no life cycle.
* ANT scripts are not reusable.
* ANT is specifically a build tool.
* ANT is less preferred than Maven.
 
Maven :
 
* Maven has conventions, so information is not to be provided in the pom.xml file.
* Maven is declarative.
* Maven is a framework.
* Maven has a life cycle.
* Maven is mainly used as a project management tool.
* Maven plugins are reusable.
* Maven is more preferred than Ant.
The minimum required elements for POM are :
 
* project root
* modelVersion – should be 4.0.0
* groupId – project’s group id
* artifactId – artifact (project) id
* version – version of the artifact
A 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.
A Build profile is a set of configuration values which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.
Build profiles are of three types :
 
Per Project : Defined in the project POM file, pom.xml.
 
Per User : Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml).
 
Global : Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml)
Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.
Yes we can create a custom archetype. For example, in our project we created a archetype which would create a custom maven projects with modules as DAO, SERVICE and WEB. These modules had the required dependencies. All project members used this archetype as it saved a lot of time and every one had same starting workspace.
* Maven artifact is known as a file, usually a JAR, that gets deployed to a Maven repository. A Maven build produces one or more artifacts, such as a compiled JAR and a "sources" JAR.

* Each artifact has a group ID (usually a reversed domain name, like com.javainuse), an artifact ID (just a name), and a version string. The three together uniquely identify the artifact.

* A project's dependencies are specified as artifacts.
 
<dependency>
    <groupId> org.selenium </groupId>
    <artifactId> seleniumJava </artifactId>
    <version> 4.0.2 </version>
</dependency>

 

Maven works wonders in setting up the project quickly and doesn’t include complex build files like build.xml. The dependencies of your Java project are automatically downloaded by Maven and stored in a local repository and can be retrieved in quick time, whenever needed. 
 
Maven also helps keep the deployment file light and easy to deploy, as it brings all the jars together in a bundle and stores them in the local repository. 
Plugins in Maven are primarily used to :
 
* Create code files 
* Create Jar files
* Create War files
* Create documentation for projects
* Create detailed project reports
There are basically 2 types of plugins that Maven provides for :
 
* The build plugins : These plugins are used during the Build process and can be configured in the <build/> element of the POM.xml file. 

* Reporting plugins : These plugins come into effect during the site generation process. They can be configured in the <reporting/> element of the POM.xml file. 
Dependency scopes include project dependencies that are in line with the current, active stage of the build process. The different Dependency scopes in Maven are :
 
Compile : This scope specifies that the current dependency is available in the classpath of the current project. 
Provided : This scope specifies that the web server will provide the dependency during runtime.
Runtime : This particular scope indicates that the dependency isn’t needed during compile time and is only needed during execution. 
Test : This scope indicates that the dependency is available limitedly during the test compilation phase. 
System : This scope specifies that the system path for the given build phase needs to be provided. 
Import : This scope indicates that the given POM should be replaced with congruent dependencies in the POM’s <DependencyManagement> section. 
We will use the below command to know the Maven version :
 
mvn –version

We will use the below command to perform a force update in Maven :
 
mvn clean install –U
To create a new project based on an archetype, we will use the below command :
 
mvn archetype : generate

A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos. In short, a mojo is a maven goal, to extend functionality not already found in maven.
The maven local repository is a local folder that is used to store all your project’s dependencies (plugin jars and other files which are downloaded by Maven). In simple, when you build a Maven project, all dependency files will be stored in your Maven local repository.
There are three types of Maven repositories.
 
They are :
 
* Local Repository : This is placed in our local machine generally in the .m2 directory. This is generated once we are able to execute a Maven command successfully. All the project dependencies reside here. Once Maven scans the pom file, it first searches for its dependencies in the local repository.

* Central Repository : This is supplied by Apache Maven. It contains most of the routinely used libraries. Once any of the dependencies are not found in the local repository, then Maven connects to a central repository.

* Remote Repository : Sometimes companies develop their own custom repository comprising of their project artifacts, jars, and libraries. This is a type of repository that remains private for use inside that organization.
Maven snapshot is that version which is still unreleased. Prior to the first release is completed, there is a 1.0-SNAPSHOT version. In the future, this version will emerge to be 1.0 version. It is called as the present development copy. Maven looks for the new SNAPSHOT version in the remote repository.
Once Maven reads through the pom file, it gets to know the list of dependencies for the project. It searches for these dependencies in Local, Central and Remote repositories. In case any of the dependency not found in any of the repositories, then Maven utilizes the external dependency.
Maven external dependencies are described in the pom xml file just like other dependencies. The parameters required to define the external dependencies are groupid, artifactId, scope set to system and system path as per the project location (relative path).
It is recommended to keep the external dependencies in the local repository instead of remote in Maven because local repository consumes less space, are easily accessible and there is no need to take care of versioning for jars.
The Maven clean lifecycle takes care of eliminating all temporary files from the output directory, including generated source files, compiled classes, and previous JAR files, among other things.
 
* pre-clean : performs tasks that are necessary prior to actual project cleaning.
* clean : delete all files created by the previous build.
* post-clean : performs tasks that are necessary to finalize project cleaning.
Everything related to generating documentation for your project is handled by the Maven site lifecycle.
 
* pre-site : performs tasks that are necessary prior to actual project site generation.
* site : develop the project’s site generation.
* post-site : performs tasks that are necessary to finalize project site generation, also prepares for site deployment.
* site-deploy : deploy the developed site documentation to the web server of your choice.
A Maven Build Profile can be activated in the following ways :
 
* Explicitly using command console input.
* Through maven settings.
* Based on environment variables (User/System variables).
* OS Settings (for example, Windows family).
* Present/missing files.
* Build = Target
* Source = src/main/java
* Test = src/main/test

jar: jar will not recompile sources; it will imply just create a JAR from the target/classes directory considering that everything else has been done.
In Maven, the order of inheritance is :
 
* Settings
* CLI parameters
* Parent POM
* Project POM
The maven-javadoc plugin is used by Maven to generate a project's javadocs. To create javadocs, this plugin internally uses the JDK\bin\javadoce.exe command. The javadocs for the project are generated when the project is deployed with the mvn install command.
The -mvn archetype: create is used to start a new project.
After reading the source and resource files, as well as the values of its parameters and other properties, the archetype is constructed.
* Maven clean is a plugin that, as the name implies, attempts to clean the files and directories generated by Maven during the build process. The target folder, which contains all of the class files, documentation, and JAR files, is removed by the plugin.
 
* The Surefire Plugin is used to run an application's unit tests during the test phase of the build lifecycle. It can generate reports in one of two file formats: plain text files or XML files.
 
* The Antrun Plugin allows you to perform Ant tasks directly from within Maven. Your Ant scripts can even be embedded in the POM!
A Maven installation is configured using the settings.xml file. It's comparable to a pom.xml file, but it's either global or user-specific. The Maven settings.xml file provides elements that define the values required to configure Maven's execution in several ways. These values include the location of the local repository, authentication information, and alternate remote repository servers among others.
 
Let's look at the elements in the settings.xml file that we can change. The settings.xml file's main element, settings, can include up to nine predefined child elements :
 
<settings xmlns = "http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
   <localRepository/>
   <interactiveMode/>
   <offline/>
   <pluginGroups/>
   <servers/>
   <mirrors/>
   <proxies/>
   <profiles/>
   <activeProfiles/>
</settings>
 
The following configurations are included :
 
* Proxy configuration
* Local repository configuration
* Remote repository configuration
* Central repository configuration
When multiple versions of an artifact are encountered, Maven determines which version of the dependency should be used. The earliest declared dependence will be used if two dependency versions are at the same depth in the dependency tree. This is referred to as "dependency mediation."
 
Dependency management allows project authors to declare the versions of artifacts that are to be utilized when they are discovered in transitive dependencies or dependencies that have no version specified.
* Maven clean is a plugin as the name suggests, that approaches to clean the files and directories generated by Maven at the time of its build.
* The plugin removes the target folder that contains all the class files, docs, JAR files.
* System dependency refers to the dependency that is present with the scope system. These dependencies are commonly used to help Maven know the dependencies that are provided by the JDK or VM.
 
* System dependencies are mostly used to resolve dependencies on artifacts that are provided by the JDK.
* Optional dependencies are used to decrease the transitive burden of some libraries.
* These dependencies are used when it is not feasible to divide a project into sub-modules.
* Some dependencies are only used for a specific feature in the project, and if that feature is not there, then that dependency will not be used.
* Maven avoids the need to find out and specify libraries that the dependencies require, by including transitive dependencies automatically.
* Transitive dependency says that if X depends on Y and Y depends on Z, then X depends on both Y and Z.
Project Aggregation specifies the modules from the parent POM instead of specifying the parent POM from the module. As a result, the parent project is aware of its modules, and if a Maven command is issued against the parent project, the Maven command is also applied to the parent's modules. For Project Aggregation, you must accomplish the following :
 
* Change the packaging of the parent POMs to "pom"
* Specify the modules' directories in the parent POM (children POMs).
Doxia is a content creation framework that aims to give powerful approaches for creating static and dynamic content to its users: Doxia can be used to create static web pages in a web-based publication context, as well as in dynamic content creation systems such as blogs, wikis, and content management systems.
 
Maven makes substantial use of Doxia, which powers the project's complete documentation system. It enables Maven to take any Doxia-supported document and output it in any format.
 
For instance, 'mvn site' is the command used by Maven to produce javadocs for a specific project. Maven calls Doxia document generation and other report generating plugins when this command is run.
package : converts the compiled code into a distributable format, such as a JAR.
 
install : adds the package to the local repository, allowing it to be used as a dependency in other projects.
The above command is used to clear the project. It will then copy any dependencies and execute all phases of the project.
The fully qualified artifact name of a project is : <groupId>:<artifactId>:<version>