Created
May 8, 2020 13:44
-
-
Save mvillafuertem/a70618a4e4ed161afa32828997d246a6 to your computer and use it in GitHub Desktop.
run h2 console from sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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