carlspring
carlspring

Reputation: 32597

How to properly use Maven BOM-s?

I haven't done this in a very long time, so I seem to have forgotten the proper way of doing it. I would like to create BOM file in which I'd like to have a bunch of dependencies defined in a <dependencyManagement/> section and then not have to define them as dependencies in my projects.

Could somebody clarify how this works? Were BOM files just for declaring a set of dependencies in a POM file that could then use versionless, similarly to parents (except you can have as many of these, as you like), or could you also make it declare the dependencies for you?

For example, if my BOM has a <dependencyManagement/> section, do I also need to have it define a <dependencies/> one, if I wanted to make all projects that use the BOM, always use all of the dependencies? In the project using the BOM, I have the following defined in the <dependencyManagement/>:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>foo-bom</artifactId>
                <version>${project.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Do I still need to manually import the versionless dependencies via explicit declarations in the POM where they are being imported? Is the correct way to also define them as <dependencies/> in the BOM (outside the <dependencyManagement/> section)?

Upvotes: 2

Views: 1785

Answers (2)

LagSeeing
LagSeeing

Reputation: 73

I think you have miss understand the purpose of BOM.

BOM is not use for your lib, but for others who what to depend on your lib.

Here is the document of POM bill-of-materials-bom-poms

BOM define the version of your artifacts which can work together. When others want to use your several artifacts in your lib, they can just import your Bom in <dependencyManagement> and then just <dependency> your artifacts without <version>. (So called Bill Of Metaerials)

I think what you want is just a parent project to have some common dependency in tag, or you want to have a separate project to have some common dependency in <dependency> tag and then create a new project depend on it.

Upvotes: 1

J Fabian Meier
J Fabian Meier

Reputation: 35795

BOMs are dependencyManagement only. They cannot declare dependencies.

They serve two main purposes:

  • You can add your dependencies versionless (and the version will come from the BOM).
  • They override versions of transitive dependencies that you pull (so that you can make sure that you have a recent log4j even if one your dependencies pulls an ancient one).

If you really want to add a bunch of dependencies, write a POM with these dependencies and add it as dependency.

Upvotes: 3

Related Questions