How to override the dependency of an sbt plugin?
Asked Answered
G

3

27

I've written an sbt plugin called sbt-jumi which implements sbt integration for Jumi. Right now the sbt-jumi plugin depends on the current Jumi release.

Here is the relevant line from the plugin's build.sbt:

libraryDependencies += "fi.jumi" % "jumi-launcher" % "0.5.376"

And a user of the plugin would add this to his project/plugins.sbt file:

addSbtPlugin("fi.jumi.sbt" % "sbt-jumi" % "0.1.0")

Now let's say that Jumi 0.6.400 is released and it's backward compatible. How can a user of the sbt-jumi plugin configure it to use Jumi 0.6.400, without me having to release a new version of the plugin?

Here is how to do it in Maven. But how to do it in sbt?

Giorgio answered 5/8, 2013 at 19:19 Comment(0)
G
39

Overriding the dependencies of plugins happens the same way as overriding normal dependencies, except that the configuration must be entered into project/plugins.sbt. Overriding dependencies is explained in Library Management. Here is a summary:

If the version you wish to use is greater than the dependency that you would get transitively, sbt will use the larger version by default. You may change the conflict manager to change the default behavior - for example this will create an error on conflict:

conflictManager := ConflictManager.strict

In other words, this in project/plugins.sbt would work:

libraryDependencies += "fi.jumi" % "jumi-launcher" % "0.6.400"

You may check your plugin dependencies using reload plugins and then show update. It should now show the older version as "(EVICTED)".

If the version you wish to use is lower than the default dependency, then you will need to override differently. One way is to force the dependency:

libraryDependencies += "fi.jumi" % "jumi-launcher" % "0.4.350" force()

Another way is to use the dependencyOverrides setting:

dependencyOverrides += "fi.jumi" % "jumi-launcher" % "0.4.350"

The difference between the two methods is that overriding doesn't introduce a direct dependency. I don't think the difference matters for plugins, but for published artifacts it has some differences.

Giorgio answered 7/8, 2013 at 16:51 Comment(3)
Here is an issue I got and want to share : day 0 : current online published version of the plugin 0.3 | day1 : I publish locally my in-development plugin with version 0.4 and reference this plugin with a local repository | day 2 : the plugin is published with version 0.4 on internet official repo | day 3 : my plugin does not do what I expect anymore because sbt load both plugins but decide to take the one from internet. To workaround the issue I had to change my version to 0.5 and then it worked.Selfpity
How can I completely exclude a dependency of an sbt plugin?Naphthyl
OK found out myself: Add excludeDependencies in build.sbt, see github.com/mkurz/coursier-excludedependencies-sbtplugin/blob/…. However be aware if you use coursier there is a bug so it does not work yet: github.com/coursier/coursier/issues/1590Naphthyl
M
1

If the dependency you want to override is itself also an sbt plugin, then the answer is slightly different.

All these changes are in the project/plugins.sbt

If the plugin is a newer version then you can simply add it like normal and the automatic dependency resolution should pick the newer version, but if you need it to be an earlier version, then you need to add this:

dependencyOverrides += {
  val sbtV = (pluginCrossBuild / sbtBinaryVersion).value
  val scalaV = (update / scalaBinaryVersion).value
  val dependency = "fi.jumi" % "jumi-sbt" % "{version}"
  sbt.Defaults.sbtPluginExtra(dependency, sbtV, scalaV)
}

this just replicates what addSbtPlugin is doing under the hood.

Melvinmelvina answered 31/3, 2023 at 17:28 Comment(0)
B
0

adding libraryDependencySchemes to the plugin.sbt file will allow version overrides in your plugins

(I'm listing scala-xml in this example since this is the popular plugin dependency causing all the conflicts with its current partial 2.x adoption by plugin libraries)

libraryDependencySchemes ++= Seq(
  "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always
)
Balancer answered 14/5, 2024 at 20:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.