现在的位置: 首页 > 综合 > 正文

sbt学习笔记

2013年07月05日 ⁄ 综合 ⁄ 共 3067字 ⁄ 字号 评论关闭

1、自动生成build.sbt文件

$sbt

>set name := "anjsoft" (:= 表示 sbt.Setting["name"]="anjsoft")

>session save

2、

ModuleID = groupId % artifact % version % configuration

"org.specs2" %% "specs2" % "1.6.1"
% "test" 返回的类型为 ModuleID ,查更多关于依赖库的方法到 http://harrah.github.io/xsbt/latest/api/#sbt.ModuleID

3、所有增加到lib目录的jar文件被加到classpath中,在 compile ,test, run, console...可用

4、

libraryDependencies += groupID % artifactID % revision

 % configuration  (+=  看到加号 就到示左边是 Seq[] ,具休这里是 Seq[ModuleID])

5 、增加一个

dependency resolver

resolvers += "Repository name" at "http://the-repository/

releases"

增加本地maven repo 

resolvers += "Local Mvn Repository" at

 "file://"+Path.userHome.absolutePath+"/.m2/repository"

解决冲突 

libraryDependencies += "slinky" % "slinky" % "2.1" from  "http://slinky2.googlecode.com/svn/artifacts/2.1/ slinky.jar"

        Sbt does not resolve dependencies with
classifier: https://github.com/harrah/xsbt/issues/285

  intransitive() or notTransitive()比如:

libraryDependencies += "org.apache.felix" % "org.apache.felix.framework" % "1.8.0" intransitive()

libraryDependencies += 
  "log4j" % "log4j" % "1.2.15" exclude("javax.jms", "jms")
ivyXML :=
  <dependencies>
    <dependency org="javax.mail" name="mail" rev="1.4.2">
      <exclude module="activation"/>
    </dependency>
  </dependencies>

5、发布 

publishTo := Some("Scala Tools Nexus" at "http://mydomain.org/content/repositories/releases/")

>publish

credentials += Credentials(Path.userHome / ".ivy2" /  ".credentials")

6、修发settings

:= 替换

+= 增加一个值

++=增加多个值

~=转换一个现有值  name ~= { _.toUpperCase }

修改靠依赖的有 <<=  eg. 

organization <<= name(_.toUpperCase)

name <<= (name, version)( _ + "-" + _ )

<+=

cleanFiles <+= (name) { n => file(.) / (n + ".log") }

watchSources <+= (baseDirectory, name) map{(dir, n) =>

          dir / "conf" / (n + ".properties")

}

7.定义个task

val time = TaskKey[Date]("time", "returns current

  time")

      lazy val root = Project("test", file(".")).settings(

          time := {

              val now = new Date()

              println("%s".format(now))

              now

})

> time

8、sbt api 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

其它

启动 http://www.scala-sbt.org/release/docs/Detailed-Topics/Launcher.html

http://harrah.github.io/xsbt/latest/api/#sbt.SettingKey

To test some build code snippets in a scala REPL(this will load all build dependencies):

console-project
We can load build definition project with(): 
reload plugins
and go back to project with():
  reload return
project definition: /build.sbt和 project/Build.scala
build definition :project/plugins.sbt和project/project/Build.scala
Inspect Settings - general informations:
inspect compile

Transforming a value: ~= 过滤自己
Computing a value based on other keys' values: <<= 靠别人改变自己(给别人起了个别名)
+= will append a single element to the sequence.
++= will concatenate another sequence.
Appending with dependencies: <+= and <++=

<  Initialize[Id[V]] 参考其它值(依赖)(<<=   )   <+=(< 的意思与+的组合)
+  单个值增加 Seq[T]   (+=)
++ 多个值增加到 Seq[T]   (++=)
def ++=
[U](values: ⇒ U)(implicit a: Values[T, U]): Setting[Id[T]]
def +=
[U](value: ⇒ U)(implicit a: Value[T, U]): Setting[Id[T]]
final def :=
(value: ⇒ T): Setting[T]
def <++=
[V](values: Initialize[Id[V]])(implicit a: Values[T, V]): Setting[Id[T]]
def <+=
[V](value: Initialize[Id[V]])(implicit a: Value[T, V]): Setting[Id[T]]
final def   <<= (app: Initialize[T]): Setting[T]

抱歉!评论已关闭.