Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggered Execution in SBT 0.13.x

Tags:

sbt

I am using SBT 0.13.2 and I want to trigger my task by the way of execution of the compile task. I know that I can achieve this by using the triggeredBy method, as in taskDefinition.triggeredBy(compile in Compile).

I have a build.sbt with my custom task defined.

The problem is that I cannot understand why the following works as expected

val triggeredTask = taskKey[Unit]("Triggered by compile")

triggeredTask <<= Def.task {
  println("TRIGGERED BY COMPILE")
}.triggeredBy(compile in Compile)

but the next does not work (compile executes just fine but my task is not triggered)

val triggeredTask = taskKey[Unit]("Triggered by compile")

triggeredTask := Def.task {
  println("TRIGGERED BY COMPILE")
}.triggeredBy(compile in Compile).value

My understanding was that SBT 0.13 made <<= obsolete and := should be sufficient.

like image 370
lpiepiora Avatar asked May 03 '14 14:05

lpiepiora


1 Answers

I might misunderstand what you are trying to do.

If I make your example more verbose it becomes like this:

val triggeredTaskKey = taskKey[Unit]("Triggered by compile")

val anonymousTask = Def.task {
  println("TRIGGERED BY COMPILE")
}.triggeredBy(compile in Compile)

// I imagine the macro doing something like this
triggeredTaskKey <<= anonymousTask map (identity)

This also does not trigger the anonymous task on compile.

Note that <<= is a gloried version of set that extracts position information. And set is simply an alias for Def.setting(key, value, position) which in turn creates a new Setting instance. Note that the setting instance that is created internally has a constructor of those 3 parameters.

I do not think there is a way in sbt to declare a setting without a key (I might be wrong here). It seems however that you want to create a setting (that is triggered) without a key.

This might be caused by a bug, I am not sure if sbt should recursively walk the dependencies to find tasks that have a triggeredBy key in their info. It currently however is not (as seen in the trigger method.

Theoretically you could make a work around that creates settings with the correct triggers bound to some arbitrary key. I am not sure how that would work.

like image 155
EECOLOR Avatar answered Sep 20 '22 09:09

EECOLOR