0
votes

During the sbt build of my scala program i write the current sha1 hash to a file so i can use it easily from the application. This is how it looks like in my build.sbt file:

val dummy =  {
   val sha1 = Process("git rev-parse HEAD").lines.head    
   IO.write(file("conf/version.conf"), s"""sha1="$sha1"""")  
}

The problem ist that now the build has the dependency that git command line has to be installed, otherwise it will fail since it cannot execute the git command.

Is it possible in sbt to ignore an error that occurs during the build and somehow just take "unknown" as the sha1 hash? The sbt docs say something about "failures" http://www.scala-sbt.org/0.13.5/docs/Detailed-Topics/Tasks.html but i am not sure if this can be applied to my problem.

1
Why not just wrap the sha1 assignment in a try? val sha1 = try { /* */ } catch { case e: NonFatal => "unknown" } - Sean Vieira
Yes, that works! I keep forgetting that i can use plain scala in sbt. - nemoo

1 Answers

1
votes

sbt files are normal Scala files in most regards. Simply assign to sha1 the result of a try / catch expression:

val sha1 = try {
    Process("git rev-parse HEAD").lines.head
  } catch { case e: NonFatal => "unknown" }
IO.write(file("conf/version.conf"), s"""sha1="$sha1"""")