如何在playFramework 2.3.x中的子项目中访问父项目的类

时间:2015-10-10 09:36:50

标签: java eclipse scala playframework-2.3 sbt-0.13

我正在使用游戏2.3.8并使用this gudie在我的项目中创建子项目我创建了子项目' mySubProject'然后我在eclipse中导入项目父项目myParentProjectmySubProject现在我有两个问题 第一 - >这是正确的,首先我在eclipse中导入myParentProject然后导入mySubProject 二线>在我的mySubProject我可以访问myParentProject的类并导入其包 但在mySubProject我希望访问myParentProject的类/包时,我不会让它显示错误Object not found 这是根项目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"
)

这里是子项目mySubProject

的构建文件
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+")

这是代码 在我的父项目myParentProject中我创建了一个名为app / myPackagae / abc.scala的类,这里是代码

  package myPackagae
import mySubProject._
class abc {
def helloAbc()={
  println(" i am root project and  i am class abc ")
}
val test=new Testing
test.helloTesting()
}

并在mySubProject我在/mySubProject/src/main/scala-2.11/mySubProject/Testing.scala中创建了一个类 这是代码

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
}

请指导我如何在子项目中导入root项目的包/类

1 个答案:

答案 0 :(得分:0)

这是一个古老的问题,但仍被认为可以给出答案。

TLDR

通常避免将根项目导入子项目,除非您确定根项目不依赖于子项目(例如,您可以将根路由设置为子模块,这是一个依赖项)。

详细答案

下面是我关注的项目结构:

- root  
  - app
    - controllers
    - models
- modules
  - moduleone
     - build.sbt
     - app
       - controllers.moduleone
       - models.moduleone
       - ...
  - moduletwo
    - build.sbt
    - app
      - controllers.moduletwo
      - ...
- build.sbt

下面是根项目build.sbt文件。 请注意子项目的dependsOn设置,它基本上将根项目作为依赖项添加到子项目,因此您可以将根项目类导入子项目。

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
  )

在子项目中,您可以执行以下操作:

package models.moduleone

import models.SOMETHING_FROM_ROOT_PROJECT

...

将根项目和子项目定义为不同的包是一个好主意,以使导入更加清晰。

还请注意,您应该避免子项目和根项目之间的依赖循环。我见过一些项目,其中根项目依赖于子项目的某些功能。在这种情况下,您将为根项目添加dependsOn,但是如果您的子项目也做同样的事情,它将无法编译。

一个更常见的情况是,您通常最终会在根路由中这样做:

-> /api/moduleone moduleone.Routes

这会将相关流量路由到moduleone。现在,需要做的是首先编译moduleone,然后在根项目中使用该路由,这意味着根build.sbt需要为:

val root: Project = project.in(file("."))
  .enablePlugins(PlayScala)
  .dependsOn(moduleone)
  .aggregate(moduleone)
  .settings(
    libraryDependencies ++= common
  )

如果您随后将根项目导入到moduleone中,它将变成一个循环,永远不会编译。