October 25, 2024
Chicago 12, Melborne City, USA
java

Maven – using PluginManagement and DependencyManagement for versions from parent


Assume I have a parent POM like this (simplified):

<project>
  <artifactId>foo-parent</artifactId>
  ...
  <dependencies>
    <dependency>
      <artifactId>slf4j-api</artifactId>
      <version>2.0.0</version>
    <dependency>
  <dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
      </plugin>
    </plugins>
  </build>
</projects>

And a POM inheriting from it

<project>
  <parent>
    <artifactId>foo-parent</artifactId>
  </parent>
  <artifactId>foo-child</artifactId>

  ...
  <dependencyMangement>
    <dependencies>
      <dependency>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.16</version>
      <dependency>
    <dependencies>
  </dependencyManagement>
  <build>
    <pluginManagment>
      <plugins>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.5.1</version>
        </plugin>
      </plugins>
    </pluginManagment>
  </build>
</projects>

Based on what described in https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management , it mentioned that dependencyManagement would affect the dependency resolved transitively.

I was expecting that, when I build foo-child, I will be using surefire 3.5.1 and slf4j-api 2.0.16, as I was expecting dependencyMangement and pluginManagement to affect the versions I "transitively" inherited from foo-parent. However I found that foo-child is still using surefire 3.0.0-M5 and slf4j-api 2.0.0.

Does it mean that, versions defined in parent’s dependencies / plugins are not affected by dependencyManagement/pluginManagement as they are not treated as "transitive" (or other reasons)?

Then I take a step forward, by explicitly declaring dependency / plugin in my foo-child POM without mentioning version, by adding:

<project>
  ...
  <dependencies>
    <dependency>
      <artifactId>slf4j-api</artifactId>
    <dependency>
  <dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</projects>

(of course, the dependencyManagement and pluginManagement is kept there)

Now foo-child will have slf4j-api:2.0.16 resolved, but surefire-plugin is still 3.0.0.

How to explain the difference in behavior between dependencyMangement & pluginManagement in this case?

I am using Maven 3.9.6



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video