英文:
Can I use one class to other project in scala multi project?
问题
I want to import classes in one project to the other project in scala multi-project.
Now My project structure:
Project
ᄂ project1
│ ᄂ src
│ ᄂ main
│ ᄂ scala
│ ᄂ com.foo1.bar
│ ᄂ aaa
│ ᄂ aaa1.scala
ᄂ project2
ᄂ src
ᄂ main
ᄂ scala
ᄂ com.foo2.bar
ᄂ bbb
ᄂ bbb1.scala
bbb1.scala :
class bbb1() {
def bFunction() = {
// some function
}
}
In this case, I want to import bbb1 in aaa1.scala file like below:
aaa1.scala
import com.foo2.bar.bbb._
class aaa1() {
def aFunction() = {
val t = bFunction()
}
}
I set the build.sbt dependsOn()
. But IDE highlight the import com.foo2.bar.bbb._
is wrong.
And If I run some test code, error raise.
I use IntelliJ and scala 2.12.12.
英文:
I want to import classes in one project to the other project in scala multi-project.
Now My project structure:
Project
ᄂ project1
│ ᄂ src
│ ᄂ main
│ ᄂ scala
│ ᄂ com.foo1.bar
│ ᄂ aaa
│ ᄂ aaa1.scala
ᄂ project2
ᄂ src
ᄂ main
ᄂ scala
ᄂ com.foo2.bar
ᄂ bbb
ᄂ bbb1.scala
bbb1.scala :
class bbb1() {
def bFunction() = {
// some function
}
}
In this case, I want to import bbb1 in aaa1.scala file like below:
aaa1.scala
import com.foo2.bar.bbb._
class aaa1() {
def aFunction() = {
val t = bFunction()
}
}
I set the build.sbt dependsOn()
. But IDE highlight the import com.foo2.bar.bbb._
is wrong.
And If I run some test code, error raise.
I use IntelliJ and scala 2.12.12.
答案1
得分: 2
Your build.sbt
should be:
lazy val project1 = project
.dependsOn(project2)
lazy val project2 = project
Don't forget package declarations package ...
in your sources.
In project1/src/main/scala/com/foo1/bar/aaa/aaa1.scala:
package com.foo1.bar.aaa
import com.foo2.bar.bbb._
class aaa1() {
def aFunction() = {
val t = new bbb1().bFunction()
}
}
In project2/src/main/scala/com/foo2/bar/bbb/bbb1.scala:
package com.foo2.bar.bbb
class bbb1() {
def bFunction() = {
// some function
}
}
Please note that packages and subprojects are different concepts. If bbb1
is a class, you should call bFunction
on an instance, e.g., new bbb1().bFunction()
. You can refer to the SBT documentation for more information: https://www.scala-sbt.org/1.x/docs/Multi-Project.html.
英文:
(Just for future readers, the beginning was here: https://stackoverflow.com/questions/75993396/how-to-packaging-specific-directory-in-scala)
Your build.sbt
should be
lazy val project1 = project
.dependsOn(project2)
lazy val project2 = project
Don't forget to re-import the project in IDE.
Don't forget package declarations package ...
in your sources
project1/src/main/scala/com/foo1/bar/aaa/aaa1.scala
package com.foo1.bar.aaa // <-- !!!
import com.foo2.bar.bbb._
class aaa1() {
def aFunction() = {
val t = new bbb1().bFunction()
}
}
project2/src/main/scala/com/foo2/bar/bbb/bbb1.scala
package com.foo2.bar.bbb // <-- !!!
class bbb1() {
def bFunction() = {
// some function
}
}
Packages and subprojects are different concepts: https://stackoverflow.com/questions/75700171/why-cant-sbt-assembly-find-the-main-classes-in-a-project-with-multiple-subproje
Please notice that if bbb1
is a class rather than object then you should call bFunction
on some instance, e.g. new bbb1().bFunction()
.
Sbt documentation: https://www.scala-sbt.org/1.x/docs/Multi-Project.html
While confused, you should rely on sbt clean
, sbt compile
, sbt run
, sbt test
rather than IntelliJ functionality (Ctrl+Alt+F10 etc.).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论