Reputation: 3485
I am using play 2.3.8 and using this gudie to create sub project in my project i created subproject 'mySubProject' and then i imported the project in eclipse the parent project myParentProject
and mySubProject
now i have two questions
first -> is this correct that first i imported myParentProject
in eclipse then i imported mySubProject
Second-> in my mySubProject
i can access the classes of myParentProject
and import its packages
but in mySubProject
when i want to access the class/packages of my myParentProject
its does not let me do that it show error Object not found
here is the build file of root project myParentProject
name := """myParentProject"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
.aggregate(mySubProject)
.dependsOn(mySubProject)
scalaVersion := "2.11.1"
fork in run := true
javaOptions in run ++= Seq("-J-Xms1G", "-J-Xmx2G")
val appDependencies = Seq(
// Add your project dependencies here,
"org.scalatestplus" %% "play" % "1.2.0" % "test"
)
lazy val mySubProject = project
libraryDependencies ++= Seq("org.scalatest" %% "scalatest" % "2.2.1" % "test"withSources() withJavadoc(),
"com.esotericsoftware.kryo" % "kryo" % "2.10",
"org.mongodb" %% "casbah" % "2.8.0",
"org.slf4j" % "slf4j-api" % "1.6.4",
"org.elasticsearch" % "elasticsearch" % "1.6.0",
"org.codehaus.groovy" % "groovy-all" % "2.4.0",
"org.apache.lucene" % "lucene-expressions" % "4.10.4",
"org.scalatest" %% "scalatest" % "2.2.1" % "test"withSources() withJavadoc(),
"org.easymock" % "easymock" % "3.1" withSources() withJavadoc(),
"org.mockito" % "mockito-all" % "1.9.5",
"com.typesafe.akka" %% "akka-actor" % "2.3.6",
"ch.qos.logback" % "logback-core" % "1.0.9",
"com.github.nscala-time" %% "nscala-time" % "2.0.0",
"net.liftweb" %% "lift-json" % "2.6+",
"net.liftweb" %% "lift-json" % "2.6+",
"com.hazelcast" % "hazelcast" % "3.5",
"com.hazelcast" % "hazelcast-client" % "3.5",
"com.twitter" % "chill-bijection_2.11" % "0.7.0"
//,"com.codahale" % "jerkson_2.9.1" % "0.5.0"
)
and here is build file of child projectmySubProject
name := """mySubProject"""
version := "1.0-SNAPSHOT"
scalaVersion := "2.11.1"
libraryDependencies ++= Seq("org.scalatest" %% "scalatest" % "2.2.1" % "test"withSources() withJavadoc(),
"org.slf4j" % "slf4j-api" % "1.6.4",
"org.elasticsearch" % "elasticsearch" % "1.6.0",
"org.codehaus.groovy" % "groovy-all" % "2.4.0",
"org.apache.lucene" % "lucene-expressions" % "4.10.4",
"org.easymock" % "easymock" % "3.1" withSources() withJavadoc(),
"org.mockito" % "mockito-all" % "1.9.5",
"com.typesafe.akka" %% "akka-actor" % "2.3.6",
"ch.qos.logback" % "logback-core" % "1.0.9",
"com.github.nscala-time" %% "nscala-time" % "2.0.0",
"net.liftweb" %% "lift-json" % "2.6+",
"net.liftweb" %% "lift-json" % "2.6+")
and here is the code
in my parent project myParentProject
i created a class named app/myPackagae/abc.scala here is the code
package myPackagae
import mySubProject._
class abc {
def helloAbc()={
println(" i am root project and i am class abc ")
}
val test=new Testing
test.helloTesting()
}
and in mySubProject
i created a class in /mySubProject/src/main/scala-2.11/mySubProject/Testing.scala
here is the code
package mySubProject
//import parentProjectPackage._
import myPackagae._ //here is an error not found: object myPackagae
class Testing {
def helloTesting() ={
println("i am a subproject or child project and i am class Testing")
}
//and here i want to access class abc and its method helloAbc() but eclipse is not importing
}
please guide me how can i import packages/classes of root project in child project
Upvotes: 1
Views: 882
Reputation: 288
This is an old question but thought to pose an answer still.
TLDR:
Generally avoid importing root project into subproject unless you are sure root project does not depends on subproject (for example you may set root route to submodule and this is an dependency).
Detailed Answer:
Below is the project structure I am following:
- root
- app
- controllers
- models
- modules
- moduleone
- build.sbt
- app
- controllers.moduleone
- models.moduleone
- ...
- moduletwo
- build.sbt
- app
- controllers.moduletwo
- ...
- build.sbt
Below is the root project build.sbt file. Note the dependsOn setting for sub project, it basically add the root project as dependency to the subproject and thus you can import root project classes into subproject.
lazy val modelone: Project = project.in(file("modules/moduleone"))
.enablePlugins(PlayScala)
.settings(
name := "module-one",
libraryDependencies ++= common
)
.dependsOn(root)
////*******************************
//// Root module
////*******************************
val root: Project = project.in(file("."))
.enablePlugins(PlayScala)
.settings(
name := "root",
libraryDependencies ++= common
)
In your subproject then you can do:
package models.moduleone
import models.SOMETHING_FROM_ROOT_PROJECT
...
It's a good idea to define root project and subproject into different packages so that imports are more clearer.
Also note you should avoid dependency cycle between subproject and root project. I've seen projects where root project relies on subproject for some functionality. In this case you will then add dependsOn
for root project, but if your subproject also did the same thing, it will not compile.
A more common case is you often end up doing this in your root routes:
-> /api/moduleone moduleone.Routes
This routes related traffic to moduleone. Now, what play needs to do is to first compile moduleone and then use that route in root project, which means, the root build.sbt needs to be:
val root: Project = project.in(file("."))
.enablePlugins(PlayScala)
.dependsOn(moduleone)
.aggregate(moduleone)
.settings(
libraryDependencies ++= common
)
If you are then importing root project into moduleone, it becomes a cycle, which will never compile.
Upvotes: 1