mickiewicz
mickiewicz

Reputation: 289

How to define a new setting and set its value for a task?

I've a myTask task that invokes other tasks as follows:

def myTask = Task <<= (Task1, Task2, Task3) map {(_,_,_)=>;}

Task1, Task2, Task3 take a tcWebApp config variable that is a directory.

 tcWebApp := file("../tomcat")

Everything works fine.

What I need now is to create another task myTask2 that'd be similar to myTask, but I'd like to invoke this task with another directory set for the tcWebApp setting, i.e. the setting should have another value for the task. Is it possible?

I've tried something like

tcWebApp in myTask2 := file("newDir") 

but it didn't work. Please advice.

Upvotes: 2

Views: 230

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30498

When you write:

`tcWebApp in myTask2` := ...

It doesn't mean "while myTask2 is executing, tcWebApp has the following value," as you want it to. What it does mean is, "if anyone asks myTask2 what value it has for tcWebApp, it will reply as follows." It doesn't have any effect on the global value of tcWebApp; and if nobody ever asks myTask2 what its value for tcWebApp is, then setting it in that task has no effect at all. So Task1 will continue to use the global value of tcWebApp.

I found some related questions on Stack Overflow:

Here Daniel Sobral writes "From what I understand from your question, you want the setting to be different for a dependency depending on what is depending on it. This doesn't make sense -- a dependency either is satisfied or it isn't, and what depends on it doesn't come into the equation." As I understand it, that is the answer to your question.

In order to work around this, instead of attempting to reuse Task1 and Task2 as tasks, reuse the code inside them instead. Have Task1 and Task2 invoke ordinary methods that you define, and then have myTask2 call those same methods, passing them different parameters. In other words don't try to solve your problem with settings; solve it by means of ordinary Scala code.

Or, here's another approach you could take. If you make myTask2 a command rather than a task, you can do what you want. See http://www.scala-sbt.org/release/docs/Extending/Commands.html which says "a command can look at or modify other sbt settings".

Upvotes: 2

Related Questions