-->

Featured

DSA Interview Question

Question: Various S orting algorithms Answer: There are various sorting algorithms, each with its own advantages and disadvantages in terms ...

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: 

Types of Maven Lifecycles

Maven has 3 built-in lifecycles:

1️⃣ Default Lifecycle – Build & test the project
2️⃣ Clean Lifecycle – Clean old build files
3️⃣ 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)

PhasePurpose
validateChecks project structure
compileCompiles source code
testExecutes TestNG/JUnit tests
packageCreates JAR/WAR
verifyVerifies integration tests
installStores artifact in local repo
deployDeploys to remote repo

🔹 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:

  1. Deletes old target/

  2. Compiles code

  3. Executes Selenium + TestNG tests

  4. Generates reports


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

  • mvn test → runs Selenium tests

  • mvn install → saves build to local repo

  • Maven always executes previous phases automatically




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, test execution control.

  • <repositories> – Define external repo URLs (e.g., Nexus).

  • <properties> – Project-level config (e.g., Java version).

  • <profiles> – For managing different environments (dev, QA, prod).


Question: How do you manage dependencies in Maven?
Answer:Dependencies are declared inside the <dependencies> 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 custom one


Question: What is the difference between compile, test, provided, and runtime scopes in Maven?
Answer: 
Scope Description
compile -- Default scope, available in all classpaths.
test -- Available only during testing.
provided -- Required for compile, but not at runtime (e.g., servlet API).
runtime -- Required only during execution, not compilation.

Question: How do you execute tests using Maven?
Answer:Use the Surefire plugin to run tests:

mvn clean test

To run a specific test suite:
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 tests but skips running them.

To skip compilation and execution:

mvn install -Dmaven.test.skip=true


Question: What is the difference between clean, install, validate, package, and verify?
Answer:
Command Description
clean -- Deletes target/ directory.
validate -- Checks project is correct and all needed info is available.
compile -- Compiles the source code.
test -- Runs tests using testing framework.
package -- Packages compiled code into a .jar or .war.
verify -- Runs checks on test results.
install -- Installs the package to local repo (~/.m2).

Question: How do you run TestNG XML using Maven?
Answer: 
<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 highlights 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 used for executing unit and integration tests.

<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 -P qa

No comments:

Post a Comment

popular posts