sbt custom task using a class in project

sbt, scala

Solution

I need to know why you want to call the methods, since this changes the answer. If you want to do something ...

Build related

Want to use class/ methods which do something build related ( minfiy things, uploaded jar/wars to servers, etc..)

- you have to put this in a plugin or you need to put the sources in a project folder.

- The code cannot be called from your project

If it is build related, someone has probably dealt with a similar problem and there is probably a sbt plugin already, but if not, then let me know and I can explain creating sbt plugins.

Non-build related

Want to just call / test methods which don't have anything to do with the build cycle.

- You can put this in an object in your project called Script (name it whatever), start up console and then import script object and run.

- To make this even easier you can make a custom import script for the console which imports all scripts automatically, which you can then run

So for example, package script

object Script {
    def foo = println("I am doing things non-build related")
}

in sbt now run

console
>> import script._
>> foo    // prints out  "I am doing things non-build related"

Problem

How can I add a custom task to sbt build definition that consumes (uses classes, runs methods etc.) the project sources ? It seems it tries to find them before compiling even.

Original source