So my project structure looks like this: My root project contains the settings, tasks and configs. In addition is has a subprojects folder which contains all subprojects. I have created a task on the root project that depends on some code in a subproject X. Is it possible to call a method from subproject x inside that task definition?
My code looks like this :
lazy val rootSettings: Seq[Setting[_]] = Seq (someRootTask := { //I need to call an object from a subproject here..})
I tried to use the reflection api with no success:
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.
The code above throws an error. It can't find the object, i know its path but i can't reference to it as package.class from the root project.
Reference scala file from build.sbt
If I understood correctly your project looks like
In
project/build.sbt
I can writeSuppose
subproject1/src/main/scala/com/example/package1/App.scala
isThen in root
build.sbt
I can callfoo
If in sbt shell I run
root/sampleUnitTask
it printsfoo
.I created Play project with
sbt new playframework/play-scala-seed.g8
. Everything seems to work. I addedproject/build.sbt
andsubproject1/src/main/scala/com/example/package1/App.scala
as above. Then with the following rootbuild.sbt
root/sampleUnitTask
executed in sbt shell printsfoo
.