Sbt:从根项目调用子项目的源代码
Sbt: call subproject's source code from root project
所以我的项目结构如下所示:我的根项目包含设置、任务和配置。此外还有一个包含所有子项目的子项目文件夹。我在根项目上创建了一个任务,该任务依赖于子项目 X 中的某些代码。是否可以从该任务定义中的子项目 x 调用方法?
我的代码如下所示:
lazy val rootSettings: Seq[Setting[_]] = Seq (someRootTask := { //I need to call an object from a subproject here..})
我尝试使用反射 api 但没有成功:
import scala.reflect.runtime.{universe => u }
lazy val docSettings: Seq[Setting[_]] = Seq(
rootTask := {
val subproject = baseDirectory.in(playApp).value.getAbsolutePath
val mirror = u.runtimeMirror(getClass.getClassLoader)
val clazz = mirror.staticModule(subproject+"/" +"controllers.pckg.obj" )
val cm = mirror.reflectModule(clazz)
val instanceMirror = mirror.reflect(cm.instance)
val methodName ="sayHi"
val methodSymbol = u.typeOf[instanceMirror.type].declaration(u.newTermName(methodName)).asMethod
val method = instanceMirror.reflectMethod(methodSymbol)
method.apply()
}
)
// still can't point to the object i want to call.
上面的代码抛出错误。它找不到对象,我知道它的路径,但我无法从根项目中将其引用为 package.class。
如果我理解正确的话你的项目看起来像
root
project
build.properties
build.sbt
subproject1
src
main
scala
com.example.package1
App.scala
subproject2
src
main
scala
com.example.package2
build.sbt
在project/build.sbt
我可以写
Compile / unmanagedSourceDirectories += baseDirectory.value / ".." / "subproject1" / "src" / "main" / "scala"
假设subproject1/src/main/scala/com/example/package1/App.scala
是
package com.example.package1
object App {
def foo(): Unit = println("foo")
}
然后在 root build.sbt
我可以调用 foo
name := "sbtdemo"
version := "0.1"
ThisBuild / scalaVersion := "2.13.4"
lazy val sampleUnitTask = taskKey[Unit]("A sample unit task.")
lazy val rootSettings: Seq[Setting[_]] = Seq(
sampleUnitTask := {
com.example.package1.App.foo()
}
)
lazy val root = project
.in(file("."))
.dependsOn(subproject1, subproject2)
.settings(rootSettings)
lazy val subproject1 = project
.in(file("subproject1"))
lazy val subproject2 = project
.in(file("subproject2"))
如果在 sbt shell I 运行 root/sampleUnitTask
它打印 foo
.
我用 sbt new playframework/play-scala-seed.g8
创建了 Play 项目。一切似乎都有效。我如上所述添加了 project/build.sbt
和 subproject1/src/main/scala/com/example/package1/App.scala
。然后用下面的root build.sbt
name := """playframeworkdemo"""
organization := "com.example"
version := "1.0-SNAPSHOT"
lazy val sampleUnitTask = taskKey[Unit]("A sample unit task.")
lazy val rootSettings: Seq[Setting[_]] = Seq(
sampleUnitTask := {
com.example.package1.App.foo()
}
)
lazy val root = (project in file(".")).enablePlugins(PlayScala)
.dependsOn(subproject1, subproject2)
.settings(rootSettings)
ThisBuild / scalaVersion := "2.13.3"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.example.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.example.binders._"
lazy val subproject1 = project
.in(file("subproject1"))
lazy val subproject2 = project
.in(file("subproject2"))
root/sampleUnitTask
在 sbt 中执行 shell 打印 foo
.
所以我的项目结构如下所示:我的根项目包含设置、任务和配置。此外还有一个包含所有子项目的子项目文件夹。我在根项目上创建了一个任务,该任务依赖于子项目 X 中的某些代码。是否可以从该任务定义中的子项目 x 调用方法?
我的代码如下所示:
lazy val rootSettings: Seq[Setting[_]] = Seq (someRootTask := { //I need to call an object from a subproject here..})
我尝试使用反射 api 但没有成功:
import scala.reflect.runtime.{universe => u }
lazy val docSettings: Seq[Setting[_]] = Seq(
rootTask := {
val subproject = baseDirectory.in(playApp).value.getAbsolutePath
val mirror = u.runtimeMirror(getClass.getClassLoader)
val clazz = mirror.staticModule(subproject+"/" +"controllers.pckg.obj" )
val cm = mirror.reflectModule(clazz)
val instanceMirror = mirror.reflect(cm.instance)
val methodName ="sayHi"
val methodSymbol = u.typeOf[instanceMirror.type].declaration(u.newTermName(methodName)).asMethod
val method = instanceMirror.reflectMethod(methodSymbol)
method.apply()
}
)
// still can't point to the object i want to call.
上面的代码抛出错误。它找不到对象,我知道它的路径,但我无法从根项目中将其引用为 package.class。
如果我理解正确的话你的项目看起来像
root
project
build.properties
build.sbt
subproject1
src
main
scala
com.example.package1
App.scala
subproject2
src
main
scala
com.example.package2
build.sbt
在project/build.sbt
我可以写
Compile / unmanagedSourceDirectories += baseDirectory.value / ".." / "subproject1" / "src" / "main" / "scala"
假设subproject1/src/main/scala/com/example/package1/App.scala
是
package com.example.package1
object App {
def foo(): Unit = println("foo")
}
然后在 root build.sbt
我可以调用 foo
name := "sbtdemo"
version := "0.1"
ThisBuild / scalaVersion := "2.13.4"
lazy val sampleUnitTask = taskKey[Unit]("A sample unit task.")
lazy val rootSettings: Seq[Setting[_]] = Seq(
sampleUnitTask := {
com.example.package1.App.foo()
}
)
lazy val root = project
.in(file("."))
.dependsOn(subproject1, subproject2)
.settings(rootSettings)
lazy val subproject1 = project
.in(file("subproject1"))
lazy val subproject2 = project
.in(file("subproject2"))
如果在 sbt shell I 运行 root/sampleUnitTask
它打印 foo
.
我用 sbt new playframework/play-scala-seed.g8
创建了 Play 项目。一切似乎都有效。我如上所述添加了 project/build.sbt
和 subproject1/src/main/scala/com/example/package1/App.scala
。然后用下面的root build.sbt
name := """playframeworkdemo"""
organization := "com.example"
version := "1.0-SNAPSHOT"
lazy val sampleUnitTask = taskKey[Unit]("A sample unit task.")
lazy val rootSettings: Seq[Setting[_]] = Seq(
sampleUnitTask := {
com.example.package1.App.foo()
}
)
lazy val root = (project in file(".")).enablePlugins(PlayScala)
.dependsOn(subproject1, subproject2)
.settings(rootSettings)
ThisBuild / scalaVersion := "2.13.3"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.example.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.example.binders._"
lazy val subproject1 = project
.in(file("subproject1"))
lazy val subproject2 = project
.in(file("subproject2"))
root/sampleUnitTask
在 sbt 中执行 shell 打印 foo
.