6

In an SBT Plugin, I'm trying to access to managed resources of subprojects.

Here is the build file:

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {
  val appName         = "demo"
  val appVersion      = "1.0-SNAPSHOT"
  val appDependencies = Seq(
    "org.jruby" % "jruby-complete" % "1.7.1"
  )

  val widgets = play.Project("widgets", appVersion, appDependencies, path = file("widgets"))
  val main = play.Project(appName, appVersion, appDependencies, path = file("demo"))
    .dependsOn(widgets)

}

I'm working in an SBT plugin defined in plugins.sbt.

Now, I need to use resources files from the subproject (widgets) during compilation of the parent project (demo).

So far the closest I've got to is the buildDependencies settings key - but I'm only getting ProjectRef objects, and the only information is the build base and the project id. I couldn't find a way to get to that project's resources directory.

erwan
  • 1,285
  • 5
  • 14
  • 28

2 Answers2

2

I'm not familiar with writing plugins, but at least in your build.sbt you can define the resource file.

Or, again in the build.sbt you can create a "common" project that others reference, like:

lazy val common = (project in file("common"))
  .settings(
    Seq(
      includeFilter in unmanagedResources := new SimpleFileFilter(_.getCanonicalPath.startsWith((sourceDirectory.value / "main" / "resources").getCanonicalPath))
    )
  )

Then other code (e.g. a Task) could reference this like:

lazy val doSomething = taskKey[Seq[File]]("Does something useful")
lazy val doSomethingSetting = doIt := {

  val resourceDir = (resourceDirectory in common in Compile).value
  println(resourceDir)

}

So your other projects could run this or reference that directory

Hopefully there's a straight forward way to implement one of those solutions for a plugin vs a build?

ecoe
  • 4,994
  • 7
  • 54
  • 72
0

Unfortunately I do not believe this is possible. I was trying something similar but found the following in the documentation:

Note: At runtime, all plugins for all builds are loaded in a separate, parent class loader of the class loaders for builds. This means that plugins will not see classes or resources from build definitions

See: SBT Plugins

tysonjh
  • 1,329
  • 1
  • 12
  • 27
  • 2
    The quote is referring to classes and resources that are available from the plugin class loader. This means `Class.forName(..., getClass.getClassLoader)` and `getClass.getClassLoader.getResource(...)`. Also, it is only talking about classes and resources defined for the build definition. That is, the ones in `project/` and not the main project. A plugin can still use settings/tasks as usual to get the `File`s corresponding to the resources and classes of a project. – Mark Harrah Aug 01 '13 at 20:00