Freewind @ Thoughtworks scala java javascript dart 工具 编程实践 月结 math python english [comments admin] [feed]

(2014-09-13) build.sbt中有哪些操作符可用

广告: 云梯:翻墙vpn (省10元) 土行孙:科研用户翻墙http proxy (有优惠)

:=

给一个Key设置为一个新值

name := "Hello"

+=

给一个Seq类型的key追加一个值:

libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.3.2"

++=

给一个Seq类型的key追加多个值:

libraryDependencies += Seq(
    "org.apache.commons" % "commons-lang3" % "3.3.2",
    "org.scalaz" %% "scalaz-core" % "7.1.0"
)

~=

在key已有的值的基础上,修改为一个新值:

name ~= { n => n + "!!!" }

<<=

在一个key中使用另一个key的值

使用原值

假设已经定义了一个key:

name := "Hello" 

现在想在另一个key中直接使用它的值:

anotherKey <<= name

使用旧值

如果想在另一个值的基础上生成一个新值,可以:

anotherKey <<= name { n => n + "!!!"}

同时使用多个值

anotherKey2 <<= (name, anotherKey) { (n,a) => n + a }

可用来判断一个key是否有值

lazy val unintiedKey = settingKey[String]("Unitialized key")

lazy val someKey = settingKey[String]("Key to check the value of another")

someKey := unintiedKey.?.value getOrElse "new value"

这时someKey的值是:

new value

但是如果已经给unintiedKey赋了值,比如:

unintiedKey := "Another value"

那么someKey的值将会是:

Another value

??

?有一点相似:

lazy val unintiedKey = settingKey[String]("Unitialized key")

lazy val someKey = settingKey[String]("Key to check the value of another")

someKey := (unintiedKey ?? "uninitedKey had no value").value

someKey的值是:

uninitedKey had no value

more

从文档上看到还有这一些奇怪的操作符,不知道有什么用。

<++=
<+=

不过万能的stackoverflow告诉我,自从sbt 0.13有了.value以后,就再也不需要它们了。

http://stackoverflow.com/questions/25819886/where-are-examples-of-in-build-sbt

comments powered by Disqus