sbt publish a package with varying dependencies

ghz 8months ago ⋅ 82 views

I have an sbt project which consists of 3 different layer:

  • base
  • impl (is a directory which contains many different implementation packages, and each package depends on base)
  • api (depends on an "impl", could be any of them)

The code in the base and the api package is always the same, the only thing that can change is one of the impls, for instance, I can introduce a new implementation.

Now I want to publish locally the api package depending on each of the implementations, by different names, for instance, one package called api-impl1, api-impl2 and so on.

One thing that I've tried is to do this one by one, which is not really a smart idea. So I was wondering if there's any dynamics to sbt to do this for me

Answers

Yes, you can automate this process in SBT using sbt-sriracha or sbt-dynver plugins, both of which provide dynamic versioning capabilities.

Here's a basic example of how you can achieve this using sbt-sriracha:

  1. First, add sbt-sriracha plugin to your project/plugins.sbt file:
addSbtPlugin("com.geirsson" % "sbt-sriracha" % "0.1.6")
  1. Then, enable sbt-sriracha in your build.sbt file:
enablePlugins(SrirachaPlugin)
  1. Next, define a setting in your build.sbt to compute the version dynamically based on the implementation:
version := {
  val implVersion = ??? // Compute the version dynamically based on the implementation
  s"api-$implVersion"
}

Replace ??? with your logic to compute the version dynamically based on the implementation. For example, if your implementation packages follow a specific naming convention like impl1, impl2, etc., you can extract the implementation version from the package name.

  1. Finally, publish your project locally:
sbt publishLocal

This will publish your API package with the dynamic version, where the version is derived from the implementation.

Keep in mind that you need to ensure that the dependencies between the API and implementation packages are properly managed in your build.sbt file, so that sbt can resolve the dependencies correctly during the publishing process.