✅ Maven Interview Questions and Answers

Vaibhav UpareVaibhav Upare
5 min read

1. What is Maven?

Maven is a build automation and dependency management tool for Java-based projects. It simplifies the process of building, reporting, and documenting via a Project Object Model (POM).


2. What are the advantages of using Maven?

  • Dependency Management: Automatically downloads and resolves dependencies.

  • Consistent Builds: Ensures builds are reproducible.

  • Convention over Configuration: Reduces configuration effort.

  • Multi-module project support.

  • Integration with CI/CD tools.


3. What is a POM file? What does it contain?

POM (Project Object Model) is an XML file (pom.xml) that describes the project and configuration details like:

  • Project coordinates (groupId, artifactId, version)

  • Dependencies

  • Build plugins

  • Profiles

  • Repositories

  • Properties


4. What is the default location of the local repository in Maven?

~/.m2/repository
This is the local cache where Maven stores downloaded artifacts.


5. What is the difference between install, package, and deploy goals in Maven?

  • package: Compiles code and packages it into a .jar or .war.

  • install: Installs the package into the local repository.

  • deploy: Uploads the built artifact to a remote repository (used in release processes).


6. How do you add a dependency in Maven?

Add the dependency block to pom.xml:

xmlCopyEdit<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>6.0.0</version>
</dependency>

7. What is the role of settings.xml in Maven?

settings.xml (located in ~/.m2/) contains:

  • Proxy settings

  • Repository credentials

  • Mirrors

  • Profiles specific to the user or environment


8. What are transitive dependencies?

Dependencies that are not directly declared but are included because they are required by other dependencies. Maven resolves them automatically.


9. What is the Maven lifecycle?

Three built-in lifecycles:

  1. Clean – cleans the project (clean)

  2. Default – builds the project (compile, test, package, etc.)

  3. Site – generates documentation (site)

Each lifecycle consists of phases and goals.


10. How do you build a Maven project from the command line?

bashCopyEditmvn clean install

This cleans previous builds and installs the new build to the local repository.


🛠️ Intermediate Maven Interview Questions and Answers

11. How does Maven resolve dependencies?

Maven checks:

  1. Local Repository (~/.m2/repository)

  2. Remote Repositories (like Maven Central or custom)

  3. Transitive Dependencies

It uses the nearest-wins strategy in case of conflicts.


12. What is the difference between compile, provided, runtime, test, and system scope?

  • compile (default): Needed for compile and runtime.

  • provided: Needed to compile, but expected to be provided at runtime (e.g., Servlet API).

  • runtime: Not needed at compile, but needed at runtime.

  • test: Used only during testing.

  • system: Similar to provided, but requires an explicit path.


13. How do you create a custom Maven plugin?

  1. Create a Java project with maven-plugin packaging.

  2. Annotate plugin classes with @Mojo.

  3. Define goals using @Parameter.

  4. Register in the POM's build/plugins section.


14. How do profiles work in Maven?

Profiles allow conditional builds based on:

  • OS

  • Environment

  • Properties

  • Active profile (via -P flag)

They are defined in pom.xml or settings.xml.


15. What is the use of the dependencyManagement tag?

Used in a parent POM to define versions of dependencies so that child projects can use them without repeating the version tag.


16. How do you override a dependency version in Maven?

  • Use dependencyManagement in a parent POM.

  • Or redefine the dependency in the child with the desired version.


17. What is the effective-pom? How can you see it?

The effective POM is the final configuration that Maven uses after combining all inherited POMs and settings.
To view:

bashCopyEditmvn help:effective-pom

18. How do you skip tests in a Maven build?

bashCopyEditmvn install -DskipTests

This compiles the test code but does not run it.
Use -Dmaven.test.skip=true to skip compilation as well.


19. What is the difference between SNAPSHOT and RELEASE versions?

  • SNAPSHOT: Mutable, can be updated with newer builds (for development).

  • RELEASE: Immutable, stable release versions.


20. What is a parent POM? How does inheritance work in Maven?

A parent POM provides shared configurations and dependencies to child modules via inheritance. Common settings go in the parent, and the child references it using:

xmlCopyEdit<parent>
  <groupId>com.example</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
</parent>

🔍 Advanced Maven Interview Questions and Answers

21. How do you troubleshoot dependency conflicts in Maven?

Use:

bashCopyEditmvn dependency:tree

This shows all dependencies and where conflicts occur. Use exclusions to resolve.


22. What is the difference between pluginManagement and plugins in a POM?

  • pluginManagement: Specifies plugin versions and configuration for child projects, but does not activate them.

  • plugins: Activates the plugin and includes it in the build.


23. What are some common issues with multi-module Maven projects?

  • Incorrect parent references

  • Dependency resolution across modules

  • Build order issues

  • Inconsistent plugin versions


24. How can Maven be integrated with CI/CD pipelines?

  • Use Maven in tools like Jenkins, GitLab CI, GitHub Actions.

  • Typical pipeline: mvn clean install → run tests → deploy artifacts


25. What is the role of the assembly plugin in Maven?

The Assembly Plugin is used to create distributable packages (ZIPs, TARs) including dependencies, configuration, and executables.


26. How do you configure Maven to use a proxy or mirror for downloading dependencies?

Edit ~/.m2/settings.xml:

xmlCopyEdit<proxies>
  <proxy>
    <id>example-proxy</id>
    <active>true</active>
    <protocol>http</protocol>
    <host>proxy.example.com</host>
    <port>8080</port>
  </proxy>
</proxies>

27. Explain how Maven Central works.

Maven Central is the default remote repository for Maven. When a dependency is not found locally, Maven tries to fetch it from Central.


28. What is the difference between mvn clean install and mvn install clean?

  • mvn clean install: Executes the clean phase first, then builds and installs.

  • mvn install clean: Executes install phase first, then tries to clean, which is illogical. Order matters.


29. How do you enforce code quality or formatting standards in a Maven build?

Use plugins:

  • maven-checkstyle-plugin

  • maven-pmd-plugin

  • spotbugs-maven-plugin

  • formatter-maven-plugin

They can be bound to lifecycle phases or run manually.


30. What strategies do you use to optimize large Maven projects?

  • Use parallel builds: mvn -T 4 clean install

  • Use dependencyManagement to avoid redundancy

  • Modularize the project properly

  • Avoid SNAPSHOT in production builds

  • Use mvn dependency:analyze to remove unused deps

0
Subscribe to my newsletter

Read articles from Vaibhav Upare directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaibhav Upare
Vaibhav Upare

"Passionate DevOps Engineer dedicated to crafting robust, scalable, and automated solutions for seamless software delivery. With expertise in cloud technologies, CI/CD pipelines, and infrastructure as code, I thrive on optimizing workflows and driving collaboration between development and operations teams. Let's build together for continuous innovation and efficiency."