Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make SBT task fail and hence build itself?

Tags:

sbt

I wrote a SBT task to run cssLint for my project using rhino. cssLint returns exit code to my SBT task.

My question is how to make the task fail if the exit code is non-zero?

I don't want to throw any exceptions. I want my last line of the task result to show [Failed] instead of [success] and exit code of my SBT task to be non-zero.

SAMPLE

MyTask {
  val exitcode = //rhino functions

  //what to do??
}

The actual intent is to fail the build if css errors are present.

like image 904
Arun Kumar Sundaramurthy Avatar asked Feb 15 '14 11:02

Arun Kumar Sundaramurthy


2 Answers

My understanding is that the success message is printed out always unless

  • showSuccess setting is set to false or
  • a task throws an exception.

In your particular case you want to report an error and so you should throw an exception or a value of the type of the result that might be considered a sort of exception like None or Failure.

Say, you've got the following task defined in build.sbt:

lazy val tsk = taskKey[Unit]("Task that always fails")

tsk := {
  throw new IllegalStateException("a message")
}

When you execute the tsk task, the exception is printed out with no [success] afterwards.

[no-success]> tsk
[trace] Stack trace suppressed: run last *:tsk for the full output.
[error] (*:tsk) java.lang.IllegalStateException: a message
[error] Total time: 0 s, completed Feb 15, 2014 11:45:27 PM

I would rather prefer avoiding this style of programming and rely on Option as a way to report an issue with processing.

With the following tskO definition:

lazy val tskO = taskKey[Option[String]]("Task that reports failure as None")

tskO := None

you'd then check the result and if it's None you'd know it's a failure.

like image 131
Jacek Laskowski Avatar answered Jan 01 '23 18:01

Jacek Laskowski


The way of failing the build without producing the stacktrace on the console is using the exceptions that are specifically handled:

  • for sbt.MessageOnlyException an error message is logged twice (without task name and then with task name) and the build is stopped
  • mix in sbt.FeedbackProvidedException or sbt.UnprintableException to implement custom exceptions for which sbt does not print a stacktrace. The string with task name and exception's toString is logged on the top level once and the build is stopped. It is expected that essential information for the user is already logged before throwing these.

Disclaimer: I've not seen this information in sbt manual. Extracted from the sources of sbt 0.13.16. sbt.FeedbackProvidedException is used this way by sbt compiler, sbt tests and by sbt-web and Play sbt plugins.

like image 23
Konstantin Pelepelin Avatar answered Jan 01 '23 19:01

Konstantin Pelepelin