如何将全局设置的值设置为单例对象方法?

时间:2016-07-14 08:07:31

标签: scala sbt

我对sbt有以下问题。从sbt控制台我可以获得baseDirectory全局设置的值。

> baseDirectory
[info] /home/georginaumov/Documents/hello
> 

我在build.sbt

中添加了一个任务
lazy val printBaseDirectory: TaskKey[Unit] = TaskKey[Unit]("printBaseDirectory", "Print baseDirectory for the project", KeyRanks.ATask)
printBaseDirectory <<= streams map Tasks.printBaseDirectory

这是Tasks singleton对象的代码。

import sbt.Keys.TaskStreams
import sbt._

object Tasks {
  def printBaseDirectory(streams: TaskStreams): Unit = {
    streams.log.info("Here I want to print value of baseDirectory")
  }
}

但我无法获得价值。我尝试了很多东西,实际上问题是我无法从sbt.SettingKey [java.io.File]获取java.io.File。 如何解决问题?

我试过

printBaseDirectory <<= streams map Tasks.printBaseDirectory(baseDirectory)

def printBaseDirectory(baseDir: sbt.File)(streams: TaskStreams): Unit = {
    streams.log.info("Here I want to print value of baseDirectory")
  }

进入单例对象但得到以下错误:

error: type mismatch;
[error] Type error in expression
 found   : sbt.SettingKey[java.io.File]
 required: sbt.File
    (which expands to)  java.io.File

编辑:

非常感谢马丁。我在我的博客上写了article,以便将来遇到类似问题的人。

1 个答案:

答案 0 :(得分:1)

我认为你想要的是将Tasks定义为:

import sbt.Keys.TaskStreams
import sbt._

object Tasks {
  def printBaseDirectory(streams: TaskStreams, dir: File): Unit = {
    streams.log.info(dir.getAbsolutePath)
  }
}

build.sbt

lazy val printBaseDirectory: TaskKey[Unit] = TaskKey[Unit]("printBaseDirectory", "Print baseDirectory for the project", KeyRanks.ATask)
printBaseDirectory <<= (streams, baseDirectory) map Tasks.printBaseDirectory

或者:

lazy val printBaseDirectory: TaskKey[Unit] = TaskKey[Unit]("printBaseDirectory", "Print baseDirectory for the project", KeyRanks.ATask)
printBaseDirectory := {
  Tasks.printBaseDirectory(streams.value, baseDirectory.value)
}

使用.value宏是首选方法。