Maven Tutorial
Maven in IDE
Maven is a powerful build tool for Java projects, primarily used for dependency management and project building. Inheritance is a concept in Maven that allows a project to inherit settings from a parent project. This tutorial will walk you through Maven inheritance and how it can be applied to your projects.
Maven projects are defined in a Project Object Model (POM) file named "pom.xml". Inheritance in Maven is achieved by specifying a parent POM file in a project's POM file. The child project then inherits the configurations, dependencies, and plugins from the parent project.
To create a parent POM, create a new folder and a "pom.xml" file inside it. Define the parent POM with a unique groupId, artifactId, and version. Here's a simple parent POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>parent-pom</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>11</java.version> </properties> <dependencies> <!-- Add your dependencies here --> </dependencies> <build> <plugins> <!-- Add your plugins here --> </plugins> </build> </project>
Create another folder for the child project, and a "pom.xml" file inside it. To specify the parent project, add the "parent" element to the child POM, as shown below:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>parent-pom</artifactId> <version>1.0.0</version> <relativePath>../parent-pom/pom.xml</relativePath> </parent> <artifactId>child-project</artifactId> <dependencies> <!-- Add child-specific dependencies here --> </dependencies> <build> <plugins> <!-- Add child-specific plugins here --> </plugins> </build> </project>
The child project inherits properties, dependencies, and plugins from the parent POM. However, you can override these settings by redefining them in the child POM.
To build the project, navigate to the child project's directory and run the following command:
mvn clean install
Maven will automatically build the parent project first, then the child project, applying the inherited configurations, dependencies, and plugins.
In conclusion, Maven inheritance allows you to manage common configurations, dependencies, and plugins in a parent POM.
How to use parent POM for Maven inheritance:
parent-pom.xml
:<project> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> </project>
Configuring child POMs and inheritance in Maven:
child-project/pom.xml
:<project> <parent> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> </parent> <!-- Add specific configurations for the child project --> </project>
Scoping and properties inheritance in Maven projects:
<properties> <java.version>1.8</java.version> </properties>Reference in child POMs:
${java.version}
.