Home > Enterprise >  Execute sbt task with modified dependency
Execute sbt task with modified dependency

Time:10-04

I want to create a custom runLocal task that executes the sbt run task with modified unmanagedClasspath.
I want the unmanagedClasspath modification to only be visible/last while running runLocal, not run.
What I've tried in build.sbt:

Runtime / unmanagedClasspath   = Seq(new java.io.File("src/main/my_resources")).classpath
val runLocal = taskKey[Unit]("Run app with my config")
runLocal := {
  (Runtime / run).toTask("").value
}

The above works but the problem is that the modification of unmanagedClasspath is "global" and affects every task that uses this value.
How can I run runLocal with modified unmanagedClasspath that is not visible outside that task?

CodePudding user response:

What I ended up doing is using a Command.
Commands have access to the state and are able to modify it.

val runLocal = Command.command("runLocal") { state =>
  val extracted = Project.extract(state)
  val localConfigClasspath = Seq(new java.io.File("src/main/my_resources")).classpath
  val newState = extracted.appendWithoutSession(Seq(Runtime / unmanagedClasspath   = localConfigClasspath), state)
  Project.extract(newState).runInputTask(Runtime / run, "", newState)._1
}

This way the config is only changed while running sbt runLocal without affecting sbt run.

  • Related