Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 41 ยท Practice and Review

Maven Interview Questions

Prepare for a Maven interview: lifecycle phases, dependency scopes, mediation, dependencyManagement, SNAPSHOT versions, profiles and multi-module builds, with short answers.

Maven interviews keep returning to the same handful of ideas: what the lifecycle does, how dependencies are resolved, and how you keep a large build under control. This page collects the questions that come up most, with answers short enough to say aloud. They run from fundamentals to scenario questions.

Maven itself cannot run on this site, so every command and XML snippet below is read-only. Two runnable stand-ins, written in Java, turn the two most-asked topics (dependency mediation and lifecycle phases) into something you can run and explain. They are small models of Maven's rules, not Maven.

A tip for any interview: answer in three steps. Give the definition in one sentence, add a small example, then mention one trade-off or pitfall.

Five topics interviewers love

Lifecycle Phases run in order.
Scopes Which classpath sees a jar.
Mediation Nearest version wins.
Management One place for versions.

Know these five and most questions become variations.

1 / 2

Fundamentals

What is Maven, and what problem does it solve?

Maven is a build and dependency management tool for Java projects. You describe the project in a pom.xml (what it is, what it needs), and Maven supplies a standard way to compile, test, package and publish it. Convention over configuration means a project that follows the standard layout needs almost no build code. See What Is Maven?.

What is the POM?

The Project Object Model: an XML file that holds the project's coordinates, dependencies, plugins, properties and profiles. Every project inherits from the Super POM built into Maven, and may inherit from a parent POM too. mvn help:effective-pom prints the merged result. See The POM File.

What are the coordinates of an artifact?

groupId, artifactId and version (GAV), plus an optional packaging and classifier. Together they identify a unique artifact and map to a path in a repository: org/example/app/1.0/app-1.0.jar. See Coordinates.

What is the standard directory layout?

src/main/java and src/main/resources for production code, src/test/java and src/test/resources for tests, and target/ for everything generated. Following it means plugins find your files without configuration. See The Standard Directory Layout.

What is the Maven wrapper?

mvnw and mvnw.cmd plus a .mvn folder. The script downloads the Maven version named in the project, so every developer and CI job uses the same one without installing it. See Installing Maven and the Wrapper.

Lifecycle, phases and plugins

What are the built-in lifecycles?

Three: default (builds and deploys the project), clean (removes target/) and site (generates documentation). They are independent, so mvn clean install runs the whole clean lifecycle up to clean, then the default lifecycle up to install.

What are the main phases of the default lifecycle?

In order: validate, compile, test, package, verify, install, deploy, with many finer phases in between such as process-resources and test-compile. Running a phase runs every earlier phase first. See Lifecycles and Phases.

Java A phase runner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The loop stops at the requested phase after running everything before it. Real Maven does the same walk, but its list has more phases and its bindings depend on the packaging type.

What is the difference between a phase and a goal?

A phase is a named step in a lifecycle. A goal is a task a plugin provides, such as compiler:compile. Phases have no behavior of their own: they run whatever goals are bound to them. You can run a goal directly (mvn compiler:compile) or a phase (mvn compile). See Plugins and Goals.

What is the difference between mvn package, mvn install and mvn deploy?

package builds the JAR (or WAR) in target/. install also copies it into your local repository ~/.m2/repository, so other projects on your machine can use it. deploy uploads it to a remote repository for the team. See Publishing.

What does mvn clean install do?

It deletes target/, then runs the default lifecycle through install. Cleaning first avoids stale classes left from deleted sources. It is slower than mvn install, so people clean when something looks wrong, not on every build.

What is pluginManagement?

A block that sets default versions and configuration for plugins without activating them. Child modules then use a plugin without repeating its version. It is the plugin counterpart of dependencyManagement.

Dependencies and scopes

What are the dependency scopes?

Scopes decide the classpath

compile Default. Everywhere, passed on.
provided Needed to compile, supplied by the runtime.
runtime Not for compiling, needed to run.
test Only test code sees it.

system is deprecated and import only works in dependencyManagement.

1 / 2

compile is the default and is available on every classpath and passed to consumers. provided is used for compiling and testing but is expected from the runtime (a servlet container) and is not transitive. runtime is needed to run and test but not to compile (a JDBC driver). test is only for test code and is not transitive. system points at a local file and is deprecated. import is used only with type pom inside dependencyManagement to import a BOM. See Dependency Scopes.

What is a transitive dependency?

A dependency of your dependency. If your project needs library A and A needs library B, Maven adds B automatically. test and provided dependencies of A are not passed on, and an optional dependency is not passed on either.

How does Maven choose between two versions of the same library?

Two rules, called mediation. Nearest definition wins: the version closest to your project in the dependency tree is used. If two are equally near, the first declaration in the POM wins. A version you declare directly always beats any transitive one.

Java Nearest wins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The breadth-first walk visits shallow entries first, so commons:1.0 (depth 2) is chosen and commons:2.0 (depth 3) is omitted. That is exactly why a library can end up with an older version than the one a dependency wanted. See Transitive Dependencies and Mediation.

How do you see why a version was chosen?

mvn dependency:tree -Dverbose prints the full tree including omitted duplicates and the reason. mvn dependency:analyze lists unused declared and used undeclared dependencies. See Diagnosing Dependency Conflicts.

How do you exclude a transitive dependency?

Add an <exclusions> block with the groupId and artifactId inside the dependency that brings it in. Use it to drop a conflicting logging binding or a library you replace. Excluding without a replacement can cause ClassNotFoundException at run time.

What is the difference between dependencyManagement and dependencies?

dependencies actually adds libraries to the project's classpath. dependencyManagement only declares versions and settings for libraries; nothing is added until a project lists the dependency, and then it can leave out the version. Put it in a parent POM or BOM so every module agrees on versions. See Managing Versions.

XML
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.fasterxml.jackson</groupId>
      <artifactId>jackson-bom</artifactId>
      <version>2.18.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

What is a BOM?

A Bill of Materials: a POM that contains only a dependencyManagement section with tested, matching versions of related libraries. You import it with <scope>import</scope> and <type>pom</type>, and then every library it lists can be declared without a version.

Versions and repositories

What is a SNAPSHOT version?

A version ending in -SNAPSHOT marks work in progress. Maven treats it as mutable: it can check the remote repository for newer builds (by default once a day) and replaces the cached copy. A release version is immutable, so once published it never changes. Never release a project that depends on a SNAPSHOT.

What are the local, central and remote repositories?

The local repository is a cache on your machine (~/.m2/repository). Maven Central is the default public remote repository. Other remote repositories are company servers such as Nexus or Artifactory, often configured as a mirror in settings.xml. See Repositories and the Local Cache and settings.xml, Mirrors and Credentials.

Why avoid version ranges?

A range such as [1.0,2.0) makes the build depend on what is in the repository today, so the same commit can resolve differently next week. Builds stop being reproducible. Pin exact versions and update them on purpose, ideally with a tool.

Profiles and multi-module builds

What is a profile?

A named set of POM settings (properties, dependencies, plugin configuration) that is switched on by -P, or automatically by a property, JDK, operating system or file condition. Use profiles for real differences between environments, not to hide a second build. See Profiles and Environments.

Terminal
mvn verify -Pci
mvn verify -P!ci

What is a multi-module project?

A parent POM with packaging set to pom that lists <modules>. Running Maven in the parent builds all modules in the reactor, in an order worked out from the dependencies between them, not from the order listed. See Multi-Module Builds and the Reactor.

What is the difference between inheritance and aggregation?

Inheritance uses <parent>: the child receives the parent's properties, dependency management and plugin configuration. Aggregation uses <modules>: the parent builds the listed children together. A project can do one, the other, or both, and they do not need to be the same POM.

How do you build only one module and what it needs?

mvn -pl service -am package builds the service module and, with -am (also make), every module it depends on. -amd adds modules that depend on it, and -rf :module resumes a failed reactor build from a given module.

Scenarios

The build works on my machine but fails in CI. Where do you look?

Check the Java and Maven versions (use the wrapper and set maven.compiler.release), a stale or poisoned ~/.m2 on one side, SNAPSHOT dependencies that differ, a profile active only locally, and credentials or mirrors missing from CI's settings.xml. Run with -X and compare. See Troubleshooting Common Maven Errors.

You get NoSuchMethodError at run time. What happened?

Almost always a dependency conflict: two versions of a library were on the classpath and mediation picked the older one, which lacks the method. Run dependency:tree -Dverbose, find the library, and pin the version in dependencyManagement or exclude the older copy.

How would you speed up a slow build?

Build in parallel with -T 1C, cache ~/.m2 in CI, skip work you do not need (-DskipTests for packaging-only steps, but not in the main pipeline), build only changed modules with -pl and -am, and try the Maven daemon. See Maven in CI and Faster Builds.

Maven or Gradle?

Maven is declarative and uniform: every project looks alike, which makes it easy to learn and audit. Gradle is scripted and more flexible and has strong incremental caching, at the cost of more variation. Choose for the team's skills and the project's needs, not fashion. See Maven vs Gradle.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You run mvn package. Which phases run?

    Running a phase runs all earlier phases in the lifecycle first, and stops after the requested one.
  2. 2. Library C appears at depth 2 as version 1.0 and at depth 3 as version 2.0, and you declare neither directly. Which version does Maven use?

    Mediation uses the nearest definition in the tree, not the highest version.
  3. 3. What does a dependency listed only inside dependencyManagement do?

    dependencyManagement declares versions and settings. Only the dependencies element adds libraries to the classpath.
  4. 4. Which scope fits a servlet API that the application server supplies at run time?

    provided is available when compiling and testing but is not packaged or passed on, because the runtime supplies it.

Summary

  • Know the lifecycles and that running a phase runs all earlier phases; phases only run the goals bound to them.
  • Scopes decide which classpaths see a dependency; mediation picks the nearest version, then the first declared.
  • dependencyManagement sets versions, dependencies adds libraries; BOMs import managed versions.
  • SNAPSHOTs are mutable, releases are immutable; avoid version ranges.
  • Profiles switch settings, and multi-module builds use aggregation (modules) plus inheritance (parent), ordered by the reactor.
  • For scenarios, state the symptom, name the tool (dependency:tree, -X, help:effective-pom) and the fix.

Next, keep the commands within reach in the Maven Cheat Sheet.