Ensure matching versions with Kotlin + Kapt + hibernate-jpamodelgen

I have a Kotlin + Spring Boot project that has org.springframework.boot:spring-boot-dependencies:2.5.7 as a dependency. spring-boot-dependencies already includes hibernate-jpamodelgen as a transitive dependency. But to actually be able to use hibernate-jpamodelgen in combination with kapt, I need a line like the following in my build.gradle: kapt 'org.hibernate:hibernate-jpamodelgen:5.4.32.Final'.

So, in summary, my build.gradle file looks something like this:

plugins {
    id('kotlin-kapt')
}

group = 'org.company.app'
description = 'stuff'

dependencies {
    api(platform('org.springframework.boot:spring-boot-dependencies:2.5.7'))
    kapt 'org.hibernate:hibernate-jpamodelgen:5.4.34.Final'
}

Now, the problem is that I always need to be careful to not mix up versions, for example, if I bump the spring-boot-dependencies, I must make sure that the kapt line also includes the version of hibernate-jpamodelgen included by spring-boot-dependencies.
This is, of course, error-prone, so I’m wondering how I can improve on that?

Maybe it’s somehow possible to get the version of a transitive dependency and then include that in the kapt line in the build.gradle, or maybe there’s a completely different approach?

Thanks for any hints!

The kapt configuration is not directly related to the api configuration, thus the version constraints you add from the Spring Boot BOM are not seen by it.
Just also apply the Spring Boot BOM to the kapt configuration like you do for the api configuration and you can leave out the version for jpamodelgen.

If you also applied the Spring Boot plugin, you could even not use an explicit version for spring-boot-dependencies but instead of coordinates use org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES.

1 Like