Maven Tutorial
Maven in IDE
pluginManagement
is an element in the Maven POM file used to manage and standardize plugin configurations across multiple projects or modules. It provides a central location to manage plugin versions, configurations, and default goals for projects that inherit from the same POM.
In this tutorial, you'll learn how to use pluginManagement
to manage Maven plugins in a multi-module project.
1. Creating a multi-module project:
For this tutorial, we'll create a multi-module Maven project with the following structure:
parent-pom ���� module-a ���� module-b
Create a new directory called parent-pom
and navigate to it. Inside this directory, create a new Maven POM file named pom.xml
with the following content:
<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-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>module-a</module> <module>module-b</module> </modules> </project>
Next, create two directories named module-a
and module-b
under the parent-pom
directory. Create a pom.xml
file inside each module directory with the following content:
<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-SNAPSHOT</version> </parent> <artifactId>module-a</artifactId> <!-- Change to 'module-b' for module-b's pom.xml --> </project>
Make sure to update the artifactId
element for each module accordingly.
2. Adding pluginManagement
to the parent POM:
Open the parent-pom/pom.xml
file and add a pluginManagement
element inside the <build>
element as follows:
<build> <pluginManagement> <plugins> <!-- Add plugin configurations here --> </plugins> </pluginManagement> </build>
3. Managing plugins:
Inside the <plugins>
element of the pluginManagement
, you can now add plugin configurations that will be applied to all inheriting modules. For example, you can manage the version of the maven-compiler-plugin
like this:
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> </build>
This configuration sets the version of the maven-compiler-plugin
to 3.8.1 and specifies the source and target Java versions to 1.8. This configuration will be applied to all modules inheriting from the parent-pom
POM file.
4. Using managed plugins in child modules:
To use a managed plugin in a child module, simply reference the plugin in the module's POM without specifying the version or repeating the configuration. For example, in the module-a/pom.xml
file, you can reference the managed maven-compiler-plugin
like this:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> </plugins> </build>
This will inherit the version and configuration from the parent POM's pluginManagement
. You can apply the same to the module-b/pom.xml
.
To override or extend the configuration of a managed plugin in a specific module, add the <configuration>
element with new or modified settings to the plugin's declaration in the module's POM file.
In conclusion, using pluginManagement
in the parent POM file is a best practice for managing Maven plugins in multi-module projects, as it centralizes plugin configuration and ensures consistent settings across all modules.
Maven pluginManagement in project configuration:
pluginManagement
is used to define and manage plugin configurations in a centralized manner within a POM.<!-- Example pluginManagement section in POM --> <build> <pluginManagement> <plugins> <!-- Define plugins and versions here --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <!-- Additional plugins... --> </plugins> </pluginManagement> </build>
How to use pluginManagement in Maven POM:
<build>
section without specifying versions or detailed configurations.<!-- Example usage of plugin from pluginManagement --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!-- No version or detailed configuration here --> </plugin> <!-- Additional plugins... --> </plugins> </build>
Scoping and inheritance in Maven pluginManagement:
<!-- Example scoping and inheritance --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <!-- Configuration specific to compile phase --> </configuration> <executions> <!-- Executions for specific build phases --> </executions> </plugin> <!-- Additional plugins... --> </plugins> </build>
Configuring plugin repositories with pluginManagement:
<pluginRepositories>
section of <build>
or <pluginManagement>
.<!-- Example plugin repository in pluginManagement --> <build> <pluginManagement> <pluginRepositories> <pluginRepository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> <!-- Additional repositories... --> </pluginRepositories> </pluginManagement> </build>
Using property expressions in Maven pluginManagement:
<!-- Example using properties in pluginManagement --> <properties> <maven.compiler.version>3.8.0</maven.compiler.version> </properties> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> </plugin> <!-- Additional plugins... --> </plugins> </pluginManagement> </build>
Customizing plugin execution and reporting with pluginManagement:
<!-- Example customizing plugin execution in pluginManagement --> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0</version> <executions> <!-- Custom execution configuration... --> </executions> </plugin> <!-- Additional plugins... --> </plugins> </pluginManagement> </build>