簡體   English   中英

如何在sbt項目中使用來自其他位置的Scala源並將其與IntelliJ IDEA一起使用?

[英]How can I use scala sources from a different location in a sbt project and also make it work with IntelliJ IDEA?

該項目使用sbt編譯並運行良好。 https://github.com/stuhajbejovic/multi-project-sbt

我遇到的問題是當我以intellij idea打開hello.sbt項目時,它說:

cannot resolve symbol testing

當我將鼠標懸停在hello.scala第1行上的紅色import語句上時。

我也嘗試在build.sbt中使用它:

unmanagedSourceDirectories in Compile += baseDirectory.value / "../common"

但是隨后我在IntelliJ中收到以下警告,並且無法在IntelliJ中運行該應用程序:

這些源根目錄不能包含在IDEA項目模型中。

解決方案:為這些源聲明一個SBT項目,並將該項目包含在依賴項中。

有沒有辦法讓IntelliJ遵循sbt配置到常見源?

您需要使用一個多項目SBT構建文件。 也就是說,對於整個項目只有一個build.sbt文件。 IntelliJ應該很榮幸地做到這一點。

我認為應該將build.sbt文件放在項目的根目錄中,如下所示:

lazy val commonSettings = Seq(
  scalaVersion := "2.12.1",
  version := "0.1"
)

lazy val common = project.in(file("sbt_testing/common")).
settings(commonSettings: _*).
settings(
  name := "common"
)

lazy val hello = project.in(file("sbt_testing/hello")).
dependsOn(common).
settings(commonSettings: _*).
settings(
  name := "Hello"
)

如您所見,您可以將兩個項目共有的設置分組,也可以確保它們之間更好的一致性。

您將需要在sbt_testing/commonsbt_testing/hello刪除build.sbt文件。

更新 :糾正了SBT build.sbt文件中commonSettings使用。 抱歉造成混亂!

更新2 :為了運行HelloWorld類中的代碼,您需要執行以下操作(我還將“ root”項目重命名為“ hello”):

$ sbt
[info] Loading global plugins from /home/user/.sbt/0.13/plugins
[info] Loading project definition from /home/user/src/multiSbt/project
[info] Set current project to multisbt (in build file:/home/user/src/multiSbt/)
> project hello
[info] Set current project to Hello (in build file:/home/user/src/multiSbt/)
> run
[info] Compiling 1 Scala source to 
/home/user/src/multiSbt/sbt_testing/hello/target/scala-2.12/classes...
[info] Running HelloWorld 
Hello, world!
This is from common: testing 123
[success] Total time: 3 s, completed May 1, 2017 3:27:16 PM

有關SBT多項目構建的更多信息,請參閱官方文檔 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM