![](/img/trans.png)
[英]How to attach sources to scala sbt project at 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/common
和sbt_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.