What is pom.xml
in a Maven Project?
- In a Maven-based project,
pom.xml
(Project Object Model) is the central configuration file that contains information about the project and its dependencies, build configuration, and plugins. pom.xml
is the heart of a Maven project, managing dependencies, builds, and plugins efficiently.- It ensures that the project is easily configurable, portable, and reproducible across different environments.
Purpose of pom.xml
:
1️⃣ Manages Dependencies → Specifies all required libraries & frameworks.
2️⃣ Automates Build Process → Controls how the project is compiled, tested, and packaged.
3️⃣ Plugin Management → Integrates additional tools like Surefire
, Compiler
, Spring Boot
, etc.
4️⃣ Project Information → Defines metadata like project name, version, and description.
Key Elements in pom.xml
Tag | Description |
---|---|
<groupId> | Unique project identifier (like package name) |
<artifactId> | Name of the generated JAR/WAR file |
<version> | Project version (e.g., 1.0.0) |
<dependencies> | List of required dependencies (e.g., JUnit, Selenium) |
<repositories> | Specifies external repositories (if needed) |
<build> | Defines the build process (e.g., compiler version, plugins) |
<plugins> | Additional functionalities (e.g., Surefire for test execution) |
Common Maven Commands
Command | Description |
---|---|
mvn clean | Removes generated files (target directory) |
mvn compile | Compiles source code |
mvn test | Runs unit tests |
mvn package | Generates JAR/WAR file |
mvn install | Installs package in local repository |
mvn dependency:tree | Displays project dependency hierarchy |
Benefits of pom.xml
in Maven Projects
✔ Automatic Dependency Management → No need to manually download JARs.
✔ Simplifies Build Process → Handles compilation, testing, and packaging.
✔ Project Standardization → Ensures consistency across development teams.
✔ Easier Project Setup → Just use mvn clean install
to build the project.
Repositories / Dependencies?
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.21.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.21.1</version>
</dependency>
</dependencies>
- In Maven, dependencies are external libraries (JAR files) that a project needs.
- They are defined in the
pom.xml
file, and Maven automatically downloads them from repositories. -
pom.xml
file, and Maven automatically downloads them from repositories.
1️⃣ Maven checks the local repository (
~/.m2/repository/
).2️⃣ If not found, it downloads the dependency from the Maven Central repository.
3️⃣ It adds the JAR file to the project's classpath.