Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 520
Keren Avatar asked Feb 02 '16 17:02

Keren


People also ask

What is build framework sbt?

The build. sbt file defines settings for your project. You can also define your own custom settings for your project, as described in the sbt documentation. In particular, it helps to be familiar with the settings in sbt. To set a basic setting, use the := operator: confDirectory := "myConfFolder"

How can I change sbt version?

Create or open your sbt project. In the Project tool window, in the source root directory, locate the build. properties file and open it in the editor. In the editor explicitly specify the version of sbt that you want to use in the project.

Is Play framework still used?

Play is rock-solid and used by hundreds of thousands of Java and Scala developers every month. Play is still extremely relevant to today's application and web development and has a passionate and very capable community around it ensuring that it has many good years left.

What is play framework in Scala?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.


2 Answers

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

like image 83
waterscar Avatar answered Sep 30 '22 03:09

waterscar


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`
like image 40
biesior Avatar answered Sep 30 '22 02:09

biesior