为什么我的 sourceDirectories 设置在 sbt 中不起作用? [英] Why does my sourceDirectories setting have no effect in sbt?

查看:27
本文介绍了为什么我的 sourceDirectories 设置在 sbt 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 SBT 入门 指南.该页面以 sourceDirectories 设置为例.为了自己尝试,我将以下内容放在了我的 build.sbt 文件中,用于一个基本的hello world"项目.

I'm reading the SBT Getting Started guide. That page shows the sourceDirectories setting as an example. To try it myself, I put the following in my build.sbt file, for a basic "hello world" project.

sourceDirectories in Compile := Seq(file("other"))

(附加 +=++= 也不起作用.)

(Appending with += or ++= doesn't work either.)

我在 /other/src/main/scala 中有源文件.我运行 sbt 并输入 compile.它忽略了设置:

I have source files in <base-dir>/other and in <base-dir>/src/main/scala. I ran sbt and typed compile. It ignored the setting:

> compile
[info] Compiling 1 Scala source to /Learning/Scala/proj1/target/scala-2.9.2/classes...
[error] /Learning/Scala/proj1/src/main/scala/hello.scala:7: not found: value Foo
[error]     println(Foo.foo)

Foo 对象定义在 other 源目录的 foo.scala 中.

The Foo object is defined in foo.scala in the other source directory.

我错过了什么?

推荐答案

tl;dr @schleichardt 的回答是一个有效的 - 您需要将以下内容添加到 build.sbt 文件中:

tl;dr The answer by @schleichardt is a valid one - you need to add the following to the build.sbt file:

unmanagedSourceDirectories in Compile += file("other")

解释归结为检查 compile 任务的依赖关系.使用 inspect tree compile 找出它们.

The explanation boils down to inspecting the dependencies of the compile task. Use inspect tree compile to find them out.

[proj1]> inspect tree compile
[info] compile:compile = Task[sbt.inc.Analysis]
[info]   +-compile:compile::compileInputs = Task[sbt.Compiler$Inputs]
[info]   | +-compile:scalacOptions = Task[scala.collection.Seq[java.lang.String]]
[info]   | +-compile:sources = Task[scala.collection.Seq[java.io.File]]
[info]   | +-*/*:sourcePositionMappers = Task[scala.collection.Seq[scala.Function1[xsbti.Position, scala.Option[xsbti.Position]]]]
[info]   | +-*/*:maxErrors = 100
[info]   | +-*:compilers = Task[sbt.Compiler$Compilers]
[info]   | +-compile:compile::streams = Task[sbt.std.TaskStreams[sbt.Init$ScopedKey[_ <: Any]]]
[info]   | | +-*/*:streamsManager = Task[sbt.std.Streams[sbt.Init$ScopedKey[_ <: Any]]]
[info]   | |
[info]   | +-compile:incCompileSetup = Task[sbt.Compiler$IncSetup]
[info]   | +-*/*:compileOrder = Mixed
[info]   | +-compile:dependencyClasspath = Task[scala.collection.Seq[sbt.Attributed[java.io.File]]]
[info]   | +-compile:classDirectory = target/scala-2.10/classes
[info]   | +-*/*:javacOptions = Task[scala.collection.Seq[java.lang.String]]
[info]   |
[info]   +-compile:compile::streams = Task[sbt.std.TaskStreams[sbt.Init$ScopedKey[_ <: Any]]]
[info]     +-*/*:streamsManager = Task[sbt.std.Streams[sbt.Init$ScopedKey[_ <: Any]]]
[info]

如您所见,该任务依赖于 compile:sources 任务.该任务又取决于使用 compile:unmanagedSourceDirectories 设置的 compile:unmanagedSources 任务.

As you can see, the task depends on the compile:sources task. The task in turn depends on the compile:unmanagedSources task that uses the compile:unmanagedSourceDirectories setting.

[proj1]> inspect tree compile:sources
[info] compile:sources = Task[scala.collection.Seq[java.io.File]]
[info]   +-compile:unmanagedSources = Task[scala.collection.Seq[java.io.File]]
[info]   | +-compile:unmanagedSourceDirectories = List(/Users/jacek/sandbox/stackoverflow/proj1/src/main/scala, /Users/jacek/sandbox/stackoverflow/proj1/src/main/java, /Users/..
[info]   | | +-compile:javaSource = src/main/java
[info]   | | | +-compile:sourceDirectory = src/main
[info]   | | |   +-*:sourceDirectory = src
[info]   | | |   | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1
[info]   | | |   |   +-*:thisProject = Project(id: proj1, base: /Users/jacek/sandbox/stackoverflow/proj1, aggregate: List(), dependencies: List(), configurations: List(compile..
[info]   | | |   |
[info]   | | |   +-compile:configuration = compile
[info]   | | |
[info]   | | +-compile:scalaSource = src/main/scala
[info]   | | | +-compile:sourceDirectory = src/main
[info]   | | |   +-*:sourceDirectory = src
[info]   | | |   | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1
[info]   | | |   |   +-*:thisProject = Project(id: proj1, base: /Users/jacek/sandbox/stackoverflow/proj1, aggregate: List(), dependencies: List(), configurations: List(compile..
[info]   | | |   |
[info]   | | |   +-compile:configuration = compile
[info]   | | |
[info]   | | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1
[info]   | |   +-*:thisProject = Project(id: proj1, base: /Users/jacek/sandbox/stackoverflow/proj1, aggregate: List(), dependencies: List(), configurations: List(compile, runt..
[info]   | |
[info]   | +-*/*:excludeFilter = sbt.SimpleFileFilter@7ced8ea7
[info]   | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1
[info]   | +-*/*:unmanagedSources::includeFilter = sbt.SimpleFilter@1beb6bba
[info]   | +-*/*:sourcesInBase = true
[info]   |
[info]   +-compile:managedSources = Task[scala.collection.Seq[java.io.File]]
[info]     +-compile:sourceGenerators = List()
[info]

compile:unmanagedSourceDirectories 设置引用了非托管源目录,其中包含手动创建的源."

[proj1]> inspect compile:unmanagedSourceDirectories
[info] Setting: scala.collection.Seq[java.io.File] = List(/Users/jacek/sandbox/stackoverflow/proj1/src/main/scala, /Users/jacek/sandbox/stackoverflow/proj1/src/main/java, /Users/jacek/sandbox/stackoverflow/proj1/other)
[info] Description:
[info]  Unmanaged source directories, which contain manually created sources.
[info] Provided by:
[info]  {file:/Users/jacek/sandbox/stackoverflow/proj1/}proj1/compile:unmanagedSourceDirectories
[info] Defined at:
[info]  (sbt.Defaults) Defaults.scala:161
[info]  /Users/jacek/sandbox/stackoverflow/proj1/build.sbt:1
[info] Dependencies:
[info]  compile:javaSource
[info]  compile:scalaSource
[info]  *:baseDirectory
[info] Reverse dependencies:
[info]  compile:unmanagedSources
[info]  compile:sourceDirectories
[info] Delegates:
[info]  compile:unmanagedSourceDirectories
[info]  *:unmanagedSourceDirectories
[info]  {.}/compile:unmanagedSourceDirectories
[info]  {.}/*:unmanagedSourceDirectories
[info]  */compile:unmanagedSourceDirectories
[info]  */*:unmanagedSourceDirectories
[info] Related:
[info]  test:unmanagedSourceDirectories

设置为 scala.collection.Seq[java.io.File] 类型并且要向 seq 附加一个元素,您可以使用 += 方法.

The setting is of the scala.collection.Seq[java.io.File] type and to append an element to the seq, you can use += method.

话虽如此,build.sbt文件应该如下:

Having said that, the build.sbt file should be as follows:

unmanagedSourceDirectories in Compile += (baseDirectory / "other").value

您不必使用 baseDirectory 设置,但它使基本目录显式.

You don't have to use the baseDirectory setting, but it makes the base directory explicit.

这篇关于为什么我的 sourceDirectories 设置在 sbt 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆