Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT including the version number in a program

Tags:

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?

like image 911
Chris Eberle Avatar asked Jan 21 '12 21:01

Chris Eberle


People also ask

How can I get sbt version?

Check with sbt --script-version . the default SBT version (=sbt-launcher JAR version), decided primarily by the project SBT version (in project/build. properties ), secondarily by the launcher script itself. Check with sbt --script-version (or sbtVersion in the SBT shell)

Which version of Scala does sbt use?

sbt uses that same version of Scala to compile the build definitions that you write for your project because they use sbt APIs. This version of Scala is fixed for a specific sbt release and cannot be changed. For sbt 1.7. 1, this version is Scala 2.12.

What is ThisBuild in sbt?

Typically, if a key has no associated value in a more-specific scope, sbt will try to get a value from a more general scope, such as the ThisBuild scope. This feature allows you to set a value once in a more general scope, allowing multiple more-specific scopes to inherit the value.

What version of Java is sbt using?

sbt is a build tool for Scala, Java, and more. It requires Java 1.8 or later.


2 Answers

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/.

like image 180
yǝsʞǝla Avatar answered Nov 08 '22 09:11

yǝsʞǝla


Use the xsbt-reflect plugin. It will generate a source file that contains, among other things, the project version number.

like image 22
Michael Ekstrand Avatar answered Nov 08 '22 07:11

Michael Ekstrand