所以我的项目结构如下:我的根项目包含设置、任务和配置。此外,它还有一个子项目文件夹,其中包含所有子项目。我在根项目上创建了一个任务,该任务依赖于子项目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。
从build.sbt 引用scala文件
如果我理解正确,你的项目看起来像
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")
}
然后在根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中运行root/sampleUnitTask
,它将打印foo
。
我用sbt new playframework/play-scala-seed.g8
创建了Play项目。一切似乎都正常。我如上所述添加了project/build.sbt
和subproject1/src/main/scala/com/example/package1/App.scala
。然后使用以下根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"))
在sbt shell中执行的root/sampleUnitTask
打印foo
。