Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sbt 0.11 run task examples needed

Tags:

scala

sbt

My projects are still using sbt 0.7.7 and I find it very convenient to have utility classes that I can run from the sbt prompt. I can also combine this with properties that are separately maintained - typically for environment related values that changes from hosts to hosts. This is an example of my project definition under the project/build directory:

class MyProject(info: ProjectInfo) extends DefaultProject(info) {
  //...
  lazy val extraProps = new BasicEnvironment {
    // use the project's Logger for any properties-related logging
    def log = MyProject.this.log
    def envBackingPath = path("paths.properties")
    // define some properties that will go in paths.properties
    lazy val inputFile = property[String]
  }

  lazy val myTask = task { args =>
    runTask(Some("foo.bar.MyTask"),
      runClasspath, extraProps.inputFile.value :: args.toList).dependsOn(compile)
      describedAs "my-task [options]"
  }   
}

I can then use my task as my-task option1 option2 under the sbt shell.

I've read the new sbt 0.11 documentation at https://github.com/harrah/xsbt/wiki including the sections on Tasks and TaskInputs and frankly I'm still struggling on how to accomplish what I did on 0.7.7.

It seems the extra properties could simply be replaced a separate environment.sbt, that tasks have to be defined in project/build.scala before being set in build.sbt. It also looks like there is completion support, which looks very interesting.

Beyond that I'm somewhat overwhelmed. How do I accomplish what I did with the new sbt?

like image 867
huynhjl Avatar asked Nov 11 '11 18:11

huynhjl


1 Answers

You can define a task like this :

val myTask = InputKey[Unit]("my-task")

And your setting :

val inputFile = SettingKey[String]("input-file", "input file description")

You can also define a new configuration like :

lazy val ExtraProps = config("extra-props") extend(Compile)

add this config to your project and use it to set settings for this configuration :

lazy val root = Project("root", file(".")).config( ExtraProps ).settings(
  inputFile in ExtraProps := ...
  ...
  myTask in ExtraPops <<= inputTask { (argTask:TaskKey[Seq[String]]) =>
    (argTask, inputFile) map { (args:Seq[String], iFile[String]) =>
      ...
    }
  }
).dependsOn(compile)

then launch your task with extra-props:my-task

like image 184
David Avatar answered Nov 14 '22 09:11

David