Search code examples
sbtsbt-assembly

SBT: Add dependencies to project at runtime


There is sbt project declaration

lazy val myProject = (Project("myProject", file("someRoot"))
    enablePlugins ...
    settings (...)

It has taskKey that extracts some dependencies to file system.

My problem is that for the moment of loading SBT I can't determine all the dependencies, it could be done only after private Command Alias is executed

addCommandAlias("resolveDependencies", "; resolveDependenciesTask; TODO: update myProject dependencies and reload it")

Is there anyway to do that?


Solution

  • Actually, disregard my comment on your question. You can use a command to modify the state of the build, so after you run it, the changes it made stay.

    Something along these lines:

    // in your build.sbt
    
    commands += Command.command("yourCustomCommand")(state =>
      Project.extract(state).append(
        Seq(libraryDependencies += // settings you want to modify
          "com.lihaoyi" % "ammonite-repl" % "0.5.7" cross CrossVersion.full),
        state))
    

    Then call it with sbt yourCustomCommand.

    The state instance you return from the command becomes the new state of the build, i.e. if you've added some dependencies, the build will see them.