What is Maven
Definition
Maven is a build automation and dependency management tool for Java projects, using a standard project structure and an XML configuration file called pom.xml.
Key Benefits
- Automatically downloads and manages project dependencies (like Rest Assured, TestNG) from a central repository.
- Provides a standard, predictable project structure across teams.
- Supports build lifecycle phases: compile, test, package, and more.
- Integrates cleanly with CI/CD tools like Jenkins.
A pom.xml for a Selenium and TestNG Project
Everything Maven does is driven by the pom.xml file at the root of the project. Its coordinates (groupId, artifactId and version) identify your project, and the dependencies section lists the libraries it needs. A typical test automation project looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.qa</groupId>
<artifactId>web-tests</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
The version numbers above are only examples; check Maven Central for the current release of each library before you copy them. The Surefire plugin is what actually runs your tests during the test phase, and the configuration tells it to use your TestNG suite file.
Standard Folder Layout and the Build Lifecycle
Maven expects code in fixed places, which is why any Maven project feels familiar the moment you open it:
| Folder | Holds |
|---|---|
src/main/java | Application or framework code, such as page objects and utilities |
src/test/java | Test classes |
src/test/resources | Test data, config files and suite files |
target | Build output and reports, created by Maven and never committed |
The default lifecycle runs in order: validate, compile, test, package, verify, install, deploy. When you ask for a phase, Maven runs every phase before it as well, so mvn package also compiles and tests.
mvn clean test # delete old output, compile, run all tests
mvn test -Dtest=LoginTest # run a single test class
mvn test -DskipTests # build without running tests
mvn dependency:tree # show every library and where it came from
Downloaded libraries are cached in a local repository, usually the .m2 folder in your home directory, so the second build is much faster than the first.
Mistakes Testers Make with Maven
- Wrong dependency scope. Test libraries should use
testscope. But if your page objects live insrc/main/javaand use Selenium, Selenium needs the default compile scope. - Tests that only run from the IDE. If
mvn clean testfails on the command line, it will fail on Jenkins too. Run it locally before pushing. - Version conflicts. Two libraries can pull in different versions of the same dependency. Use
mvn dependency:treeto find the clash. - Committing the target folder. Add it to
.gitignore; Maven recreates it on every build. - Hard-coding the browser or URL. Pass values such as
-Dbrowser=chromeon the command line and read them withSystem.getProperty(), so one build can target different environments.
Gradle is the main alternative to Maven. It uses a script-based build file and is popular in Android projects, while Maven's fixed conventions remain very common in Java test automation teams.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.