Scala 如何向SBT项目添加示例?
我想在Scala库中添加一些示例。我更喜欢这样的目录布局:Scala 如何向SBT项目添加示例?,scala,sbt,project-structure,Scala,Sbt,Project Structure,我想在Scala库中添加一些示例。我更喜欢这样的目录布局: examples/ example1/ Main.scala ... example2/ Main.scala ... src/ main/ scala/ ... test/ scala/ ... build.sbt 示例使用src/main/scala/中的包
examples/
example1/
Main.scala
...
example2/
Main.scala
...
src/
main/
scala/
...
test/
scala/
...
build.sbt
示例使用
src/main/scala/
中的包。是否可以使用SBT构建和运行示例?也可能有特定于示例的依赖项吗?也可能,但您可能需要使用complete.scala定义
以下是展示:
// [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
}
您可能希望将示例作为一个“子项目”,它取决于主项目。从sbt 0.13开始,您可以在
build.sbt
中定义val
s、lazy val
s和子项目。您不需要Build.scala
。请看@sjrd cool,我不知道这一点(我通常避免使用0.13,因为它有一些bug)