You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala新手求助:sbt项目如何优雅配置包目录结构?

Fixing sbt's Unrecognized Package Issue for Your Scala Project

Hey there! Let's get your sbt package setup sorted out—this is a common hiccup for new Scala developers, so you're not alone.

First off, your project structure is actually aligned with sbt's standard conventions, which means it should work out of the box. Let's break down what's probably going wrong, and the elegant fixes to get it running smoothly.

1. Double-Check Package Declarations & Directory Matching

sbt relies on your directory structure mirroring your package declarations. Let's confirm you have this right:

  • For myproject/src/main/scala/packagefolder/Fruit.scala, the top of the file must declare the package matching the directory:
    package packagefolder
    
    class Fruit {
      // Your class logic here
    }
    
  • For your package.scala in the same directory, the package object should match too:
    package object packagefolder {
      // Package-level variables/methods go here, e.g.:
      val defaultFruitName = "Apple"
    }
    
    Make sure there are no typos (case sensitivity matters here—PackageFolder vs packagefolder will break things!).

2. Correctly Import the Package in Main.scala

Since your Main.scala lives directly in src/main/scala (the root package), you need to explicitly import classes from packagefolder to use them:

// Main.scala
import packagefolder.Fruit
import packagefolder.defaultFruitName // To use the package object's value

object Main extends App {
  val myFruit = new Fruit()
  println(s"My default fruit is $defaultFruitName")
}

3. Verify sbt's Source Detection (No Extra Config Needed!)

sbt automatically scans all subdirectories under src/main/scala for .scala files—you shouldn't need that custom scalaSources snippet from the docs unless you're doing something non-standard.

To confirm sbt sees your package files, run this in the sbt console:

show compile:scalaSources

You should see paths to both Main.scala and the files in packagefolder listed. If they're missing, double-check your directory names for typos or case mismatches.

4. Custom Source Paths (Only If You Really Need Them)

If for some reason you do need to override sbt's default source behavior (again, not recommended for standard projects), add this to your build.sbt file in the project root:

Compile / scalaSources := {
  val baseSrcDir = baseDirectory.value / "src" / "main" / "scala"
  (baseSrcDir ** "*.scala").get
}

This replicates sbt's default behavior, so it's unlikely to fix your issue—but it's where you'd place that code if you needed to tweak it.

Wrap-Up

9 times out of 10, this issue boils down to a misaligned package declaration, typo, or missing import. Stick to sbt's standard directory structure (which you're already doing!) and double-check those basics, and your project should compile and run without a hitch.

内容的提问来源于stack exchange,提问作者Xud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:37:41