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

This tutorial provides interview questions and answers specifically tailored for Java developers. It is beneficial not only for those with experience but also for DevOps engineers.

Feel free to explore my other posts, such as the one 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 employed in the development process for web, mobile, and desktop applications. It operates on a declarative basis through the pom.xml file, where all dependencies and source control management (SCM) sources are specified.

Maven serves to streamline the build process, enhance build performance, and automate continuous integration and deployment procedures.”

How to know the maven version using the command line

To find the version, use the following command in the command line:

mvn --version

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

By default, Maven includes three built-in goals for managing the life cycle of a project

  • default
  • clean
  • site

Throughout the build process, Maven comprises various phases, each serving a specific purpose:

  • validate: Ensures all necessary elements are available for the build.
  • compile: Compiles all Java source code located in src/main/java.
  • test-compile: Compiles all test Java source files found in src/main/test.
  • test: Executes Java test cases.
  • package: Packages the project, producing output in the form of jar, war, or ear files.
  • integration-test: Executes integration tests.
  • install: Installs packaged files (jar, war, or ear) locally to a server or folder.
  • deploy: Deploys packaged files to a remote server, folder, or repository. These phases are executed sequentially, and you can run each phase individually using the following command
mvn goal

For example, to compile the Java and test classes, use the below command

mvn compile

What are things we can do with Maven?

  • Build and Compile Compile, copy, and test the source code.

  • Site Documentation Generates site documentation for the project.

  • Reporting and Code Quality Check Analyze the code ensure code quality guidelines are met, and display the reports.

  • Artifacts Release Management Publishes the releases to the repository, downloads the artifacts and maintains multiple versions for the same artifact. It generates EAR, JAR, and others, and deploys them to a remote server.

  • Integration with Git or SVN for Repository Branch Management Integrates with version control systems like Git or SVN to facilitate easy branch management in the repository. It allows for easy integration with DevOps process tools such as CI and CD.

  • Deploying an Application in Different Environments Deploys the application in different environments such as development, staging, and production.

How do you create a war file using Maven?

To create a WAR file using the maven-war-plugin in the pom.xml and the install goal, you can use the following command in the command line:

mvn clean install

maven-war-plugin is properly configured in your pom.xml file. If not, you’ll need to add the plugin configuration to specify how the WAR file should be built

Here’s a basic example

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.1</version>
            <configuration>
            <!-- add configuration-->
            </configuration>
        </plugin>
    </plugins>
</build>

What are the different goals available in a maven project?

There are different predefined available goals.

Goal NameDescription
cleanRemove all the files generated during the previous run under the target folder
testrunning unit and integration tests
installcompiles and runs and generates war file in the target folder
deploythe generated war file to the target server. You need to configure server details in pom.xml
siteGenerates artifacts site documentation for maven project

Explain POM.xml in detail.

The pom.xml file is a crucial component of any Maven project, encompassing vital information about your project and its dependencies.

POM is an abbreviation for Project Object Model, presented in an XML file format.

The pom.xml file includes:

  • Detailed project information, such as artifact name, snapshot version, and dependencies, along with SVN/Git repository information.
  • Profiles, differentiating the development codebase with a ‘dev’ profile and production with a distinct profile.
  • Plugin configuration and 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, developers need to be aware of both project dependencies and transitive dependencies to effectively resolve conflicts arising from 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

<proxies>
    <!-- Http Proxy setting configuration-->
    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>proxyuser</username>
        <password>proxypass</password>
        <host>proxy-hostname.com</host>
        <port>80</port>
        <nonProxyHosts>otherhosts.com</nonProxyHosts>
    </proxy>
    <!-- HTTPS Proxy configuration details -->
    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>https</protocol>
        <username>username</username>
        <password>password</password>
        <host>proxy-hostname.com</host>
        <port>80</port>
        <nonProxyHosts>otherhosts.com</nonProxyHosts>
    </proxy>
</proxies>

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.

MavenAnt
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 are configured 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.

MavenGradle
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