DevOps Certification Training Course
- 181k Enrolled Learners
- Weekend/Weekday
- Live Class
The Maven project is developed by Apache Software Foundation where it was formerly a part of the Jakarta project. The reasons behind Maven’s popularity are dependency management, support environment (plugins etc) and build life cycle support. Maven is a tool that is a part of the build phase of the DevOps lifecycle. So in this blog on Maven Interview Questions, we have covered the maximum Maven questions that you might encounter during the interview.
A few questions that I have covered in this blog are as follows:
Ant | Maven |
Ant doesn’t have formal conventions, so we need to provide information about the project structure in the build.xml file. | Maven has a convention to place source code, compiled code, etc. So we don’t need to provide information about the project structure in pom.xml file. |
Ant is procedural, you need to provide information about what to do and when to do through code. You need to provide order. | Maven is declarative, everything you define in the pom.xml file. |
There is no life cycle in Ant. | There is a life cycle in Maven. |
Ant is a toolbox. | Maven is a framework. |
It is mainly a build tool. | It is mainly a project management tool. |
The ant scripts are not reusable. | The maven plugins are reusable. |
It is less preferred than Maven. | It is more preferred than Ant. |
NOTE: This is a common Maven Interview Question that you must know.
Maven is a project management and comprehension tool. Maven provides developers a complete build lifecycle framework. The development team is easily able to automate the project’s build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.
Some of the main features of Maven are:
It aids in setting up the project very quickly and it avoids complicated build files like build.xml. the pom.xml file is at the core of Maven.POM.xml is a collection of dependencies of your Java Project which one can specify to Maven. After this Maven will download all of them from the internet and store it to some repository i.e. local repository, central repository, and remote repository.
It helps to bundle all the jars in your package i.e. in your War file or Ear file because all of them will be stored in the repository. So next time wherever you install this application that repository will be used for any dependencies lookup. In this way, your deployment file will be very light.
Maven can be downloaded and installed on windows, linux, and MAC OS platforms. To install Maven on windows, you need to perform the following steps:
In the case of Configuration, developers have to create the build processes manually and they have to specify each and every configuration in detail. But, Maven uses convention where the developers need not create the build processes manually.
Also, for convention, users do not need to specify the configuration in detail. Once a developer creates a project in Maven then Maven will automatically create a structure. Developers just have to place the files appropriately. There is no need to specify any configuration details in pom.xml file.
NOTE: This is an important Maven Interview Question that you must know.
Maven provides developers ways to manage following −
Type the following command – mvn -version
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 −
A Build Lifecycle can be defined as a well-defined sequence of phases. It clearly defines the order in which the goals are to be executed. Each build phase contains a sequence of goals. If one life cycle is executed, all build phases in that life cycle are executed. If a build phase is executed, all build phases before it in the pre-defined sequence of build phases are executed.
NOTE: This is another important Maven Interview Question that you must know.
The three build lifecycles are −
clean: cleans up artifacts created by prior builds.
default: used to build the application.
site: generates site documentation for the project.
Type the command − mvn site
This command deletes the target directory with all the build data before starting the build process.
Following are the phases of Maven build lifecycle −
validate − validate the project and check if everything is correct and all necessary information is available.
compile − this phase compiles the source code of your project.
test − tests the compiled source code by using a suitable unit testing framework. These tests should not require the code to be packaged or deployed
package − takes the compiled code and packages it in its distributable format.
integration-test − processes and deploys the package if possible into an environment where integration tests can be run.
verify − runs any checks to verify the package is valid and meets the required quality criteria.
install − installation of the package into the local repository. This is done to use it as a dependency in other projects locally.
deploy − done in an integration environment or release environment. Here the final package is copied to the remote repository for sharing with other developers and projects.
A goal represents a specific task that contributes to the building and managing of a project. It is bound to zero or more build phases. A goal that is not bound to any build phase could be executed outside of the build lifecycle by invocating it directly.
This command will clean the project, copy the dependencies and package the project (executing all phases up to package).
The clean lifecycle consists of the following phases −
pre-clean
clean
post-clean
The phases in Site Lifecycle are −
pre-site
site
post-site
site-deploy
A Build profile is a set of configuration values that 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 and the Development environments.
Build profiles are of three types −
Per Project − Defined in the project pom.xml file.
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)
A Maven Build Profile can be activated in the following ways −
A repository is a place i.e. a directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and this can be used by Maven easily.
Maven repositories are of three types –
Local: Maven local repository is a folder location that is present on your machine. It is created when you run any maven command for the first time. Maven local repository is a location where you can find your project’s all dependencies (library jars, plugin jars etc).
Central: It is a repository provided by the Maven community. It contains a huge collection of commonly used libraries. When Maven does not find any dependency in local repository, it starts searching in central repository using the following URL: http://repo1.maven.org/maven2/.
Remote: Sometimes, Maven is not able to find a mentioned dependency in the central repository as well then it stops the build process and an output error message is displayed on the console. To avoid such a situation, Maven provides the idea of Remote Repository which is nothing but the developer’s own custom repository containing required libraries or other project jars.
~/m2./repository.
mvn install
Following is the search pattern –
Maven Plugins are used to −
Maven provides the following two types of Plugins −
Build plugins −They come into picture during the build and should be configured in the <build/> element of pom.xml
Reporting plugins −They get executed during the site generation and they should be configured in the <reporting/> element of the pom.xml
Maven dependency management uses the concept of Maven Repositories (Local, Central, Remote). Suppose dependency is not present in any of remote repositories and central repository then in such case Maven uses the concept of External Dependency.
External dependencies (library jar location) can be configured in pom.xml in same way as other dependencies are configured.
An archetype is a Maven plugin whose task is to create a project structure as per its template.
Type the following command − mvn archetype:generate
SNAPSHOT can be defined as a special version that indicates a current development copy. Unlike the regular versions, Maven checks for a new SNAPSHOT version in its remote repository. Maven does it for every build.
In the case of Version, if Maven once downloads the mentioned version say data-service:1.0, it will never try to download a newer 1.0 available in the repository. To download the updated code, the data-service version is then upgraded to 1.1.
In the case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (data-service:1.0-SNAPSHOT) every time the team builds its project.
Transitive dependency is nothing but to avoid the need to discover and specify the libraries that your own dependencies require, and including them automatically.
39. What does dependency management mean with respect to transitive dependency?
It simply means to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies. For example project C can include B as a dependency in its dependencyManagement section and directly control which version of B is to be used when it is ever referenced.
If you find two dependency versions at the same depth in the dependency tree, then you use the first declared dependency. This is nothing but dependency mediation.
Dependency scope typically includes dependencies as per the current stage of the build. The various Dependency scopes are −
compile − This scope indicates that dependency is available in the classpath of the project. It is the default scope.
provided − This indicates that the dependency is to be provided by JDK or web-Server/Container at runtime.
runtime − This scope tells that you dont need dependency is for compilation but you need it for for execution.
test − This scope states that the dependency is only available for the test compilation and execution phases.
system − This scope indicates that you must provide the system path.
import − This scope is only used when the dependency is of type pom. This scope tells that the specified POM should be replaced with the dependencies in the POM’s <dependencyManagement> section.
{groupId,artifactId,type,classifier}.
NOTE: This is a frequently asked Maven Interview Question that you must know.
The valid packaging values include jar, war, ear, and pom. If no packaging value has been specified, then by default it will be a jar.
<groupId>:<artifactId>:<version>
All POMs are inherited from a parent despite explicitly defined or not. This base POM is called Super POM and it contains values that are inherited by default.
Profiles are specified by making use of a subset of the elements that are available in the POM itself.
<repositories>, <pluginRepositories>,<dependencies>, <plugins> ,<properties>, <modules><reporting>,<dependencyManagement>,<distributionManagement>
It uses less storage and also makes checking out a project quicker, without the need for versioning the JAR files.
Dependency with scope system is always available and is not looked up in the repository, they are usually used to tell Maven about dependencies that are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts that JDK normally provides.
You can mark any transitive dependency as optional using the “optional” element. As an example, A depends upon B and B depends upon C. Now B marked C as optional. Then A will not use C.
You can exclude any transitive dependency using the “exclusion” element. Suppose if A depends upon B and B depends upon C then A can be marked C as excluded.
For this, you just need to place the clean plugin inside the execution tag in pom.xml file.
It simply means that you have executed a plugin multiple times with the same <id>. To correct this you just need to provide each <execution> with a unique <id>.
So now we have come to an end of this blog on Maven Interview Questions. We have tried to cover almost everything in this blog. If you feel that we have missed out on something then please tell us about it in the comment box below.
If you find this Maven Interview Questions relevant, check out the DevOps Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The Edureka DevOps Certification Training course helps learners gain expertise in various DevOps processes and tools such as Puppet, Jenkins, Nagios and GIT for automating multiple steps in SDLC.
Course Name | Date | Details |
---|---|---|
DevOps Certification Training Course | Class Starts on 25th November,2024 25th November MON-FRI (Weekday Batch) | View Details |
DevOps Certification Training Course | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co