5

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!

Community
  • 1
  • 1
Keren
  • 379
  • 6
  • 16
  • 2
    Take a look at sbt-buildinfo, it might help you – ahjohannessen Feb 02 '16 at 22:11
  • 1
    I tried to use that, but when I imported the package I got a weird error (I cannot remember what it was, it was a few hours ago). Edit: this error "sbt.ResolveException: unresolved dependency: com.eed3si9n#sbt-buildinfo;0.5.0: not found" – Keren Feb 02 '16 at 22:28
  • I use `MyClass.class.getPackage().getImplementationVersion()` for this. But it works only in a distribution - not during development. – Kris Feb 03 '16 at 17:22

2 Answers2

7

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

biesior
  • 55,576
  • 10
  • 125
  • 182
waterscar
  • 876
  • 1
  • 8
  • 14
  • So I didn't update, but I managed to get this working last week. Basically, I have to bring all plugins internal, to the project. The problem was mapping build-info internally to the application. Once it was brought in correctly and everything mapped out internally correctly (time consuming and dealing with paths), then was able to get quickly get it working. – Keren Feb 08 '16 at 17:10
1

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`
biesior
  • 55,576
  • 10
  • 125
  • 182