Gradle: use the bom of a dependency

Viewed 382

Using gradle, in a projectA we import the resteasy BOM:

compile platform('com.fasterxml.jackson:jackson-bom:2.13.2')
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'

Then in a projectB we import projectA

implementation("com.my.service:projectA:0.0.1-SNAPSHOT")

Now, when we compile projectB, jackson-datatype-jsr310 cannot be found. It cannot determine the version to use. Same if in projectB I want to import another jackson dependencies, that are defined in the BOM.

How can I automatically use the same version in projectB?

1 Answers

The compile scope is not inherited transitively by projects that use your module as a dependency.

To set a transitive dependency (being a BOM or of any other kind) you should use the api scope.

However, note that for using the api scope, you need to tell Gradle that your project is a library (and not an executable module). To do this, you should add the java-library plugin.

Your project A should be defined this way:

plugins {
    id 'java-library'
}

dependencies {
  api platform('com.fasterxml.jackson:jackson-bom:2.13.2')
  api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
}

You can check all the details at the java-library plugin documentation.

Related