[英]How can I use scala sources from a different location in a sbt project and also make it work with IntelliJ IDEA?
This project compiles and runs fine using sbt. 该项目使用sbt编译并运行良好。 https://github.com/stuhajbejovic/multi-project-sbt https://github.com/stuhajbejovic/multi-project-sbt
The problem I'm having is when I open the hello.sbt project in intellij idea, it says: 我遇到的问题是当我以intellij idea打开hello.sbt项目时,它说:
cannot resolve symbol testing
when I hover over the red import statement on line 1 of hello.scala
. 当我将鼠标悬停在hello.scala
第1行上的红色import语句上时。
I also tried using this in my build.sbt: 我也尝试在build.sbt中使用它:
unmanagedSourceDirectories in Compile += baseDirectory.value / "../common"
but then I get the following warning in IntelliJ and can't run the app inside IntelliJ: 但是随后我在IntelliJ中收到以下警告,并且无法在IntelliJ中运行该应用程序:
These source roots cannot be included in the IDEA project model. 这些源根目录不能包含在IDEA项目模型中。
Solution: declare an SBT project for these sources and include the project in dependencies. 解决方案:为这些源声明一个SBT项目,并将该项目包含在依赖项中。
Is there a way to get IntelliJ to follow sbt configuration to where the common sources are? 有没有办法让IntelliJ遵循sbt配置到常见源?
You need to use a multi-project SBT build file. 您需要使用一个多项目SBT构建文件。 That is, have one build.sbt
file for the entire project. 也就是说,对于整个项目只有一个build.sbt
文件。 IntelliJ should honor that without a problem. IntelliJ应该很荣幸地做到这一点。
I think your build.sbt
file, which should go in your project's root directory, would need to look like this: 我认为应该将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"
)
As you can see, you can group settings common to both projects and also ensure better consistency between them. 如您所见,您可以将两个项目共有的设置分组,也可以确保它们之间更好的一致性。
You will need to remove the build.sbt
files in sbt_testing/common
and sbt_testing/hello
. 您将需要在sbt_testing/common
和sbt_testing/hello
删除build.sbt
文件。
UPDATE : Corrected use of commonSettings
in the SBT build.sbt
file. 更新 :纠正了SBT build.sbt
文件中commonSettings
使用。 Apologies for the confusion! 抱歉造成混乱!
UPDATE 2 : In order to run the code in the HelloWorld
class, you will need to do the following (I also renamed the "root" project to "hello"): 更新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
For more information on SBT multi-project builds, refer to the official documentation ... 有关SBT多项目构建的更多信息,请参阅官方文档 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.