你可以在Scala多项目中使用一个类到另一个项目吗?

huangapple go评论80阅读模式
英文:

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:

  1. Project
  2. project1
  3. src
  4. main
  5. scala
  6. com.foo1.bar
  7. aaa
  8. aaa1.scala
  9. project2
  10. src
  11. main
  12. scala
  13. com.foo2.bar
  14. bbb
  15. bbb1.scala

bbb1.scala :

  1. class bbb1() {
  2. def bFunction() = {
  3. // some function
  4. }
  5. }

In this case, I want to import bbb1 in aaa1.scala file like below:

aaa1.scala

  1. import com.foo2.bar.bbb._
  2. class aaa1() {
  3. def aFunction() = {
  4. val t = bFunction()
  5. }
  6. }

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:

  1. Project
  2. project1
  3. src
  4. main
  5. scala
  6. com.foo1.bar
  7. aaa
  8. aaa1.scala
  9. project2
  10. src
  11. main
  12. scala
  13. com.foo2.bar
  14. bbb
  15. bbb1.scala

bbb1.scala :

  1. class bbb1() {
  2. def bFunction() = {
  3. // some function
  4. }
  5. }

In this case, I want to import bbb1 in aaa1.scala file like below:

aaa1.scala

  1. import com.foo2.bar.bbb._
  2. class aaa1() {
  3. def aFunction() = {
  4. val t = bFunction()
  5. }
  6. }

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:

  1. lazy val project1 = project
  2. .dependsOn(project2)
  3. lazy val project2 = project

Don't forget package declarations package ... in your sources.

In project1/src/main/scala/com/foo1/bar/aaa/aaa1.scala:

  1. package com.foo1.bar.aaa
  2. import com.foo2.bar.bbb._
  3. class aaa1() {
  4. def aFunction() = {
  5. val t = new bbb1().bFunction()
  6. }
  7. }

In project2/src/main/scala/com/foo2/bar/bbb/bbb1.scala:

  1. package com.foo2.bar.bbb
  2. class bbb1() {
  3. def bFunction() = {
  4. // some function
  5. }
  6. }

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

  1. lazy val project1 = project
  2. .dependsOn(project2)
  3. 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

  1. package com.foo1.bar.aaa // <-- !!!
  2. import com.foo2.bar.bbb._
  3. class aaa1() {
  4. def aFunction() = {
  5. val t = new bbb1().bFunction()
  6. }
  7. }

project2/src/main/scala/com/foo2/bar/bbb/bbb1.scala

  1. package com.foo2.bar.bbb // <-- !!!
  2. class bbb1() {
  3. def bFunction() = {
  4. // some function
  5. }
  6. }

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.).

huangapple
  • 本文由 发表于 2023年4月13日 18:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定