Maven Questions

Question: What is Maven and why is it used in automation project

Answer: Maven is a build automation and dependency management tool for Java-based projects. It’s used to:
  • Manage project dependencies through a centralized pom.xml file.
  • Compile, test, package and deploy applications.
  • Integrate with CI tools like Jenkins for continuous execution.
  • Ensure standard project structure and reproducibility across teams.

Question: Explain Maven lifecycle?

Answer: Maven has 3 built-in lifecycles:
  • Default Lifecycle – Build and test the project.
  • Clean Lifecycle – Clean old build files.
  • Site Lifecycle – Generate project documentation.
1. Clean Lifecycle – Deletes the target/ folder.

Used to remove previous build artifacts.

Phases:
pre-clean → clean → post-clean

Common command:
mvn clean
2. Default Lifecycle (Most Important for Selenium)
This lifecycle is used to compile code, run tests, and package the project.

Key Phases (Interview Focus)
Phase Purpose
validate Checks project structure
compile Compiles source code
test Executes tests using the configured testing framework, such as TestNG or JUnit
package Creates JAR/WAR
verify Runs checks to verify the package meets quality criteria and test results
install Stores the artifact in the local repository
deploy Deploys the artifact to a remote repository
How lifecycle works (Very Important)

If you run:
mvn test
Maven automatically runs:
validate → compile → test
If you run:
mvn install
Maven runs:
validate → compile → test → package → verify → install
Selenium Example
mvn clean test
Steps executed:
  • Deletes old target/.
  • Compiles code.
  • Executes the configured Selenium + TestNG tests.
  • Generates reports if the project is configured to generate them.
3. Site Lifecycle
Used to generate project documentation and reports.

Phases:
pre-site → site → post-site → site-deploy
Command:
mvn site
Quick Summary (Memorize)
  • mvn clean → removes old build artifacts.
  • mvn test → runs the tests configured for the project.
  • mvn install → saves the build artifact to the local repository.
  • Maven always executes previous phases automatically within the same lifecycle.

Question: What is pom.xml and what are its key elements?

Answer: pom.xml (Project Object Model) is the core configuration file in Maven.

Important elements:
  • <dependencies> – To manage external libraries.
  • <build> – Custom build steps, plugins, and test execution control.
  • <repositories> – Define external repository URLs, such as Nexus.
  • <properties> – Project-level configuration, such as the Java version.
  • <profiles> – For managing different environments such as dev, QA, and prod.

Question: How do you manage dependencies in Maven?

Answer: Dependencies are declared inside the tag in pom.xml.

Example:
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>4.19.0</version>
</dependency>
Maven downloads these automatically from the central repository or a configured custom repository.
Question: What is the difference between compile, test, provided, and runtime scopes in Maven?

Answer:
Scope Description
compile Default scope, available in the compile, test, and runtime classpaths.
test Available only during testing.
provided Required for compilation and testing, but expected to be supplied by the runtime environment, such as the Servlet API.
runtime Required during execution and testing, but not for compilation.

Question: How do you execute tests using Maven?

Answer: Use the Surefire plugin to run unit tests and other tests configured for the Maven test phase:
mvn clean test
To run a specific TestNG suite, configure the Surefire plugin to use the suiteXmlFile property, then run:
mvn test -DsuiteXmlFile=testng.xml

Question: How can you skip test cases in Maven?

Answer: You can skip test execution using:
mvn install -DskipTests
This compiles the tests but skips running them.

To skip both test compilation and test execution:
mvn install -Dmaven.test.skip=true

Question: What is the difference between clean, install, validate, package, and verify?

Answer:
Command Description
clean Deletes the target/ directory.
validate Checks whether the project is correct and all required information is available.
compile Compiles the source code.
test Runs tests using the configured testing framework.
package Packages compiled code into a .jar or .war.
verify Runs checks to verify the package meets quality criteria.
install Installs the package into the local repository (~/.m2).

Question: How do you run TestNG XML using Maven?

Answer: Configure the Maven Surefire plugin with the TestNG suite XML file:
<configuration>
    <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
    </suiteXmlFiles>
</configuration>

Question: How do you handle version conflicts in Maven?

Answer: Use the command:
mvn dependency:tree
This shows the dependency hierarchy and helps identify dependency conflicts. Use dependency management or exclusions to resolve them:
<exclusions>
  <exclusion>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
  </exclusion>
</exclusions>

Question: What is the Surefire plugin?

Answer: Apache Surefire is a Maven plugin primarily used for executing unit tests and other tests during Maven's test phase.

For integration tests, Maven commonly uses the Failsafe plugin.
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.2.5</version>
</plugin>

Question: How do you use profiles in Maven?

Answer: Profiles are used to run Maven builds for different environments:
<profiles>
  <profile>
    <id>qa</id>
    <properties>
      <env>qa</env>
    </properties>
  </profile>
</profiles>
Activate it with:
mvn test -Pqa