gpt4 book ai didi

scala - 如何在自定义 sbt 任务中通过 application.conf 进行 Slick 配置?

转载 作者:行者123 更新时间:2023-12-04 12:30:22 25 4
gpt4 key购买 nike

我想创建一个设置任务,该任务创建一个光滑的数据库模式。为此,我的项目中有一个如下所示的任务对象:

object CreateSchema {

val instance = Database.forConfig("localDb")

def main(args: Array[String]) {
val createFuture = instance.run(createActions)
...
Await.ready(createFuture, Duration.Inf)
}

}

在我的 build.sbt我定义了一个任务:
lazy val createSchema = taskKey[Unit]("CREATE database schema")

fullRunTask(createSchema, Runtime, "sbt.CreateSchema")

当我运行 sbt createSchema 时,它会按预期执行从命令行。

然而,问题在于 application.conf似乎没有被考虑在内(我也尝试过不同的范围,如 CompileTest )。结果,任务失败,原因是 com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'localDb' .

如何解决此问题以便配置可用?

我在这里发现了很多关于使用 application.conf 的问题。内 build.sbt本身,但这不是我需要的。

最佳答案

我使用 SBT 0.13.8 和 Slick 3.0.0 设置了一个小演示,它按预期工作。 (即使没有修改“-Dconfig.resource”。)

文件

./build.sbt

name := "SO_20150915"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.3.0" withSources() withJavadoc(),
"com.typesafe.slick" %% "slick" % "3.0.0",
"org.slf4j" % "slf4j-nop" % "1.6.4",
"com.h2database" % "h2" % "1.3.175"
)

lazy val createSchema = taskKey[Unit]("CREATE database schema")

fullRunTask(createSchema, Runtime, "somefun.CallMe")

./project/build.properties
sbt.version = 0.13.8

./src/main/resources/reference.conf
hello {
world = "buuh."
}

h2mem1 = {
url = "jdbc:h2:mem:test1"
driver = org.h2.Driver
connectionPool = disabled
keepAliveConnection = true
}

./src/main/scala/somefun/CallMe.scala
package somefun

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import slick.driver.H2Driver.api._

/**
* SO_20150915
* Created by martin on 15.09.15.
*/
object CallMe {
def main(args: Array[String]) : Unit = {
println("Hello")
val settings = new Settings()

println(s"Settings read from hello.world: ${settings.hw}" )

val db = Database.forConfig("h2mem1")
try {
// ...
println("Do something with your database.")
} finally db.close
}

}

class Settings(val config: Config) {

// This verifies that the Config is sane and has our
// reference config. Importantly, we specify the "di3"
// path so we only validate settings that belong to this
// library. Otherwise, we might throw mistaken errors about
// settings we know nothing about.
config.checkValid(ConfigFactory.defaultReference(), "hello")

// This uses the standard default Config, if none is provided,
// which simplifies apps willing to use the defaults
def this() {
this(ConfigFactory.load())
}

val hw = config.getString("hello.world")
}

结果

如果正在运行 sbt createSchema从控制台我获得输出
[info] Loading project definition from /home/.../SO_20150915/project
[info] Set current project to SO_20150915 (in build file:/home/.../SO_20150915/)
[info] Running somefun.CallMe
Hello
Settings read from hello.world: buuh.
Do something with your database.
[success] Total time: 1 s, completed 15.09.2015 10:42:20

想法
  • 请确认这个未经修改的演示项目也适用于您。
  • 然后尝试在演示项目中更改 SBT 版本,看看是否有什么改变。
  • 或者,重新检查您的项目设置并尝试使用更高版本的 SBT。
  • 关于scala - 如何在自定义 sbt 任务中通过 application.conf 进行 Slick 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32277012/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com