Skip to content

Maven โ€” 30 Interview Questions (Answers & Examples)

Build automation + dependency management for JVM projects via a declarative POM and a standard lifecycle โ€” the backbone of most SDET test-automation frameworks.

Q1. What is Apache Maven and why is it used?

Maven is a build-automation and dependency-management tool for JVM projects that uses a declarative POM and "convention over configuration" to standardize how projects are built, tested, and packaged.

In plain words: instead of scripting every build step by hand, you declare what your project needs and Maven runs a standard lifecycle to compile, test, and package it โ€” and it downloads your libraries automatically.

mvn clean install

Q2. How does Maven differ from Ant?

Maven is declarative and lifecycle-driven (you describe the project), while Ant is imperative/procedural (you script each build step yourself).

In plain words: with Ant you write every task; with Maven you follow conventions and it fills in the rest, plus it manages dependencies out of the box.

Aspect Ant Maven
Style Imperative / procedural (script each step) Declarative (describe the project)
Build file build.xml (custom targets) pom.xml (standard POM)
Lifecycle None built-in (you define order) Standard lifecycle & phases
Dependencies Manual (or needs Ivy) Built-in via repositories
Structure You define any layout Convention over configuration

Q3. What is the POM file in Maven?

The POM (Project Object Model) is the pom.xml file at the project root that declares the project's coordinates, dependencies, plugins, and build configuration.

In plain words: it is the single source of truth Maven reads to know what your project is and how to build it.

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>test-automation</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>
</project>

Q4. What are the key elements of a POM file?

The core POM elements are modelVersion, the coordinates (groupId, artifactId, version), packaging, dependencies, properties, and build (plugins).

In plain words: coordinates uniquely name the artifact, dependencies list your libraries, and build/plugins control how it is compiled and tested.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>ui-tests</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <properties>
    <maven.compiler.release>17</maven.compiler.release>
  </properties>
  <dependencies><!-- ... --></dependencies>
  <build><plugins><!-- ... --></plugins></build>
</project>

Q5. How do you define dependencies in Maven?

Dependencies are declared inside <dependencies> using the library's groupId, artifactId, and version (its coordinates), optionally with a scope.

In plain words: you list the libraries you need by their address, and Maven fetches them and their transitive dependencies for you.

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.9.0</version>
  <scope>test</scope>
</dependency>

Q6. What is the Maven lifecycle?

A Maven lifecycle is a predefined sequence of build phases; Maven ships three: clean, default (the main build), and site (documentation).

In plain words: a lifecycle is the ordered "recipe" Maven follows โ€” running a phase runs every phase before it in that lifecycle.

mvn clean deploy   # runs clean lifecycle, then default lifecycle up to deploy

Q7. Can you explain the different build phases in Maven?

The default lifecycle runs its phases in order: validate โ†’ compile โ†’ test โ†’ package โ†’ verify โ†’ install โ†’ deploy.

In plain words: it validates the project, compiles code, runs tests, builds the artifact, verifies it, installs it locally, and finally deploys it to a remote repo โ€” each phase includes all earlier ones.

mvn package   # runs validate, compile, test, then package

Q8. How do you create a Maven project from the command line?

You generate a project with mvn archetype:generate, supplying an archetype plus the new project's coordinates.

In plain words: this scaffolds the standard folder layout and a starter pom.xml for you.

mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=my-tests \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

Q9. What are Maven repositories?

A Maven repository is a store of artifacts (JARs) and their POMs that Maven pulls dependencies from and publishes built artifacts to.

In plain words: it is the warehouse of libraries โ€” Maven looks there instead of you manually downloading JARs.

<repositories>
  <repository>
    <id>central</id>
    <url>https://repo.maven.apache.org/maven2</url>
  </repository>
</repositories>

Q10. What is the difference between local, central, and remote repositories?

Local is the ~/.m2/repository cache on your machine, central is Maven's public default repo, and remote is any other hosted repo such as a company Nexus/Artifactory.

In plain words: Maven checks your local cache first, then downloads from central or a remote repo and caches the result locally.

Repository Location Role
Local ~/.m2/repository Per-machine cache; checked first
Central Maven Central (public) Default source for open-source artifacts
Remote Company Nexus/Artifactory, etc. Internal/third-party or proxy repos

Q11. How does Maven handle dependency management?

Maven resolves declared dependencies transitively (pulling their dependencies too) and can centralize versions via <dependencyManagement> and BOM imports.

In plain words: you name a library and Maven brings everything it needs; <dependencyManagement> lets a parent POM fix versions so modules stay consistent.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.9.0</version>
    </dependency>
  </dependencies>
</dependencyManagement>

Q12. What is a Maven plugin?

A plugin is a Maven artifact that provides goals โ€” the actual units of work (like compiling or running tests) that get bound to lifecycle phases.

In plain words: the lifecycle defines when things happen; plugins provide what actually happens at each step.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.2.5</version>
</plugin>

Q13. How do you add plugins to a Maven project?

Plugins are added under <build><plugins> with their coordinates and optional <configuration> and <executions>.

In plain words: you declare the plugin just like a dependency, then configure how and when its goals run.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.13.0</version>
      <configuration>
        <release>17</release>
      </configuration>
    </plugin>
  </plugins>
</build>

Q14. What is the use of the mvn clean command?

mvn clean runs the clean lifecycle, deleting the target/ directory so the next build starts from a clean state.

In plain words: it wipes previously compiled classes and artifacts to avoid stale-build issues.

mvn clean

Q15. How do you skip tests while building a Maven project?

Use -DskipTests to compile test classes but skip running them, or -Dmaven.test.skip=true to skip compiling and running tests entirely.

In plain words: skipTests still builds the tests but does not execute them; maven.test.skip ignores tests completely โ€” faster but riskier.

mvn install -DskipTests
mvn install -Dmaven.test.skip=true

Q16. What is the mvn install command used for?

mvn install builds the project and installs the resulting artifact into your local repository (~/.m2) so other local projects can use it.

In plain words: it runs the whole build up to install and copies the JAR into your local cache โ€” unlike deploy, which pushes to a remote repo.

mvn clean install

Q17. How can you run a specific test or test suite using Maven?

Run a single test class with -Dtest=ClassName (Surefire), or point Surefire at a TestNG suite file via <suiteXmlFiles>.

In plain words: use -Dtest for quick single-class runs, or configure a testng.xml suite when you need a defined group of tests.

mvn test -Dtest=LoginTest
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.2.5</version>
  <configuration>
    <suiteXmlFiles>
      <suiteXmlFile>testng.xml</suiteXmlFile>
    </suiteXmlFiles>
  </configuration>
</plugin>

Q18. How do you manage different environments in Maven?

Use build profiles (<profiles>) activated with -P to switch configuration (URLs, credentials, properties) per environment.

In plain words: define one profile per environment (dev/qa/prod) and pick one at build time instead of hardcoding settings.

<profiles>
  <profile>
    <id>qa</id>
    <properties>
      <base.url>https://qa.example.com</base.url>
    </properties>
  </profile>
</profiles>
mvn test -Pqa

Q19. What is the settings.xml file?

settings.xml is Maven's user/global configuration file (in ~/.m2/ or the Maven conf/ dir) holding machine-specific settings like repository mirrors, server credentials, and proxies.

In plain words: it keeps environment/secret settings out of the shared pom.xml and local to each machine.

<settings>
  <servers>
    <server>
      <id>nexus</id>
      <username>ci-user</username>
      <password>${env.NEXUS_PWD}</password>
    </server>
  </servers>
</settings>

Q20. How do you configure proxy settings in Maven?

Proxy settings go in settings.xml under <proxies>, specifying host, port, and optional credentials.

In plain words: behind a corporate firewall you tell Maven which proxy to route downloads through.

<proxies>
  <proxy>
    <id>corp-proxy</id>
    <active>true</active>
    <protocol>http</protocol>
    <host>proxy.company.com</host>
    <port>8080</port>
    <nonProxyHosts>*.internal.com</nonProxyHosts>
  </proxy>
</proxies>

Q21. What is the difference between compile and provided scopes in Maven dependencies?

compile (the default) puts the dependency on all classpaths and packages it into the artifact; provided is available at compile/test time but is NOT packaged, because the runtime environment supplies it.

In plain words: use compile for libraries you ship; use provided for things the server/container already provides, like the Servlet API.

Aspect compile provided
Default? Yes No
Compile classpath Yes Yes
Test classpath Yes Yes
Runtime / packaged Yes No (supplied by environment)
Typical use App libraries you ship servlet-api, container-provided libs

Q22. How do you handle version conflicts in Maven dependencies?

Maven applies "nearest-wins" mediation; you resolve conflicts by inspecting dependency:tree, then pinning versions via <dependencyManagement> or excluding transitive dependencies with <exclusions>.

In plain words: when two paths pull different versions, force the one you want and exclude the unwanted transitive copy.

<dependency>
  <groupId>com.example</groupId>
  <artifactId>lib</artifactId>
  <version>2.0</version>
  <exclusions>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Q23. What is the use of the mvn dependency:tree command?

mvn dependency:tree prints the full dependency graph, including transitive dependencies, so you can spot version conflicts and duplicates.

In plain words: it shows exactly which libraries (and versions) your build pulls in and why.

mvn dependency:tree

Q24. How do you create a Maven archetype?

An archetype is a project template; you create one from an existing project with archetype:create-from-project, then install it and generate new projects from it.

In plain words: turn a working project into a reusable scaffold so teammates start from the same standard structure.

mvn archetype:create-from-project
cd target/generated-sources/archetype
mvn install

Q25. How can you customize the build process in Maven?

Customize the build by configuring plugins (via <configuration> and <executions>), binding extra goals to phases, and using properties and profiles.

In plain words: you tune existing plugins or attach new goals to lifecycle phases to make the build do exactly what you need.

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <executions>
    <execution>
      <id>run-smoke</id>
      <phase>integration-test</phase>
      <goals><goal>test</goal></goals>
    </execution>
  </executions>
</plugin>

Q26. What is the difference between SNAPSHOT and release versions?

A SNAPSHOT is an in-development, mutable version that can change between builds, while a release version is fixed and immutable once published.

In plain words: 1.0-SNAPSHOT is a moving target Maven re-checks for updates; 1.0 is frozen and never changes.

Aspect SNAPSHOT Release
Example 1.0.0-SNAPSHOT 1.0.0
Mutability Mutable (changes over time) Immutable (fixed)
Update check Maven re-fetches latest build Cached once, never re-fetched
Use case Active development Stable, published artifacts

Q27. How do you integrate Maven with Jenkins for CI/CD?

Jenkins runs Maven goals as a build step โ€” either via a Maven project type or a shell/pipeline step invoking mvn โ€” with a configured Maven tool and JDK.

In plain words: Jenkins checks out your repo and runs mvn clean install (or test) on each commit, then publishes reports and artifacts.

stage('Build & Test') {
  steps {
    sh 'mvn clean install'
  }
}

Q28. Can Maven be used for non-Java projects?

Yes โ€” Maven is JVM-centric but can build other languages and outputs (Scala, Kotlin, Groovy, even C/C++ or web assets) through appropriate plugins.

In plain words: while designed for Java, plugins let Maven manage builds for many project types, though it is less common outside the JVM.

<plugin>
  <groupId>net.alchim31.maven</groupId>
  <artifactId>scala-maven-plugin</artifactId>
  <version>4.9.2</version>
</plugin>

Q29. What is the difference between Maven goals and phases?

A phase is a stage in the lifecycle (like compile or test); a goal is a specific task (like compiler:compile) provided by a plugin and bound to a phase.

In plain words: phases say when, goals say what โ€” running a phase executes all goals bound to it.

Aspect Phase Goal
Definition Stage in a lifecycle Task provided by a plugin
Format Single word (compile, test) plugin:goal (surefire:test)
Invocation mvn test mvn surefire:test
Relationship Contains one or more goals Bound to a phase

Q30. How do you generate reports using Maven?

Reports are generated via reporting plugins โ€” Surefire produces test result reports under target/surefire-reports, and mvn site builds an aggregated project site.

In plain words: Surefire emits XML/text test reports each run, and the site lifecycle/plugins (or Allure) turn results into browsable HTML.

mvn test          # writes target/surefire-reports
mvn surefire-report:report
mvn site