sbt 未观察到包名称

package name not observed by sbt

我编译了一个名为 AverageAgeByName.scala

的 Spark 程序的小 Scala 示例

这里是 build.sbt:

$ cd /opt/LearningSparkV2-master/chapter1/main/scala/chapter3
$ vim build.sbt
// Name of the package
name := "main/scala/chapter3"
// Version of our package
version := "1.0"
// Version of Scala
scalaVersion := "2.12.14"
// Spark library dependencies
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % "3.1.2",
"org.apache.spark" %% "spark-sql" % "3.1.2"
)

我运行命令:

$ sbt clean package
[info] Updated file /opt/LearningSparkV2 master/chapter1/main/scala/chapter3/project/build.properties: set sbt.version to 1.5.5
[info] welcome to sbt 1.5.5 (Oracle Corporation Java 1.8.0_242)
[info] loading project definition from /opt/LearningSparkV2-master/chapter1/main/scala/chapter3/project
[info] loading settings for project chapter3 from build.sbt ...
[info] set current project to main/scala/chapter3 (in build file:/opt/LearningSparkV2-master/chapter1/main/scala/chapter3/)
[success] Total time: 0 s, completed Aug 4, 2021 10:07:27 AM
[info] compiling 1 Scala source to /opt/LearningSparkV2-master/chapter1/main/scala/chapter3/target/scala-2.12/classes ...
[success] Total time: 9 s, completed Aug 4, 2021 10:07:36 AM

根据 build.sbt 中的包名称,结果 class 未放置在正确的目录层次结构中:

$ ls /opt/LearningSparkV2-master/chapter1/main/scala/chapter3/target/scala-2.12/classes
'AverageAgeByName$$typecreator1.class'  'AverageAgeByName$.class'   AverageAgeByName.class

它是平的。我希望 class 应该放在 /opt/LearningSparkV2-master/chapter1/main/scala/chapter3/target/scala-2.12/classes/main/scala/chapter3

我哪里弄错了?

build.sbt中的name := "main/scala/chapter3"与包或目标文件夹无关:它是将要使用的项目的名称例如,将项目打包为 JAR 时。

生成 classes 的文件夹由您在 Scala 文件中设置的 package 驱动 AverageAgeByName.scala.

以下将在 target/scala-2/classes/xxx/yyy 中生成 class 文件:

package xxx.yyy

class AverageAgeByName {}

另请注意,通常源文件也放在与您设置的包匹配的目录结构中。即上例中的src/main/scala/xxx/yyy

最后,您可能根本不应该关心 class 文件的生成位置。