nemoo
nemoo

Reputation: 3319

SBT: Continue even if a certain step fails

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.

Upvotes: 0

Views: 407

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 159995

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"""") 

Upvotes: 1

Related Questions