SBT 在程序中包含版本号 [英] SBT including the version number in a program
问题描述
我希望我正在构建的程序能够在运行时报告自己的版本(例如 scala myprog.jar --version
).传统上,在 maven 项目中,我会使用资源过滤(pom.xml -> file.properties -> 在运行时读取值).我知道有 sbt-filter-plugin 来模拟这个功能,但我很好奇是否有在 SBT 中执行此操作的更标准/首选/聪明的方法.
tl;dr 如何在运行时读取 build.sbt
中定义的版本号?
更新...
https://github.com/ritschwumm/xsbt-reflect(上面提到的)是过时了,但有一个很酷的 SBT 发布工具,可以自动管理版本等等:https://github.com/sbt/sbt-release.
或者,如果您想要快速修复,您可以像这样从清单中获取版本:
val 版本:String = getClass.getPackage.getImplementationVersion
此值将等于您在 build.sbt
或 Build.scala
中设置的项目中的 version
设置.>
另一个更新...
Buildinfo SBT 插件可以根据build.sbt
生成带有版本号的类:
/** 该对象由 sbt-buildinfo 生成.*/案例对象 BuildInfo {/** 值为helloworld".*/val 名称:String = "helloworld"/** 值为0.1-SNAPSHOT".*/val 版本:String = "0.1-SNAPSHOT"/** 值为2.10.3".*/val scalaVersion: String = "2.10.3"/** 值为0.13.2".*/val sbtVersion: String = "0.13.2"override val toString: String = "name: %s, version: %s, scalaVersion: %s, sbtVersion: %s" 格式(name, version, scalaVersion, sbtVersion)}
在此处查看有关如何启用它的文档:https://github.com/sbt/sbt-buildinfo/.
I want a program I'm building to be able to report its own version at runtime (e.g. scala myprog.jar --version
). Traditionally in a maven project, I'd use resource filtering (pom.xml -> file.properties -> read value at runtime). I know there's sbt-filter-plugin to emulate this functionality, but I'm curious if there's a more standard / preferred / clever way of doing this in SBT.
tl;dr how can I read the version number defined in build.sbt
at runtime?
Update...
https://github.com/ritschwumm/xsbt-reflect (mentioned above) is Obsolete, but there is this cool SBT release tool that can automatically manage versions and more: https://github.com/sbt/sbt-release.
Alternatively, if you want a quick fix you can get version from manifest like this:
val version: String = getClass.getPackage.getImplementationVersion
This value will be equal to version
setting in your project which you set either in build.sbt
or Build.scala
.
Another Update ...
Buildinfo SBT plugin can generate a class with version number based on build.sbt
:
/** 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)
}
See the docs on how to enable it here: https://github.com/sbt/sbt-buildinfo/.
这篇关于SBT 在程序中包含版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!