SBT: Can I dynamically add a library dependency from another?
I am trying to create a plugin that adds a runtime dependency of a particular library (JVM agent for java animator) if the project libraryDependencies
contains one of a specific set of other libraries (one of the Kamon libraries that needs and then I can link to that downloaded dependency file when building a Docker image (using sbt-docker).
Kamon defines aspectweaver as a "provided" dependency, but AFAICTs are not transiently available from projects that depend on one of these Kamon libs.
The problem is that I cannot override libraryDependencies
in my plugin because at that time the actual project had not added any dependencies yet, so I get an empty list. So I have to somehow add this dependency after the project has it installed libraryDependencies
, but before SBT starts loading dependencies.
Is it possible? Is there another setting that I can override to achieve this?
My backup is somehow loading this jj aspect of myself from a task in my plugin. Is there some SBT subsystem I could use to do this so that the jar file is resolved against (or downloaded) the local Ivy repository?
source to share
I figured it out using allDependencies
. Here's the (shortened) code:
object AspectjWeaverPlugin extends AutoPlugin {
private val aspectjWeaver = "org.aspectj" % "aspectjweaver" % "1.8.5" % "runtime"
private val aspectjWeaverLibs = List("kamon-spray", "kamon-akka", "kamon-scala")
val aspectjWeaverJarFile = taskKey[Option[File]]("The aspectj weaver jar file if it exists on the classpath.")
override def requires = plugins.JvmPlugin
override def projectSettings = Seq(
allDependencies := {
allDependencies.value ++ libraryDependencies.value.find(module => aspectjWeaverLibs.contains(module.name)).map(_ => aspectjWeaver)
},
aspectjWeaverJarFile := {
val classpath = (managedClasspath in Runtime).value
classpath.find(_.data.getName == s"${aspectjWeaver.name}-${aspectjWeaver.revision}.jar").map(_.data)
}
)
}
Now I can use this new task with the additional aspectj weaver jar to set the required option -javaagent:...
to run
, re-start
and into the Docker image that I build with sbt-docker.
Still wondering what is the best way to do it though.
source to share