20+ Maven Interview Questions and answers. You must prepare for 2021

This tutorial covers interview questions and answers for java developers. This was also useful for experienced persons as well as for DevOps engineers. You can check my other posts on maven commands

So let’s dive deep into useful latest maven interview questions and answers.

What is Maven?. Why it is used for?

Maven is a Java build and deployment tool used for web, mobile, and desktop applications in the development process and it is declarative in the pom.xml. the configuration file contains all the dependencies and scm source.

How to know the maven version using the command line

use the following code via command line

mvn --version

What are the different build phases/goals of a maven project?

By default, It has three inbuilt goals for the life cycle of a project.

  • default
  • clean
  • site

There are different maven phases in a build.

  • validate: validation to check all things are available for build
  • compile : Compiles all java source code available src//main//java
  • test-compile: compiles all test Java source files located in src//main//test
  • test: Running java test cases
  • package: package the project and output is jar, war or ear
  • integration-test: Running integration tests
  • install: packaged files (jar, war, or ear) to local server or folder.
  • deploy: packaged files to remote server or folder or repository.

All these phases are executed sequentially.

You can also run each phase using the below command.

mvn compile

What are things we can do with maven?

  • Build and compile
  • Site documentation
  • Reporting and code quality check
  • Artifacts release management
  • Integration with git or svn to repository branch management
  • Ship an application with different environments

How do you create a war file using maven?

use the following code via command line
use install goal to create a war file

mvn clean install

What are the different goals available in a maven project?

There are different predefined available goals.

Goal Name  Description 
cleanremove all the files generated during the previous run under the target folder

| |test| running unit and integration tests| |install| compiles and runs and generates war file in the target folder| |deploy| the generated war file to the target server. You need to configure server details in pom.xml | |site| Generates artifacts site documentation for maven project|

Explain POM.xml in detail?

The pom.xml file is an important component of any maven project.

It contains important information about your project and dependencies.

POM is abbreviated as Project object model which is an XML file.

  • Project detailed information like artifact name and snapshot version dependencies SVN/git repository information
  • Profiles, where the Development codebase has a dev profile and production, have a different profile
  • Plugin configuration Parent dependencies

How to compile java projects using maven

Maven has a predefined goal called compile that compiles all the java files and generates class files in the target folder. And test goal is to compile test java files and generate class files in a target directory.

// compile source code
mvn compile
// compile test java files
mvn test

To know Maven’s direct and indirect dependencies?

Sometimes a developer needs to know project dependencies and transitive dependencies to resolve conflicts with different versions of artifacts.

mvn dependency:tree

How to compile and run the test code of the project

You can compile the test code using the test goal. if you want to run it, You need maven surefire plugin dependency. You need the configuration of test classes and resource files in pom.xml for the generation of output.

How to skip testing during the maven build process?

sometimes it is required to skip the test class running. there are many ways to skip running test classes. use -DskipTests or -Dmaven.test.skip=true options to maven command.

How to configure Proxy Settings in the maven application

Proxy for HTTP and HTTPS is configured in settings.xml.

You can find the path of this file in C:\\Documents and Settings\\username\\.m2\\. If you don’t find it, You can create and add the below content to it.

Here is a sample settings.xml example

    <!-- Http Proxy setting configuration-->
    <!-- HTTPS Proxy configuration details -->

What is the difference between Maven and Ant tools?

Both are used to build and deploy java projects developed by the Apache foundation.

Here is the difference between maven and ant.

Maven contains goalsAnt contains scripts
goals here are reusablescripts are not reusable
Project dependencies management toolOnly Build tool
It has inbuilt in the lifecycle and is customizableNo lifecycle scripts
Maven has convention rules and directory structureThere is no coding convention or project structure
!All the configurations configure in pom.xml! build.xml contains default configuration
All the dependencies copied from local and remoteDependencies are part of the project
It is very easy to manage dependency versionsIt is difficult to manage project dependency versions

What is the difference between Maven and Gradle tools?

Maven and Gradle are complete project management.

Here is the difference between maven and Gradle.

It has a pom.xml file containing project dependencies and other configurationsIt is not an XML file for configuration information, But uses Domain Scripting language(DSL)
Continuous Integration is supportedIt has also support for CI tools
Compilations are slow as incremental build not supportedFast compilation as only modified files are compiled
Customization is difficultEasy customization