DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Tracking Dependencies Beyond the Build Stage
  • Automating Maven Dependency Upgrades Using AI
  • Maven Dependency Scope Applied
  • How To Approach Dependency Management in Java [Video]

Trending

  • Avoiding AI Debt as a Team
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Every Generation Fears the Machine. Here Is How the AI Job Panic Usually Ends
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. The Bill of Materials in Maven

The Bill of Materials in Maven

Learn more about BOM — one of the few ways Spring helps us forget about transitive dependency issues.

By 
Ayush Prashar
·
Updated Jun. 07, 19 · Presentation
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
35.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This blog was first published at Knoldus Blogs.

Lately, while working with Spring WebFlux, I came across this really helpful concept called the bill of materials, also known as BOM, a concept not really limited to Spring at all. BOM is one of the few ways Spring helps us forget about issues related to transitive dependencies and focus solely on our project requirements.

So when we generally create a large scale project with dozens or hundreds of dependencies, chances are multiple of them use something common internally by transitive dependencies. So, let's say we have a dependency called “common-3.0.0.RELEASE,” which is used by our logging artifact and “common-2.9.0.Release” used by our testing framework. In such a case, we have a conflict. We need to ensure we get a working version so that both the artifacts can work together in synergy.

There’s a way Maven solves this problem for us. It uses the concept of dependency mediation. It works on something known as a path to the dependency. Whoever has the shortest path to the project is picked over the other. So, let's say, in our case, the chain is something like log4j —> commons —>2.9.0.Release and JUnit —> unit-tests —> common.3.0.0.Release, maven selects commons 2.9 since it has a shorter path to this POM.

But why rely on some external method when we can control stuff right? We can always use a version we’re hoping to use even transitively by specifying it in the POM. Sure, but this would clutter our POM because how many are we going to mention? Or we could do one better. We can let dependency management take control.

The BOM

Maven lets us define the versions of our dependencies or transitive dependencies in a separate POM. It is in this POM that we declare the versions and scope of the dependencies. Hence, we get a centralized place to mention all the dependency details.

Let’s create a sample bill of materials POM file.

Screenshot from 2019-06-04 15-08-09

As can be seen, the bill of materials is a perfectly normal POM file where we can include our dependency information.

How to Use It?

Now we understand that there’s a file that contains all information regarding the versions of any dependency that we may want to be a part of our project. The next thing is to include this file in our original POM.

One way to do this is the way Spring does it, by inheriting it like a parent project. All we need to do to inherit is to provide the parent information in the parent tags.

Screenshot from 2019-06-04 15-09-21

Though this is how Spring works, we can not inherit from more than one parent POM. So, in case you wish to inherit from more than one, we pass the credentials in our dependencies.

Screenshot from 2019-06-04 15-03-57

The Precedence

So, there are multiple ways of deciding the versions, which means there is an order of precedence.

  1. Versions provided in the direct declaration in POM take the highest precedence.
  2. Version provided in parent pom takes second precedence.
  3. Version from imported pom takes third place
  4. Lastly, whatever we get from dependency mediation

In case of conflict when inheriting multiple POMs, the dependency declared earlier in order takes precedence.

So now, you can understand why things magically auto-configure in Spring POMs, what a BOM is, and what to do in case there’s any custom operation you wish to perform. Thanks for reading the blog and please let me know your thoughts in the comments.

References:

  • Maven Doc
  • Maven Fundamentals
Apache Maven Dependency

Published at DZone with permission of Ayush Prashar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Tracking Dependencies Beyond the Build Stage
  • Automating Maven Dependency Upgrades Using AI
  • Maven Dependency Scope Applied
  • How To Approach Dependency Management in Java [Video]

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook