我对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,以便将来遇到类似问题的人。
答案 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
宏是首选方法。