gpt4 book ai didi

scala - 从 build.sbt 和应用程序代码访问对象

转载 作者:行者123 更新时间:2023-12-04 07:29:12 27 4
gpt4 key购买 nike

我需要访问一个 build.sbt应用程序代码中的变量,或者定义一些可以从 build.sbt 访问的类/对象和应用程序代码。这该怎么做?

例如,build.sbt :

propName := "hello"
MyApp.scala :
buildSbtProvider.getVariable("propName")

或者 build.sbt :
propName := CommonObject.hello
MyApp.scala :
propName = CommonObject.hello

最佳答案

您可能想使用 sbt-buildinfo 插件。

如文档中所述,您只需在构建中添加一些定义

lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin).
settings(
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
buildInfoPackage := "hello"
)

这将生成以下类,您可以在重新加载后使用它:
package hello

import java.io.File
import java.lang._
import java.net.URL
import scala._; import Predef._

/** This object was generated by sbt-buildinfo. */
case object BuildInfo {
/** The value is "helloworld". */
val name: String = "helloworld"
/** The value is "0.1-SNAPSHOT". */
val version: String = "0.1-SNAPSHOT"
/** The value is "2.10.3". */
val scalaVersion: String = "2.10.3"
/** The value is "0.13.2". */
val sbtVersion: String = "0.13.2"
override val toString: String = "name: %s, version: %s, scalaVersion: %s, sbtVersion: %s" format (name, version, scalaVersion, sbtVersion)
}

您可以引用 README项目文件以获取更多详细信息。

关于scala - 从 build.sbt 和应用程序代码访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782647/

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