Gradle upgrade - dependency problems


I’m upgrading (6.x->7.6) a multi-project gradle repository. I have a subproject that has had a dependency declaration like the following:

dependencies {
  compile libs.hdrhistogram
  compile libs.yetusAnnotations

  provided libs.scalaLibrary
  provided libs.sparkCore
  provided libs.sparkSql
  provided libs.slf4jApi

  testCompile libs.junit
  testCompile libs.scalatest
  testCompile libs.log4jApi
  testCompile libs.log4jCore
  testCompile libs.log4jSlf4jImpl

Based on the upgrade guide (Upgrading your build from Gradle 6.x to 7.0) and the maven migration guide (Migrating Builds From Apache Maven) I changed the dependencies to as follows:

dependencies {
  implementation libs.hdrhistogram
  implementation libs.yetusAnnotations

  implementation libs.scalaLibrary
  implementation libs.sparkCore
  implementation libs.sparkSql
  implementation libs.slf4jApi

  testImplementation libs.junit
  testImplementation libs.scalatest
  testImplementation libs.log4jApi
  testImplementation libs.log4jCore
  testImplementation libs.log4jSlf4jImpl

Unfortunately the project doesn’t compile when setting the provided dependencies to compileOnly.
The problem comes when I inspect the contents of the created jar. Previously the generated jar did not include the scala/spark/slf4j classes, however after the change the jar size has exploded from 17Mb to 130Mb. Try as I might I can’t really find a simple way to define depenencies to be used during compile, but exclude during the jar task. Am I missing something, or should I use a specific plugin for this?

Some points here.

  • why does it not compile with compileOnly? If something is compileOnly it is available at compile time, so it sounds unlikely that it is not available at compile time.
  • how to define something that “is available during compile but excluded during the jar task” cannot be answered without seeing your build, because Gradle does not build bad-practice fat jars at all, unless you configure it to and if you do, it depends on how you do it, but generally, you would pack the runtimeClasspath while using compileOnly. If you really need to build such a bad practice fat jar, I’d recommend you at least use John Engelman’s shadow plugin, which sails around some of the cliffs at least.