Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a custom task in project/Build.scala in sbt?

Tags:

sbt

With the following task declaration in project/Build.scala, the print task is not recognised when I type in print at an SBT console.

lazy val print = task { println("print") }

What's wrong?

like image 272
Nick Avatar asked Jun 17 '13 06:06

Nick


People also ask

How do I create a Scala project in sbt?

Delegate a Scala project build to sbt Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Build Tools | sbt. In the sbt projects section, select a project for which you want to configure build actions. In the sbt shell section, select the builds option. Click OK to save the changes.

How do we specify library dependencies in sbt?

The libraryDependencies key Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.


Video Answer


2 Answers

You need a TaskKey for this to work that can be instantiated by using the taskKey macro:

lazy val printTask = taskKey[Unit]("print")

I recommend having a look at the corresponding documentation about tasks. The documentation says:

The name of the val is used when referring to the task in Scala code. The string passed to the TaskKey method is used at runtime, such as at the command line

like image 134
Christian Avatar answered Nov 15 '22 08:11

Christian


taskKey[Unit]("print") := println("print")

Then in your SBT console,

> print
print

In more complex code, you'll usually see keys separate from the settings.

val printTask = taskKey[Unit]("print")

printTask := println("print")
like image 31
Paul Draper Avatar answered Nov 15 '22 10:11

Paul Draper