Skip to content

Instantly share code, notes, and snippets.

@mvillafuertem
Created May 8, 2020 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvillafuertem/a70618a4e4ed161afa32828997d246a6 to your computer and use it in GitHub Desktop.
Save mvillafuertem/a70618a4e4ed161afa32828997d246a6 to your computer and use it in GitHub Desktop.
run h2 console from sbt
// Method 1 Create a task
// It can throw java.util.NoSuchElementException: head of empty list if you don't have h2 dependency
lazy val h2Console = taskKey[Int]("Init H2 console task")
h2Console := (Compile / dependencyClasspathAsJars)
.value
.map(_.data)
.filter(_.getPath.contains("h2database"))
.map(h2database => Process(s"java -jar ${h2database} org.h2.tools.Server").!)
.head
// Method 2 Create a command
val h2Command = Command.command("h2Console") {
state =>
Project.runTask(Compile / dependencyClasspathAsJars, state)
.get
._2
.toEither
.fold(
exception => exception.printStackTrace(),
value => value.map(_.data).filter(_.getPath.contains("h2database")).map(file => Process(s"java -jar ${file} org.h2.tools.Server").!).head
)
state
}
commands += h2Command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment