简体   繁体   中英

Set task settings from build.sbt

I am writing a small sbt plugin to generate some files which should be configurable by a target path parameter. Therefore I wrote this plugin code:

object GeneratorPlugin extends AutoPlugin {
  object autoImport {
    val targetPath = settingKey[String]["target directory"]
    val generateFiles = taskKey[Unit]["generate files"]
  }

  import autoImport._

  override def trigger = allRequirements

  override lazy val buildSettings = Seq(
    targetPath := ".",
    generateFiles := generateTask
  )

  lazy val generateTask = Def.task {
    System.out.println(targetPath.value)
  }
}

When importing this using addSbtPlugin in project/plugins.sbt and running it with sbt generateFiles is correctly printing . . However when I change the value of targetPath in my build.sbt the result does not change.

targetPath := "/my/new/path"

Result of sbt generateFiles is still . . Is there a way to change the value of targetPath within my build.sbt when importing the plugin?

You can change it like so:

targetPath in ThisBuild := "/my/new/path"

or in the sbt 1.1's new slash syntax

ThisBuild / targetPath := "/my/new/path"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM