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