HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Maven / Maven BOM – Bill Of Materials Dependency

Maven BOM – Bill Of Materials Dependency

If you have worked on maven in your projects for dependency management, then you must have faced one problem at least once or may be more than that. And the problem is version mismatch. It generally happens when you got some dependencies which bring it’s related dependencies together with certain version. And if you have included those dependencies with different version numbers already, they can face undesired results in compile time as well as runtime also.

Ideally to avoid above issue you need to explicitly exclude the related dependency, but it is quite possible that you can forget to do so.

To solve version mismatch issue, you can use the concept of a “bill of materials” (BOM) dependency. A BOM dependency keep track of version numbers and ensure that all dependencies (both direct and transitive) are at the same version.

1. How to add maven BOM dependency

Maven provides a tag dependencyManagement for this purpose. You need to add the maven bom information in this tag as follows. I am taking the example of Spring bom file.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.0.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring framework artifacts. So it will work perfectly fine.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
<dependencies>

Each project has it’s own maven bom file

Please note that there is no common or universal bom file. Each project (if support this feature) provides its own bom file and manages versions of it’s related dependencies.

Few example of various bom files are below:

1) RESTEasy Maven BOM dependency

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.jboss.resteasy</groupId>
			<artifactId>resteasy-bom</artifactId>
			<version>3.0.6.Final</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

2. JBOSS Maven BOM dependency

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.jboss.bom</groupId>
			<artifactId>jboss-javaee-6.0-with-tools</artifactId>
			<version>${some.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement> 

3) Spring Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.0.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

4) Jersey Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Those are few examples. So whenever you work next time in maven, try to explore this feature.

3. Maven BOM vs POM

First of all, BOMs are ordinary pom.xml files — they contain no source code and their only purpose is to declare their bundled modules. It defines the versions of all the artifacts that will be created in the library. Other projects that wish to use the library should import this pom into the dependencyManagement section of their pom.

POM files are more than just dependencies. For example organization and licenses, the URL of where the project lives, the project’s dependencies, plugins, profiles and many such things. It also control the complete build process of the project.

Happy Learning !!

Reference:

POM – Maven Docs
Maven Dependency Mechanism

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Biswakalyan

    June 16, 2016

    like this post. I was using this since long as concept of dependency management but good to know it’s BOM

  2. Jay

    July 13, 2015

    very useful post.

    Do you have any idea, how to import a pom file in another pom file with profiles, so that I can use imported pom file profile as well.
    Regards

    • Lokesh Gupta

      July 13, 2015

      I am not aware of this.

  3. Bibhash Roy

    February 19, 2014

    It is not clear from the article how the conflict in jars (which inevitably sometime happens) is managed / resolved using tag. Can you please elaborate on that using an example.

    Also does the tag has to be put in a parent POM in case there are multiple projects to be handled. Or, can the be put in the same POM (of a project) where the dependencies are mentioned? Please highlight on the same.

Comments are closed on this article!

Search Tutorials

Maven Tutorial

  • Maven – Installation
  • Maven – Settings
  • Maven – Dependency Mgmt
  • Maven – Dependency Scopes
  • Maven – POM
  • Maven – Parent POM
  • Maven – Repositories
  • Maven – Local Repo Path
  • Maven – M2_REPO
  • Maven – Network Proxy
  • Maven – Enforce Java Versions
  • Maven – Simple Java Project
  • Maven – Web Project
  • Maven – Multi-module Project 1
  • Maven – Multi-module Project 2
  • Maven – Java Source Folders
  • Maven – BOM [Bill Of Materials]
  • Maven – Import Remote Catalogs
  • Maven – Create Custom Archetype
  • Maven – Compiler Level Mismatch
  • Maven – Ant Build
  • Maven – IntelliJ
  • Maven – JSTL Support
  • Maven – Tomcat Plugin
  • Maven – Uber Jar for Spring Boot
  • Maven – Shade Plugin
  • Maven – Remove corrupt jars

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces