SBT 包括程序中的版本号 [英] SBT including the version number in a program

查看:15
本文介绍了SBT 包括程序中的版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我正在构建的程序能够在运行时报告它自己的版本(例如 scala myprog.jar --version).传统上,在 maven 项目中,我会使用资源过滤(pom.xml -> file.properties -> 在运行时读取值).我知道有 sbt-filter-plugin 来模拟这个功能,但我很好奇是否有在 SBT 中执行此操作的更标准/首选/聪明的方式.

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 如何在运行时读取 build.sbt 中定义的版本号?

tl;dr how can I read the version number defined in build.sbt at runtime?

推荐答案

更新...

https://github.com/ritschwumm/xsbt-reflect(上面提到的)是过时了,但是有一个很酷的 SBT 发布工具,可以自动管理版本等等:https://github.com/sbt/sbt-release.

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

此值将等于您在 build.sbtBuild.scala 中设置的项目中的 version 设置.

This value will be equal to version setting in your project which you set either in build.sbt or Build.scala.

另一个更新...

Buildinfo SBT插件可以根据build.sbt生成一个带有版本号的类:

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)
}

在此处查看有关如何启用它的文档:https://github.com/sbt/sbt-buildinfo/.

See the docs on how to enable it here: https://github.com/sbt/sbt-buildinfo/.

这篇关于SBT 包括程序中的版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆