简体   繁体   English

SBT:访问子项目的托管资源?

[英]SBT: Access managed resources of a subproject?

In an SBT Plugin, I'm trying to access to managed resources of subprojects. 在SBT插件中,我正在尝试访问子项目的托管资源。

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. 我正在使用plugins.sbt中定义的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. 到目前为止,我最接近的是buildDependencies设置键 - 但我只获得了ProjectRef对象,唯一的信息是构建基础和项目ID。 I couldn't find a way to get to that project's resources directory. 我找不到进入该项目资源目录的方法。

I'm not familiar with writing plugins, but at least in your build.sbt you can define the resource file . 我不熟悉编写插件,但至少在build.sbt你可以定义资源文件

Or, again in the build.sbt you can create a "common" project that others reference , like: 或者,再次在build.sbt您可以创建其他人引用的“常见”项目 ,例如:

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

Then other code (eg 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? 希望有一种直接的方法来实现插件与构建的解决方案之一?

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 请参阅: SBT插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM