Dependency Override for multiple modules under scala application

How to solve the not found class exception in a scala module

When a class is not found under a sbt multi-module application

Recently I found an issue where I was pulling a base docker image for a scala project. Then inside my project's Dockerfile, there was a dependency that wasn't getting include in the classpath of the project.

I got the following error:

java.lang.NoClassDefFoundError: org/apache/commons/dbcp2/BasicDataSource
...
Caused by: java.lang.ClassNotFoundException: org.apacha.commons.dbcp2.BasicDataSource

I initially thought that among the different stages of the base image, the jar for this dependency was not getting transferred to the user's Docker container.

However when I opened a shell and look for the .ivy2 cache jars, I found the jar.

So I realized the issue was that the dependency wasn't loading as expected in the Job's project.

I had the following Sbt setup in the base image:

lazy val platform = project.in(file("modules/platform")
	.enablePlugins(PlayScala)
	.settings(
		name := "content-platform",
		libraryDependencies ++= Seq(
			cache,
			ws,
			filters,
			...
			"org.apache.commons" % "commons-dbcp2" % "2.12.0"
		)
	)

lazy val job = project.in(file("modules/job"))
	.enablePlugins(PlayScala)
	.settings(
		name := "content-job"
		dependencyOverrides += "org.apache.commons" % "commons-dbcp2" % "2.12.0"
	)
	.dependsOn(platform % "test->test;compile->compile")
	.aggreate(platform)

It wasn't until I added dependencyOverrides that running the Job module started to work. No matter I've previously wiped out my entire docker infra, so it could rebuilt from scratch.

Again, this was a very weird case only happening on an Intel CPU. I haven't found the root cause, but at least I found a solution to continue working.