我想要一个我正在构建的程序,以便能够在运行时报告自己的版本(例如scala myprog.jar --version
)。传统上在maven项目中,我使用资源过滤(pom.xml - > file.properties - >在运行时读取值)。我知道有sbt-filter-plugin来模仿这个功能,但我很好奇是否有更标准/首选/聪明的方法在SBT中这样做。
tl; dr如何在运行时读取build.sbt
中定义的版本号?
答案 0 :(得分:23)
...更新
https://github.com/ritschwumm/xsbt-reflect(如上所述)已过时,但有一个很酷的SBT发布工具可以自动管理版本及更多:https://github.com/sbt/sbt-release。
或者,如果您想快速修复,可以从清单中获取版本:
val version: String = getClass.getPackage.getImplementationVersion
此值将等于您在version
或build.sbt
中设置的项目中的Build.scala
设置。
另一次更新......
Buildinfo SBT插件可以生成基于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/。
答案 1 :(得分:7)
使用xsbt-reflect插件。它将生成一个源文件,其中包含项目版本号。
答案 2 :(得分:3)
一般来说,没有任何插件,你可以这样做:
from data import tankOBJ as tank
with open(os.path.join(os.path.join(APP_ROOT, '/csv tester/motherwell.csv')),'r',newline='') as output_file:
fieldnames = ['id','name','product','temperature','density','timestamp','water']
writer = csv.Dictwriter(output_file, fieldnames=fieldnames)
csv_writer.writeheader()
for line in csv_reader:
del line['shortname','state','shape','pos_x','pos_y','datum',',max_work','min_work','offset_bottom','offset_volume','offset_level','roof_density','roof_weight','roof_start','roof_end','shell_insulated','shell_cte','shell_temperature','dimension_a','dimension_b','dimension_c','dimension_d','knee_forth','knee_third','knee_second','knee_first','suction_forth,','suction_third','suction_second','suction_first','rundown_forth','rundown_third','rundown_second','rundown_first','suction_low_forth','suction_low_third','suction_low_second','suction_low_first','rundown_low_forth','rundown_low_third','rundown_low_second','rundown_low_first','sll','slu','svl','suv','swlv','swuv','swll','swul','volume','innage','flow','pressure','maximum_volume',]
writer.writerow(tank)
return 'Hello World!'
然后用sourceGenerators in Compile += Def.task {
val file = (sourceManaged in Compile).value / "foo" / "bar" / "BuildInfo.scala"
IO.write(
file,
s"""package foo.bar
|object BuildInfo {
| val Version = "${version.value}"
|}""".stripMargin
)
Seq(file)
}.taskValue
做任何你想做的事。
或者更一般:
foo.bar.BuildInfo.Version
示例:
def generateBuildInfo(packageName: String,
objectName: String = "BuildInfo"): Setting[_] =
sourceGenerators in Compile += Def.task {
val file =
packageName
.split('.')
.foldLeft((sourceManaged in Compile).value)(_ / _) / s"$objectName.scala"
IO.write(
file,
s"""package $packageName
|object $objectName {
| val Version = "${version.value}"
|}""".stripMargin
)
Seq(file)
}.taskValue
您甚至可以更改此项以将对象属性作为settings(generateBuildInfo("foo.bar"))
传递并适当地生成对象。
答案 3 :(得分:1)
我最终制作了构建系统(我在Makefile
之上使用sbt
)准备一个src/main/resources/version.txt
文件供Scala阅读。
在Makefile
:
$(RESOURCES_VERSION): build.sbt
grep "^version := " $< | cut -f2 -d\" > $@
在Scala中:
val version: String = {
val src = Source.fromURL( getClass.getResource("/version.txt") )
src.getLines.next // just take the first line
}
这对我有用。
很奇怪Scala中不容易获得这样一个所需的功能(我认为)。我们欢迎使用一个非常简单的sbt
插件。