Tricks for scala with gradle
Complex scala builds in gradle
With the recent release of gradle 6.x its support for scala has greatly increased. Now, not only scala 2.13 is supported, but also the compiler was upgraded to a up-to-date version of zinc.
This has many advantages regarding speed, but also for correctness of builds. I previously experienced flaky builds for complex and compiler heavy projects. One such example is a complex class hierarchy when using a library like pureconfig which relies heavily on macros and reflection, where not all the classes reside in the same file.
Now, after the upgrade setting:
tasks.withType(ScalaCompile).configureEach {
scalaCompileOptions.forkOptions.with {
jvmArgs = ["-Xss2m"]
}
}
is still required to allow for enough memory during compilation. Otherwise, strange errors like:
could not find implicit value for parameter A
occured, just like before the upgrade.
See https://github.com/pureconfig/pureconfig/issues/644 for details.
In case you cannot upgrade there are two workarounds:
- simplify the configuration case class hierarchy
- put all of the class hierarchy into the same file. The outdated scala 2.10 build code fails to handle compilation order correctly otherwise.