TExcelTAD
Home › Java Builds: Maven & Gradle › dependencyManagement & BOM Imports

dependencyManagement & BOM Imports

The previous lesson used <dependencyManagement> to fix one conflict. Its real job is bigger: it is the single place where a project (or a whole organisation) decides which version of each library is used, so that modules declare what they use and never which version.

What dependencyManagement does, and doesn't do

  • An entry in <dependencyManagement> adds nothing to the build. It says: "if this artifact appears anywhere in the tree, use this version (and this scope, if given)".
  • A <dependency> without a <version> takes it from dependencyManagement. Without a managed version, that's an error.
  • Managed versions apply to transitive dependencies too (you saw version managed from 1.0).
  • They are inherited: put them in a parent POM and every module agrees.

BOMs: version sets you import

Libraries made of many artifacts (Jackson has about 40, JUnit a dozen, Spring hundreds) publish a BOM ("bill of materials"): a POM with packaging pom that contains nothing but a <dependencyManagement> section listing every artifact of the release in matching versions. You import it with type pom and scope import, and its entries are copied into your dependencyManagement:

bash · try it yourself
mkdir -p bom/src/main/java/demo && cd bom
cat > src/main/java/demo/Dates.java <<'SRC'
package demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.LocalDate;

public class Dates {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
        System.out.println(mapper.writeValueAsString(LocalDate.of(2026, 1, 15)));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>bom</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.fasterxml.jackson</groupId>
        <artifactId>jackson-bom</artifactId>
        <version>2.22.4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <!-- no versions: the BOM supplies them -->
    <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
    <dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId></dependency>
  </dependencies>
</project>
POM
echo "artifacts managed by jackson-bom 2.22.4: $(grep -c '<artifactId>jackson-' ~/.m2/repository/com/fasterxml/jackson/jackson-bom/2.22.4/jackson-bom-2.22.4.pom)"
mvn -q compile
mvn -q -o dependency:3.11.0:tree -DoutputFile=/dev/stdout
java -cp "target/classes:$(mvn -q -o dependency:3.11.0:build-classpath -Dmdep.outputFile=/dev/stdout)" demo.Dates
artifacts managed by jackson-bom 2.22.4: 72
com.example:bom:jar:1.0
+- com.fasterxml.jackson.core:jackson-databind:jar:2.22.4:compile
|  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.22:compile
|  \- com.fasterxml.jackson.core:jackson-core:jar:2.22.4:compile
\- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.22.4:compile
[2026,1,15]

Note the versions: jackson-databind and jackson-core are 2.22.4, but jackson-annotations is 2.22. Since 2.20 the annotations module has its own, shorter version scheme; the BOM knows that, which is exactly why you let the BOM pick versions instead of writing ${jackson.version} everywhere.

Who wins when versions are managed twice?

Real projects import several BOMs and also manage some versions themselves. The precedence, highest first:

  1. A <version> written directly on a <dependency>.
  2. An entry declared directly in your (or a parent's) <dependencyManagement>.
  3. Imported BOMs, in declaration order: the first BOM that manages an artifact wins.

The third rule bites when two BOMs overlap. This session imports two JUnit BOMs, 6.1.3 and 5.14.4, in both orders:

bash · try it yourself
mkdir -p order && cd order
pom() {   # pom <first BOM version> <second BOM version>
  cat > pom.xml <<POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId><artifactId>order</artifactId><version>1.0</version>
  <dependencyManagement>
    <dependencies>
      <dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>$1</version><type>pom</type><scope>import</scope></dependency>
      <dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>$2</version><type>pom</type><scope>import</scope></dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope></dependency>
  </dependencies>
</project>
POM
  echo "BOM $1 first, then $2:"
  mvn -q -o dependency:3.11.0:tree -DoutputFile=/dev/stdout | sed -n 2p
}
pom 6.1.3 5.14.4
pom 5.14.4 6.1.3
BOM 6.1.3 first, then 5.14.4:
\- org.junit.jupiter:junit-jupiter-api:jar:6.1.3:test
BOM 5.14.4 first, then 6.1.3:
\- org.junit.jupiter:junit-jupiter-api:jar:5.14.4:test

Silent and order-dependent. Two habits avoid it: import each BOM family once, and put the most important platform BOM (Spring Boot's, your company's) first.

Your own BOM

Organisations with many repositories publish their own BOM (often called a platform): one artifact that pins the approved versions of Jackson, JUnit, logging and in-house libraries. Every service imports it, so an upgrade is one change in one place, and the enforcer checks nobody escapes it. It's an ordinary pom-packaged project:

xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.platform</groupId>
  <artifactId>platform-bom</artifactId>
  <version>2026.10.0</version>
  <packaging>pom</packaging>
  <dependencyManagement>
    <dependencies>
      <dependency>                                    <!-- BOMs can import BOMs -->
        <groupId>com.fasterxml.jackson</groupId><artifactId>jackson-bom</artifactId>
        <version>2.22.4</version><type>pom</type><scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Tip: Don't confuse a BOM with a parent POM. A parent is inherited (<parent>), and there can be only one; it carries plugins, properties and profiles too. BOMs are imported, there can be many, and they carry only versions. Gradle consumes the same BOMs with platform(...), as you'll see in Version Catalogs.

Test yourself

What happens when you add an artifact only to <dependencyManagement>?

  1. It is added to the compile classpath
  2. Nothing is added; its version applies if the artifact appears in the tree
  3. It is downloaded but not used
  4. It is deployed with the project

How is a BOM imported in Maven?

  1. As a <parent>
  2. In <dependencyManagement> with <type>pom</type> and <scope>import</scope>
  3. In <dependencies> with scope compile
  4. Through settings.xml

Two imported BOMs manage the same artifact in different versions. Which version is used?

  1. The highest
  2. The one from the BOM declared first
  3. The one from the BOM declared last
  4. The build fails