Skip to main content

Command Palette

Search for a command to run...

Maven for Automation Testing

Updated
β€’25 min readβ€’View as Markdown

1. What is Maven?

Maven is a build automation and project management tool mainly used for Java projects.

  • It simplifies the process of compiling code, packaging it into JAR/WAR, managing dependencies (external libraries), running tests, and deploying applications.

  • It uses an XML file called pom.xml (Project Object Model) to manage everything about a project.

πŸ‘‰ Think of Maven as your project manager + builder that automatically does repetitive tasks.


2. Why Maven came?

Before Maven, developers used tools like ANT for build automation. But:

  • Dependency management was manual (you had to download and configure every JAR file yourself).

  • Project builds were not standardized (every company/team had different scripts).

  • No clear project lifecycle (compile β†’ test β†’ package β†’ deploy).

Maven solved these problems by:
βœ” Providing centralized dependency management (from Maven Central Repository).
βœ” Standardizing project structure & lifecycle.
βœ” Making projects portable (same build works anywhere).


3. When Maven came?

  • Maven was created by Apache Software Foundation.

  • First release: 2004 (successor of Apache Ant).

  • It became popular from 2005 onwards because of its dependency management system.


4. What problems does Maven solve?

βœ… Dependency Management β†’ Automatically downloads required JARs from repositories.
βœ… Standard Build Lifecycle β†’ Same commands (mvn compile, mvn test, mvn package) work for any project.
βœ… Consistent Project Structure β†’ Standard folder structure (src/main/java, src/test/java).
βœ… Multi-Module Projects β†’ Manage multiple sub-projects in one parent project.
βœ… Integration with CI/CD β†’ Works smoothly with Jenkins, GitHub Actions, etc.
βœ… Plugin Ecosystem β†’ Ready-to-use plugins for testing, reporting, deployment.


5. Where can we use Maven?

  • Java Applications (Core Java, Advanced Java).

  • Web Applications (Spring, Hibernate, JSP/Servlet, Struts).

  • Automation Frameworks (Selenium, TestNG, JUnit).

  • Microservices (Spring Boot, REST APIs).

  • Big projects with multiple modules.


6. When do we use Maven?

We use Maven when:

  • Project has dependencies (external libraries like Selenium, TestNG, Log4j).

  • You want standardized builds (compile, test, package, deploy).

  • Working in a team (so everyone follows same structure).

  • Project is integrated with CI/CD pipelines.

πŸ‘‰ Example: In Selenium Automation Projects, Maven is used to manage TestNG, WebDriver, Apache POI, Extent Reports, etc.


7. Prerequisite Knowledge for Maven

Before learning Maven, you should know:

  1. Core Java (OOPs, Exceptions, Collections).

  2. JDK & JRE setup (basic Java program execution).

  3. Eclipse/IntelliJ IDE basics.

  4. Command line basics (running commands in terminal).

  5. Basic XML understanding (because POM file is XML).

πŸ‘‰ Optional (but useful):

  • Knowledge of JUnit/TestNG (for test automation).

  • Git (for version control).

  • Jenkins (for CI/CD integration).


βœ… Summary in One Line:
Maven is a Java project management & build tool (introduced in 2004) that came to solve dependency hell and build standardization problems, used widely in Java, Selenium, Spring Boot projects, and requires basic Java + XML knowledge before starting.

πŸ”„ Maven Workflow Diagram

                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚   Developer       β”‚
                β”‚ runs Maven cmd    β”‚
                β”‚ (e.g., mvn test)  β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚     POM.xml       β”‚
                β”‚ (Project Object   β”‚
                β”‚   Model file)     β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β–Ό                   β–Ό                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Maven Local   β”‚  β”‚ Maven Central β”‚   β”‚ Other Repos     β”‚
β”‚ Repository    β”‚  β”‚ Repository    β”‚   β”‚ (Company, 3rd)  β”‚
β”‚ (~/.m2)       β”‚  β”‚ Online source β”‚   β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
   Dependencies
   downloaded once
   & stored locally
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€-┐
β”‚   Maven Lifecycle  β”‚
β”‚  (Phases/Goals)    β”‚
β”‚ compile β†’ test β†’   β”‚
β”‚ package β†’ verify β†’ β”‚
β”‚ install β†’ deploy   β”‚
└─────────┬─────────-β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€-─┐
β”‚   Build Output     β”‚
β”‚   (JAR/WAR, Test   β”‚
β”‚    Reports, etc.)  β”‚
└───────────────────-β”˜

πŸ“ Explanation of Flow

  1. You run a Maven command (e.g., mvn clean install).

  2. Maven checks the POM.xml to see project details (dependencies, plugins, version).

  3. Maven looks for required dependencies:

    • First in Local Repository (~/.m2 folder).

    • If not found, it downloads from Maven Central Repository (online).

    • Or from Custom Company Repository (like Nexus/Artifactory).

  4. Dependencies are cached locally so they don’t download again.

  5. Maven executes build lifecycle phases in order:

    • validate β†’ compile β†’ test β†’ package β†’ verify β†’ install β†’ deploy.
  6. Finally, Maven produces output (like .jar or .war file, test reports, logs).


πŸ‘‰ This is why Maven is powerful:

  • You only need a POM.xml.

  • Maven does everything automatically (download, compile, test, build).

πŸ”‘ Maven Components and Their Work


1. POM (Project Object Model)

  • What it is:
    An XML file (pom.xml) at the root of every Maven project.

  • What it does:

    • Defines project information (name, version, packaging type).

    • Lists dependencies (external libraries).

    • Declares plugins (e.g., for compiler, surefire for test execution).

    • Manages build configurations.

    • Supports parent–child hierarchy for multi-module projects.

πŸ‘‰ Without POM.xml, Maven cannot work.


2. Repositories

Maven stores and fetches dependencies (JAR files) from repositories.

  • Types of Repositories:

    1. Local Repository

      • A folder in your machine (~/.m2/repository).

      • Stores downloaded dependencies for reuse (so you don’t re-download every time).

    2. Central Repository

      • Maintained by Apache (https://repo.maven.apache.org/maven2).

      • Default online source for dependencies.

      • Contains thousands of open-source libraries (Selenium, TestNG, JUnit, etc.).

    3. Remote/Private Repository

      • Used in organizations (e.g., Nexus, Artifactory, JFrog).

      • Stores custom/company-specific JARs.


3. Dependencies

  • What it is: External libraries (like Selenium, MySQL connector, TestNG).

  • What it does:

    • Defined inside pom.xml.

    • Maven automatically downloads & manages versions.

    • Avoids β€œJAR Hell” (manual download and conflicts).

πŸ‘‰ Example:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>4.23.0</version>
</dependency>

4. Maven Lifecycle

Maven has three built-in lifecycles:

  1. Clean Lifecycle – Cleans project (mvn clean).

    • Deletes target/ folder (previous builds).
  2. Default Lifecycle – Main build process.

    • Phases: validate β†’ compile β†’ test β†’ package β†’ verify β†’ install β†’ deploy
  3. Site Lifecycle – Generates project reports/documentation (mvn site).

πŸ‘‰ Each lifecycle has phases, and each phase has goals (actual tasks).


5. Plugins

  • What it is: Small tools that extend Maven’s functionality.

  • What it does:

    • Each plugin performs a task in a build lifecycle.

    • Example plugins:

      • Compiler Plugin β†’ Compiles Java code.

      • Surefire Plugin β†’ Runs unit tests (JUnit/TestNG).

      • Shade Plugin β†’ Creates a fat JAR with dependencies.

πŸ‘‰ Example (in pom.xml):

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
      <configuration>
        <source>17</source>
        <target>17</target>
      </configuration>
    </plugin>
  </plugins>
</build>

6. Build Profiles

  • What it is: Custom build configurations in pom.xml.

  • What it does:

    • Helps create different builds (e.g., dev, test, prod).

    • You can activate a profile with:

        mvn clean install -Pdev
      

πŸ‘‰ Example:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <env>development</env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <env>production</env>
    </properties>
  </profile>
</profiles>

7. Archetypes

  • What it is: Project templates provided by Maven.

  • What it does:

    • Helps quickly generate a standard project structure.

    • Example:

        mvn archetype:generate
      
    • Creates folders like src/main/java, src/test/java, etc.


8. Super POM

  • What it is: The default parent POM provided by Maven.

  • What it does:

    • Contains default configurations (compiler, reporting, plugins).

    • All projects inherit from it (if you don’t define something in pom.xml, Maven uses defaults from Super POM).


9. Effective POM

  • What it is: Final POM after merging:
    Super POM + Parent POM + Your POM.xml.

  • What it does:

    • Shows actual settings Maven will use.

    • You can see it with:

        mvn help:effective-pom
      

🎯 Quick Summary Table

ComponentRole / Work
POM.xmlCentral configuration file (dependencies, plugins, builds).
RepositoriesSource of dependencies (Local, Central, Remote).
DependenciesExternal libraries managed automatically.
LifecycleDefines phases of build (clean, compile, test, package, deploy).
PluginsExecute specific tasks (compile, test, package).
ProfilesDifferent build environments (dev, prod, test).
ArchetypesTemplates for creating new projects.
Super POMDefault parent POM with built-in configs.
Effective POMFinal merged POM (Super + Parent + Current).

🎯Topic-wise task plan for grip on maven

below is a linear, topic-wise task plan that will make you actually do every important Maven concept used in real-world Automation Testing (Selenium + TestNG). Follow tasks in order. Each task has: Goal β†’ Exact commands / snippets β†’ How to verify β†’ Short explanation / tip. Do them one-by-one β€” this will give you practical mastery.

1) Prerequisites & quick checks

Goal: confirm environment is ready.
Commands:

java -version
mvn -v
git --version

Verify: each prints a version and no errors.
Tip: Maven needs JDK; JAVA_HOME must be set.


2) Create a starter project (archetype)

Goal: scaffold a Maven project skeleton.
Command:

mvn archetype:generate -DgroupId=com.practice.automation -DartifactId=selenium-maven \
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd selenium-maven

Verify: pom.xml, src/main/java, src/test/java exist.
Why: gives a basic pom to extend.


3) Basic build lifecycle run

Goal: run the default lifecycle and inspect outputs.
Commands:

mvn clean
mvn compile
mvn test
mvn package

Verify: target/ created, compiled classes in target/classes, JAR in target/.
Why: see how phases map to file system results.


4) Add core automation dependencies

Goal: modify pom.xml to add Selenium, TestNG, WebDriverManager.
Snippet (inside <dependencies>):

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>4.23.0</version>
</dependency>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.10.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.github.bonigarcia</groupId>
  <artifactId>webdrivermanager</artifactId>
  <version>5.9.2</version>
</dependency>

Command:

mvn clean test

Verify: Maven downloads dependencies into ~/.m2/repository and tests run.
Tip: inspect ~/.m2/repository/org/seleniumhq/selenium-java.


5) Understand dependency scopes & transitive deps

Task A β€” scopes:

  • Add a small example dependency with <scope>provided</scope> and run mvn test β†’ observe it isn’t on runtime classpath.
    Task B β€” transitive:

  • Add a dependency that brings transitive ones, then run:

mvn dependency:tree

Verify: see full tree and identify transitive nodes.
Why: you must be able to read the tree and reason about conflicts.


6) Resolve dependency conflicts (practice)

Goal: simulate conflicting transitive versions and fix them.
Steps:

  1. Add two dependencies that depend on different versions of the same artifact (e.g., add two libraries known to cause conflict or simulate by adding a direct dependency with older version).

  2. Run:

mvn dependency:tree
  1. Fix by:

    • adding <exclusion> in the dependency that brings unwanted version, or

    • adding the correct version in <dependencyManagement> of parent POM.
      Verify: dependency:tree shows intended version only.


7) Use dependencyManagement in a parent POM

Goal: centralize versions for multi-module projects.
Steps:

  1. Create a parent pom.xml with <packaging>pom</packaging> and <dependencyManagement> listing versions.

  2. Create child modules and use dependencies without versions.
    Command:

mvn -pl child-module clean test

Verify: child builds use versions from parent.
Why: makes version control easier in enterprise projects.


8) Effective POM & Super POM

Goal: learn how defaults are inherited.
Command:

mvn help:effective-pom

Verify: examine merged config (Super POM + your POM).
Why: helps debug default plugin versions & behaviors.


9) Configure Surefire + TestNG suite + run selective tests

Steps:

  1. Add a testng.xml in project root containing your suite and parallel settings.

  2. Configure maven-surefire-plugin in pom.xml to point to the suite.

  3. Run:

mvn -Dtest=MyTest#myMethod test

Verify: only that method/class runs; surefire reports appear in target/surefire-reports.
Tip: learn -Dtest, -Dgroups, and surefire config.


10) Headless runs via system property

Goal: pass headless flag to tests dynamically.
Code pattern (in your test base):

boolean headless = Boolean.getBoolean("headless"); // default is false
if(headless) options.addArguments("--headless=new");

Run with:

mvn clean test -Dheadless=true

Verify: browsers start in headless mode; reuse -Dheadless=false to run visibly.


11) Make WebDriver Thread-safe (ThreadLocal)

Goal: enable proper parallel runs.
Task:

  • Implement a DriverFactory using ThreadLocal<WebDriver> and use it in @Before/@After.
    Verify: parallel tests do not share drivers and do not interfere.

12) Parallel execution (TestNG + Maven)

Goal: run tests in parallel and observe behavior.
Approach:

  • Configure testng.xml with parallel="methods" or parallel="tests" and set thread-count.

  • Or configure <parallel> and <threadCount> in Surefire <configuration>.
    Run:

mvn clean test -Dheadless=true

Verify: multiple sessions run concurrently; monitor CPU and logs.


13) Reporting: Surefire, ExtentReports, Allure

Tasks:
A. Surefire default: run tests and inspect target/surefire-reports/*.xml and *.txt.
B. ExtentReports: add dependency, create Extent init in @BeforeSuite, log in tests, flush in @AfterSuite; open target/extent-report.html.
C. Allure: add allure-testng dependency and allure-maven plugin. After mvn clean test run:

allure serve target/allure-results

Verify: reports show steps, screenshots (if added), attachments.


14) Add screenshots on failure and attach to reports

Task:

  • Capture screenshot in @AfterMethod on failure and save to target/screenshots.

  • Attach file path to Extent or Allure result.
    Verify: screenshot visible in report when test fails.


15) Create multi-module project (Parent + modules)

Goal: split utilities vs tests into modules.
Steps:

  1. Parent POM with <modules> listing automation-core and automation-tests.

  2. automation-core contains DriverFactory, BaseTest, utilities.

  3. automation-tests depends on automation-core.
    Commands:

mvn clean install   # from parent

Verify: parent builds children; artifacts installed in local repo.


16) Packaging and fat-jar (shade)

Goal: create a runnable fat JAR if needed for distribution.
Add maven-shade-plugin config:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.4.1</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals><goal>shade</goal></goals>
    </execution>
  </executions>
</plugin>

Run:

mvn clean package

Verify: target/<artifact>-shaded.jar created and includes dependencies.


17) Snapshot vs Release & deploy (local β†’ remote)

Task A β€” SNAPSHOT:

  • Set version to 1.0-SNAPSHOT. Build & mvn install β†’ check local repo path.
    Task B β€” release:

  • Change to 1.0 and use maven-release-plugin or manually set version and mvn deploy.
    Task C β€” set up distributionManagement for remote repo (Nexus/Artifactory) and deploy:

<distributionManagement>
  <repository><id>company-repo</id><url>http://nexus.company/repo/releases</url></repository>
  <snapshotRepository><id>company-snapshots</id><url>http://nexus.company/repo/snapshots</url></snapshotRepository>
</distributionManagement>

Verify: artifacts appear in Nexus or local ~/.m2.


18) Setup & use settings.xml (mirrors, servers, credentials)

Task:

  • Create/edit ~/.m2/settings.xml with <mirrors> pointing to Nexus and <servers> with credentials for deploy.
    Example snippet:
<servers>
  <server>
    <id>company-repo</id>
    <username>deploy_user</username>
    <password>******</password>
  </server>
</servers>

Verify: mvn deploy authenticates & uploads artifact.


19) Offline builds & cache

Task:

  • Populate ~/.m2 by running an online build.

  • Simulate offline build:

mvn -o clean package

Verify: build succeeds when all deps present locally; fails if missing.
Tip: practice how to prepare an agent to run offline.


20) Force update & debugging

Commands to practice:

  • Force update dependencies from remotes:
mvn clean install -U
  • Debug a failing build:
mvn -X test
mvn -e test

Verify: logs show verbose stack and dependency resolution details.


21) Maven profiles (dev/test/prod)

Task:

  1. Add <profiles> to pom.xml with different properties (e.g., baseUrl, browser, headless).

  2. Run:

mvn test -Pdev
mvn test -Pprod -Dheadless=true

Verify: different profiles change behavior (URLs, timeouts).


22) Archetypes β€” create a reusable template

Task:

  • Create a custom archetype of your framework skeleton and generate a new project from it:
mvn archetype:create-from-project
cd target/generated-sources/archetype
mvn install
mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=...

Verify: new project is created using your template.


23) Generate site & project reports

Commands:

mvn site
mvn site:run

Verify: local site with project reports opens (dependencies, surefire summary, javadocs if added).


24) Integration with Jenkins (CI)

Task:

  • Create a Jenkins Pipeline (Jenkinsfile) that:

    • checks out code

    • uses specific JDK & Maven

    • runs mvn clean test -Dheadless=true -U

    • archives artifacts and publishes reports (junit/allure/html)

  • Configure Jenkins tools & settings.xml for Nexus.
    Verify: pipeline runs, test results appear in Jenkins, artifacts published.


25) Fix missing dependencies on Jenkins agents

Troubleshoot steps:

  1. Run mvn -U on agent to force download.

  2. Check network/proxy; ensure agent can reach Nexus/Maven Central.

  3. Place a proper settings.xml on Jenkins with mirror-server and credentials.
    Verify: build no longer fails due to missing deps.


26) Troubleshooting exercises (do these deliberately)

A. Delete a dependency folder from ~/.m2 and run mvn -o β†’ observe failure.
B. Run mvn dependency:analyze and act on unused/undeclared dependencies.
C. Simulate corrupted artifact in ~/.m2 (e.g., remove jar size) β†’ run mvn -U to fix.


27) Advanced: write a tiny Maven plugin (optional)

Goal: understand plugin mechanism & Plexus.
Steps:

  1. Create new project with packaging = maven-plugin.

  2. Add a Mojo (Java class annotated with @Mojo) that prints a message or performs a simple file copy.

  3. mvn install the plugin and then use it in another project’s pom.xml under <plugins>.
    Verify: plugin goal runs during the lifecycle and your message appears in build logs.
    Why: helps you understand how Maven delegates to plugins.


28) Best-practice checklist (final)

Make sure you can:

  • Explain lifecycle vs phase vs goal.

  • Read and fix dependency:tree.

  • Use dependencyManagement and parent POM.

  • Configure surefire and TestNG suites.

  • Build parallel, thread-safe tests with ThreadLocal drivers.

  • Generate Extent/Allure reports and attach screenshots.

  • Use settings.xml, distributionManagement and perform deploy to a remote repo.

  • Debug builds with -X and fix Jenkins dependency issues.

  • Create multi-module project and manage versions centrally.


29) Self-test mini-project (put it all together)

Goal: create a mini real-world repo that demonstrates everything.
Requirements:

  • Parent POM with dependencyManagement.

  • core module (DriverFactory, utilities).

  • tests module (several TestNG classes, TestNG xml parallel).

  • ExtentReports and screenshot-on-failure.

  • Jenkinsfile for CI to run mvn clean test -Dheadless=true -U and publish reports.

  • Deploy the produced artifact to a local Nexus or simulate by copying into a file-repo configured in distributionManagement.
    Verify: CI runs green, artifact appears in Nexus, reports accessible, and you can run offline using mvn -o after pre-caching.

πŸ“Œ Maven Interview Questions for Automation Testers


πŸ”Ή Basic Level (Fundamentals)

  1. What is Maven? Why do we use it in automation testing?

  2. Difference between Maven and Ant/Gradle?

  3. What is pom.xml in Maven?

  4. What is a Maven project structure (src/main/java, src/test/java, target)?

  5. What are Maven lifecycles? Name them.

  6. What are Maven build phases? (e.g., clean, compile, test, package, install, deploy)

  7. Difference between Maven build lifecycle, phase, and goal.

  8. What is the default lifecycle in Maven?

  9. How do you run Maven commands from the command line?

  10. Where is Maven installed? How do you check Maven version?


πŸ”Ή Dependency Management

  1. What are Maven dependencies? How are they managed in automation projects?

  2. What happens if two dependencies have the same transitive dependency but with different versions?

  3. What is dependency scope in Maven? (compile, test, provided, runtime, system, import)

  4. Explain dependency hierarchy and conflict resolution.

  5. How to check the complete dependency tree in Maven? (mvn dependency:tree)

  6. What is transitive dependency in Maven?

  7. What is the difference between compile scope and test scope dependency?

  8. Where does Maven store downloaded dependencies?


πŸ”Ή Repositories

  1. What are the types of Maven repositories?

  2. What is the Maven Central Repository?

  3. What is a local repository in Maven?

  4. What is a remote/private repository? (e.g., Nexus, JFrog Artifactory)

  5. How does Maven resolve dependencies (local β†’ central β†’ remote)?


πŸ”Ή Plugins

  1. What are Maven plugins? Why are they used?

  2. What is the Maven Surefire plugin? (used in TestNG/JUnit test execution)

  3. What is the Maven Compiler plugin?

  4. What is the Maven Clean plugin?

  5. What is the Maven Install plugin?

  6. Which plugins do you use in automation testing frameworks?


πŸ”Ή POM (Project Object Model)

  1. What is POM.xml?

  2. What are the important tags in pom.xml? (groupId, artifactId, version, dependencies, build)

  3. What is the difference between Super POM, Parent POM, and Effective POM?

  4. How do you see the effective POM? (mvn help:effective-pom)

  5. How do you manage multiple modules in Maven? (multi-module project)

  6. What is a Maven parent-child project?


πŸ”Ή Profiles

  1. What are Maven build profiles?

  2. How do you create and activate a Maven profile?

  3. How do you run a Maven command with a profile? (mvn clean install -Pdev)


πŸ”Ή Integration with Automation Testing

  1. Why is Maven used in Selenium/TestNG frameworks?

  2. How do you add TestNG dependency in Maven?

  3. How do you execute TestNG XML files using Maven?

  4. How do you generate reports using Maven (Surefire / ExtentReports)?

  5. How do you run Selenium automation scripts using Maven from Jenkins?

  6. What command do you give in Jenkins to trigger Maven build?

  7. Can we run specific test classes or methods using Maven? (Yes β†’ -Dtest=ClassName#methodName)

  8. What is the role of Maven in CI/CD pipelines?


πŸ”Ή Command Line Usage

  1. What is the difference between:
  • mvn clean

  • mvn compile

  • mvn test

  • mvn package

  • mvn install

  • mvn deploy

  1. How do you skip tests in Maven? (-DskipTests or -Dmaven.test.skip=true)

  2. How do you run only failed tests in Maven? (mvn -rf :module-name)

  3. How do you run tests in parallel using Maven?


πŸ”Ή Advanced / Troubleshooting

  1. What is dependency conflict? How do you resolve it in Maven?

  2. What is the difference between SNAPSHOT and RELEASE in Maven?

  3. How do you force Maven to update dependencies? (mvn clean install -U)

  4. What happens when your internet is down β€” will Maven still build?

  5. How do you handle versioning in Maven?

  6. How do you create a Maven project from scratch using Archetypes? (mvn archetype:generate)

  7. How do you integrate Maven with Jenkins for continuous testing?

  8. What is the difference between Maven Build Tool and Maven Project Management?

  9. Have you ever written your own Maven plugin? (rare, but sometimes asked).

  10. What are some alternatives to Maven? (Gradle, Ant)


🎯 Special Maven Questions for Automation Testers

  • Which dependencies do you commonly add for a Selenium/TestNG Maven project?

  • How do you run Selenium tests in headless mode via Maven?

  • How do you integrate Maven with reporting tools (ExtentReports, Allure)?

  • How does Maven handle parallel execution of Selenium tests?

  • If your Jenkins pipeline is failing due to missing Maven dependencies, how do you fix it?

πŸ“Œ Maven Interview Questions & Answers (Automation Testing)


πŸ”Ή Basic Level

Q1. What is Maven? Why do we use it in automation testing?
πŸ‘‰ Maven is a build automation and project management tool mainly for Java projects.

  • In automation testing, it helps:

    • Manage dependencies (Selenium, TestNG, Apache POI, etc.).

    • Standardize project structure.

    • Automate build, execution, and reporting.

    • Integrate with Jenkins for CI/CD.


Q2. Difference between Maven and Ant/Gradle?

  • Ant: No lifecycle, manual build scripts, no dependency management.

  • Maven: Lifecycle phases, XML (pom.xml), dependency management.

  • Gradle: Uses DSL (Groovy/Kotlin), faster, more flexible.


Q3. What is pom.xml in Maven?
πŸ‘‰ pom.xml = Project Object Model file.

  • Defines project structure, dependencies, plugins, build settings.

  • Example:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>automation</artifactId>
  <version>1.0</version>
</project>

Q4. What is Maven project structure?

project-name/
 └─ src/
     β”œβ”€ main/java        β†’ application code
     β”œβ”€ test/java        β†’ test code
 └─ target/              β†’ compiled output (JAR/WAR, reports)
 └─ pom.xml              β†’ Maven config file

Q5. What are Maven lifecycles?

  • Clean β†’ cleans old files.

  • Default β†’ compiles, tests, packages, installs, deploys.

  • Site β†’ generates documentation.


Q6. What are Maven build phases?
Common phases in default lifecycle:

  • validate β†’ compile β†’ test β†’ package β†’ verify β†’ install β†’ deploy.

Q7. Difference between lifecycle, phase, and goal?

  • Lifecycle: Sequence of build steps (default, clean, site).

  • Phase: Step inside a lifecycle (compile, test, package).

  • Goal: Actual task executed by a plugin (e.g., compiler:compile).


Q8. Default lifecycle in Maven?
πŸ‘‰ default lifecycle (build lifecycle).


Q9. How do you run Maven commands?

  • mvn clean

  • mvn compile

  • mvn test

  • mvn package

  • mvn install

  • mvn deploy


Q10. How do you check Maven version?

mvn -v

πŸ”Ή Dependency Management

Q11. What are Maven dependencies?
πŸ‘‰ External libraries required for the project. Declared in pom.xml.


Q12. What if two dependencies need the same transitive dependency with different versions?
πŸ‘‰ Maven follows β€œnearest wins” strategy in dependency tree.

  • The dependency closest to your project POM is used.

Q13. What is dependency scope?

  • compile (default) β†’ available everywhere.

  • test β†’ only during test phase.

  • provided β†’ provided by container (e.g., servlet API in Tomcat).

  • runtime β†’ needed only at runtime (e.g., JDBC driver).

  • system β†’ local JAR outside repo.

  • import β†’ used in dependency management.


Q14. What is dependency hierarchy?
πŸ‘‰ If dependencies bring their own dependencies (transitive), Maven builds a tree and resolves conflicts.


Q15. How to check dependency tree?

mvn dependency:tree

Q16. What is transitive dependency?
πŸ‘‰ Dependencies brought by your direct dependencies.


Q17. Difference between compile and test scope?

  • compile: Used in production & tests.

  • test: Only available to test classes.


Q18. Where does Maven store downloaded dependencies?
πŸ‘‰ Local repo: ~/.m2/repository.


πŸ”Ή Repositories

Q19. Types of Maven repositories?

  • Local β†’ your machine (~/.m2).

  • Central β†’ global repo maintained by Apache.

  • Remote/Private β†’ company repos (Nexus, Artifactory).


Q20. What is Maven Central Repository?
πŸ‘‰ Default global repo with thousands of open-source libraries.


Q21. What is a local repository?
πŸ‘‰ Cache on your machine (~/.m2/repository).


Q22. What is a remote/private repo?
πŸ‘‰ Hosted inside companies (e.g., Nexus, Artifactory) for internal JARs.


Q23. How Maven resolves dependencies?
πŸ‘‰ Order: Local repo β†’ Central repo β†’ Remote repo.


πŸ”Ή Plugins

Q24. What are Maven plugins?
πŸ‘‰ Small tools that perform tasks in build lifecycle.


Q25. What is Maven Surefire plugin?
πŸ‘‰ Runs tests (JUnit/TestNG). Generates test reports.


Q26. What is Maven Compiler plugin?
πŸ‘‰ Compiles source code.

<plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
   <source>17</source>
   <target>17</target>
 </configuration>
</plugin>

Q27. What is Maven Clean plugin?
πŸ‘‰ Deletes target/ folder.


Q28. What is Maven Install plugin?
πŸ‘‰ Copies built artifact into local repo (~/.m2).


Q29. Which plugins are used in automation testing frameworks?

  • Compiler plugin.

  • Surefire plugin.

  • Failsafe plugin (integration tests).

  • Shade plugin (fat JAR).


πŸ”Ή POM

Q30. What is POM.xml?
πŸ‘‰ Central config file for Maven.


Q31. Important tags in pom.xml?

  • <groupId> β†’ company/project id.

  • <artifactId> β†’ project/module name.

  • <version> β†’ version of artifact.

  • <dependencies> β†’ libraries.

  • <build> β†’ plugins and configurations.


Q32. Difference between Super POM, Parent POM, Effective POM?

  • Super POM: Default parent provided by Maven.

  • Parent POM: Custom parent defined in your org/project.

  • Effective POM: Combination of all POMs applied.


Q33. How to see effective POM?

mvn help:effective-pom

Q34. Multi-module projects in Maven?
πŸ‘‰ Parent POM + child modules defined under <modules>.


Q35. What is parent-child project?
πŸ‘‰ Child inherits configuration from parent POM.


πŸ”Ή Profiles

Q36. What are Maven build profiles?
πŸ‘‰ Custom build configs for different environments (dev, test, prod).


Q37. How do you create and activate a profile?

<profiles>
  <profile>
    <id>dev</id>
    <properties><env>dev</env></properties>
  </profile>
</profiles>

Activate:

mvn clean install -Pdev

πŸ”Ή Integration with Automation Testing

Q39. Why is Maven used in Selenium/TestNG frameworks?
πŸ‘‰ To manage dependencies, run tests, generate reports, integrate with Jenkins.


Q40. How do you add TestNG dependency in Maven?

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

Q41. How do you execute TestNG XML files with Maven?

  • Add in pom.xml β†’ Surefire plugin:
<configuration>
  <suiteXmlFiles>
    <suiteXmlFile>testng.xml</suiteXmlFile>
  </suiteXmlFiles>
</configuration>

Run:

mvn test

Q42. How do you generate reports in Maven?

  • Surefire plugin β†’ default HTML reports in target/surefire-reports.

  • ExtentReports/Allure β†’ add plugins/dependencies.


Q43. How do you run Selenium automation scripts from Jenkins?

  • In Jenkins β†’ add Maven build step β†’ enter clean test.

Q44. What command in Jenkins to trigger Maven build?

clean test

(or clean install -DsuiteXmlFile=testng.xml)


Q45. Can we run specific test classes/methods using Maven?
Yes:

mvn -Dtest=ClassName#methodName test

Q46. Role of Maven in CI/CD?
πŸ‘‰ Standardizes builds, manages dependencies, integrates with Jenkins/GitHub Actions, generates test reports.


πŸ”Ή Command Line Usage

Q47. Difference between commands?

  • clean β†’ delete target/.

  • compile β†’ compile main code.

  • test β†’ run tests.

  • package β†’ create JAR/WAR.

  • install β†’ put in local repo.

  • deploy β†’ put in remote repo.


Q48. How to skip tests?

mvn install -DskipTests

(or)

mvn install -Dmaven.test.skip=true

Q49. Run only failed tests?

mvn surefire:test

(or) rerun with -rf :module-name.


Q50. Run tests in parallel?
Configure in pom.xml β†’ Surefire plugin:

<parallel>methods</parallel>
<threadCount>4</threadCount>

πŸ”Ή Advanced

Q51. What is dependency conflict? How to resolve?
πŸ‘‰ When 2 versions of the same library exist.

  • Resolve by specifying version explicitly in pom.xml.

Q52. Difference between SNAPSHOT and RELEASE?

  • SNAPSHOT: Latest dev build (can change anytime).

  • RELEASE: Fixed stable version.


Q53. How to force Maven update dependencies?

mvn clean install -U

Q54. Will Maven build without internet?
πŸ‘‰ Yes, if all dependencies are already in local repo (~/.m2).


Q55. How to handle versioning in Maven?
πŸ‘‰ Use <dependencyManagement> in parent POM.


Q56. How to create Maven project from scratch?

mvn archetype:generate

Q57. How to integrate Maven with Jenkins?

  • Install Maven plugin in Jenkins.

  • Add Maven build step with goals (clean test).

  • Jenkins triggers build using mvn command.


Q58. Difference: Maven Build Tool vs Project Management?

  • Build tool: Compiles, packages, tests.

  • Project management: Handles dependencies, lifecycle, reporting.


Q59. Written your own plugin?
πŸ‘‰ Usually no (not common in testing). But you can extend Maven with custom plugins.


Q60. Alternatives to Maven?

  • Gradle (faster, flexible).

  • Ant (older, manual).


⚑ Automation-Specific Add-ons

  • Maven is used to manage Selenium, TestNG, ExtentReports dependencies.

  • Integrates with Jenkins for CI.

  • Generates test reports automatically.

🎯 Special Maven Questions & Answer for Automation Testers

Q1. Which dependencies do you commonly add for a Selenium/TestNG Maven project?

πŸ‘‰ The most common dependencies:

<dependencies>
   <!-- Selenium Java -->
   <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>selenium-java</artifactId>
     <version>4.23.0</version>
   </dependency>

   <!-- TestNG -->
   <dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
     <version>7.10.0</version>
     <scope>test</scope>
   </dependency>

   <!-- WebDriverManager (for driver binaries) -->
   <dependency>
     <groupId>io.github.bonigarcia</groupId>
     <artifactId>webdrivermanager</artifactId>
     <version>5.9.2</version>
   </dependency>

   <!-- Logging (optional) -->
   <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-simple</artifactId>
     <version>2.0.16</version>
   </dependency>
</dependencies>

πŸ‘‰ Add reporting libraries (ExtentReports / Allure) if needed.


Q2. How do you run Selenium tests in headless mode via Maven?

  1. In your test code, configure headless mode:
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new"); // for Chrome 109+
options.addArguments("--disable-gpu");
WebDriver driver = new ChromeDriver(options);
  1. Trigger Maven test run:
mvn clean test
  1. To control headless dynamically, pass system property:
if (Boolean.getBoolean("headless")) {
    options.addArguments("--headless=new");
}

Run via Maven:

mvn clean test -Dheadless=true

Q3. How do you integrate Maven with reporting tools (ExtentReports, Allure)?

ExtentReports

  • Add dependency:
<dependency>
  <groupId>com.aventstack</groupId>
  <artifactId>extentreports</artifactId>
  <version>5.1.1</version>
</dependency>
  • Create a listener/test base to log results.

  • Reports are generated in test-output/ or custom folder.


Allure Reports

  1. Add dependency:
<dependency>
  <groupId>io.qameta.allure</groupId>
  <artifactId>allure-testng</artifactId>
  <version>2.29.0</version>
</dependency>
  1. Add Maven plugin:
<plugin>
  <groupId>io.qameta.allure</groupId>
  <artifactId>allure-maven</artifactId>
  <version>2.12.0</version>
</plugin>
  1. Run tests:
mvn clean test
  1. Generate report:
allure serve target/allure-results

Q4. How does Maven handle parallel execution of Selenium tests?

πŸ‘‰ Parallel execution is controlled via TestNG + Surefire plugin in Maven.

  • In pom.xml:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.1.2</version>
  <configuration>
    <suiteXmlFiles>
      <suiteXmlFile>testng.xml</suiteXmlFile>
    </suiteXmlFiles>
    <parallel>methods</parallel>
    <threadCount>4</threadCount>
  </configuration>
</plugin>
  • Or configure inside testng.xml:
<suite name="Suite" parallel="tests" thread-count="4">
   <test name="Test1">...</test>
   <test name="Test2">...</test>
</suite>

πŸ‘‰ Run with Maven:

mvn clean test

Q5. If your Jenkins pipeline is failing due to missing Maven dependencies, how do you fix it?

πŸ‘‰ Common fixes:

  1. Force Maven to update dependencies:
mvn clean install -U
  1. Check .m2 repo on Jenkins server – sometimes dependencies are missing/corrupted.

    • Delete ~/.m2/repository on Jenkins node β†’ rerun build.
  2. Add remote repository in pom.xml if dependency is not in Maven Central:

<repositories>
  <repository>
    <id>my-company-repo</id>
    <url>http://nexus.company.com/repository/maven-public/</url>
  </repository>
</repositories>
  1. Check internet/firewall issues – Jenkins agent must access Maven Central or company Nexus.

  2. Use Jenkins Maven tool configuration – add Maven version in Jenkins Global Tools and link it in pipeline.


⚑ Pro tip: In interviews, if they ask this last one β†’ say you’ll first force dependency update, then check repo configuration, then Jenkins Maven settings. That shows systematic troubleshooting.

More from this blog

Shreenibas Samal's blog

49 posts