2014-07-04 23 views
2

我想添加一些例子到我的Scala庫。我更喜歡這樣的目錄佈局:如何將示例添加到SBT項目?

examples/ 
    example1/ 
     Main.scala 
     ... 
    example2/ 
     Main.scala 
     ... 
src/ 
    main/ 
     scala/ 
      ... 
    test/ 
     scala/ 
      ... 
build.sbt 

這些示例使用src/main/scala/中的軟件包。是否可以使用SBT構建和運行示例?是否也可以具有特定於示例的依賴項?

+1

你可能想使例子「子項目」,這依賴於主體工程。 – Ryan

回答

3

這是可能的,但你可能不得不求助於完整的.scala定義。 I've put example project on bitbucket

這裏是展示:

// [info] Loading global plugins from /Users/omnomnom/.sbt/0.13/plugins 
// [info] Loading project definition from /Users/omnomnom/projects/example-of-examples/project 
// [info] Set current project to root-project (in build file:/Users/omnomnom/projects/example-of-examples/) 
> projects 
// [info] In file:/Users/omnomnom/projects/example-of-examples/ 
// [info]  example1 
// [info]  example2 
// [info] * root-project 
> project example1 
// [info] Set current project to example1 (in build file:/Users/omnomnom/projects/example-of-examples/) 
> run 
// [info] Updating {file:/Users/omnomnom/projects/example-of-examples/}root-project... 
// [info] Resolving org.fusesource.jansi#jansi;1.4 ... 
// [info] Done updating. 
// [info] Updating {file:/Users/omnomnom/projects/example-of-examples/}example1... 
// [info] Resolving org.fusesource.jansi#jansi;1.4 ... 
// [info] Done updating. 
// [info] Compiling 1 Scala source to /Users/omnomnom/projects/example-of-examples/target/scala-2.10/classes... 
// [info] Running Main 
I'm example #1 
// [success] Total time: 4 s, completed Jul 4, 2014 10:17:54 PM 
> project example2 
// [info] Set current project to example2 (in build file:/Users/omnomnom/projects/example-of-examples/) 
> run 
// [info] Updating {file:/Users/omnomnom/projects/example-of-examples/}example2... 
// [info] Resolving org.fusesource.jansi#jansi;1.4 ... 
// [info] Done updating. 
// [info] Compiling 1 Scala source to /Users/omnomnom/projects/example-of-examples/examples/example2/target/scala-2.10/classes... 
// [info] Running Main 
I'm example #2 
// [success] Total time: 3 s, completed Jul 4, 2014 10:18:04 PM 

而且Build.scala,公正的情況下:

import sbt._ 
import Keys._ 

object Build extends Build { 

    override lazy val settings = super.settings ++ Seq(
    scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xlint"), 
    organization := "me.lazyval", 
    scalaVersion := "2.10.4", 
    initialCommands in console := "import me.lazyval._" 
) 

    // altering source path, since you don't want to replicate usual src/main ... stuff 
    val exampleSourcePath = scalaSource in Compile := baseDirectory.value/"." 

    lazy val root = Project(id = "root-project", base = file("."), settings = Project.defaultSettings ++ settings) 

    lazy val example1 = Project(id = "example1", base = file("./examples/example1"), settings = Project.defaultSettings ++ settings ++ Seq(exampleSourcePath)) dependsOn root 
    // in `settings= ...` section you can set whatever dependencies you like 
    lazy val example2 = Project(id = "example2", base = file("./examples/example2"), settings = Project.defaultSettings ++ settings ++ Seq(exampleSourcePath)) dependsOn root 
} 
+1

從sbt 0.13開始,你可以在'build.sbt'中定義'val's,'lazy val'和子項目。你不需要'Build.scala'。見http://www.scala-sbt.org/0.13/tutorial/Multi-Project.html – sjrd

+0

@sjrd酷,不知道這一點(我一般避免使用0.13,因爲它有一些錯誤) –