如何读取我自己的sbt插件中定义的修改设置
我试图通过以下方式创建自己的sbt插件,代码如下: myplugin中的如何读取我自己的sbt插件中定义的修改设置,sbt,Sbt,我试图通过以下方式创建自己的sbt插件,代码如下: myplugin中的build.sbt sbtPlugin := true lazy val plugin = (project in file(".")). settings( name := "myplugin", version := "0.1-SNAPSHOT", scalaVersion := "2.10.4" ) package sbthello import sbt._ import Keys
build.sbt
sbtPlugin := true
lazy val plugin = (project in file(".")).
settings(
name := "myplugin",
version := "0.1-SNAPSHOT",
scalaVersion := "2.10.4"
)
package sbthello
import sbt._
import Keys._
object HelloPlugin extends AutoPlugin {
object autoImport {
val greeting = settingKey[String]("greeting")
val obfuscate = taskKey[String]("Obfuscates files.")
}
import autoImport._
lazy val baseSettings: Seq[Def.Setting[_]] = Seq(
greeting := "Hi!",
obfuscate := {
println(greeting.value)
greeting.value + " value"
}
)
override val projectSettings =
inConfig(Compile)(baseSettings)
}
myplugin中的HelloPlugin.scala
sbtPlugin := true
lazy val plugin = (project in file(".")).
settings(
name := "myplugin",
version := "0.1-SNAPSHOT",
scalaVersion := "2.10.4"
)
package sbthello
import sbt._
import Keys._
object HelloPlugin extends AutoPlugin {
object autoImport {
val greeting = settingKey[String]("greeting")
val obfuscate = taskKey[String]("Obfuscates files.")
}
import autoImport._
lazy val baseSettings: Seq[Def.Setting[_]] = Seq(
greeting := "Hi!",
obfuscate := {
println(greeting.value)
greeting.value + " value"
}
)
override val projectSettings =
inConfig(Compile)(baseSettings)
}
测试项目中的build.sbt
lazy val usage = (project in file("."))
.enablePlugins(HelloPlugin).
settings(
name := "sbt-test",
version := "0.1",
scalaVersion := "2.10.4")
.settings(
greeting := "Hello"
)
使用上述文件,执行结果为:
> show obfuscate
Hi!
[info] Hi! value
> show greeting
[info] Hello
模糊任务无法读取“Hello”值
如果将HelloPlugin.scala中的问候语:=“嗨!”
修改为模糊问候语:=“嗨!”,
模糊任务现在可以读取“Hello”值
> show greeting
[info] Hello
> show obfuscate
Hello
[info] Hello value
但现在我无法删除buildInDemo.sbt中的问候语:=“Hello”
,否则它将出现以下错误:
[error] References to undefined settings:
[error]
[error] compile:greeting from compile:obfuscate ((sbthello.HelloPlugin) HelloPlugin.scala:40)
[error] Did you mean compile:obfuscate::greeting ?
[error]
[error] compile:greeting from compile:obfuscate ((sbthello.HelloPlugin) HelloPlugin.scala:40)
[error] Did you mean compile:obfuscate::greeting ?
[error]
配置范围
覆盖val项目设置=
inConfig(编译)(基本设置)
这将baseSettings
中的所有设置范围扩展到Compile
配置中。所以这和说:
在Compile
设置中定义问候语
请参阅编译中的模糊处理任务中的编译中的问候语设置
因此,在build.sbt
中,您应该按照
编译中的问候语:=“你好”
您可以通过以下方式从shell访问此设置:
> compile:greeting
如果您不想要该行为,请不要将baseSettings
放在inConfig(…)
中
任务和配置范围
模糊中的问候语是任务范围界定。因此,与图(…)中的相结合将需要
问候语(编译,模糊):=“你好”
定制
有关更多详细信息,请参阅。非常感谢,尤金。我应该做的是将“override val projectSettings=inConfig(Compile)(baseSettings)”更改为“override val projectSettings=baseSettings”。