Keren
Keren

Reputation: 539

Play Framework: Reading Version from Build.sbt

I've been seeing a bunch of questions about how to read a version from build.sbt, and there have been a lot of work-arounds provided for how to point build.sbt to conf/application.conf and have the version specified in conf/application.conf instead.

I have a Configuration object that needs to get in the version. I currently have it set up like this (How get application version in play framework and build.sbt), where the Configuration objects from application.conf. However, I'd still like to get it directly from build.sbt. How can I do that? Should I perhaps run a bash command on the build.sbt file to get the version from there? Any suggestions?

Thanks!

Upvotes: 6

Views: 1483

Answers (2)

waterscar
waterscar

Reputation: 876

We managed to get information via build-info, here's some detail configuration.

  1. Add plugins (we also include sbt-git since we want git version as well) into project/plugins.sbt

    addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.4")
    addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.5.0")
    
  2. Configure plugins in build.sbt

    enablePlugins(BuildInfoPlugin)
    
    enablePlugins(GitVersioning)
    
    buildInfoKeys := Seq[BuildInfoKey](organization, name, version, BuildInfoKey.action("gitVersion") {       
       git.formattedShaVersion.?.value.getOrElse(Some("Unknown")).getOrElse("Unknown") +"@"+ git.formattedDateVersion.?.value.getOrElse("")        
    })
    
    buildInfoPackage := "version"
    
  3. Within views (*.html.scala), display those info simply by

    Version @version.BuildInfo.version - build @version.BuildInfo.gitVersion
    
  4. Or you can just using all values from BuildInfo in your java or scala code, by calling version.BuildInfo.XX

Upvotes: 7

biesior
biesior

Reputation: 55798

Maybe not quite what you want but some time ago I used such workaround (I needed version for additional deployment steps): I created file like app-version.cfg in the main app directory, so I could use it within build.sbt like:

version := scala.io.Source.fromFile("app-version.cfg").mkString

and in Unix bash:

version=`cat app-version.cfg`

Upvotes: 2

Related Questions