【问题标题】:SBT: Access managed resources of a subproject?SBT:访问子项目的托管资源?
【发布时间】:2013-01-21 11:06:41
【问题描述】:

在 SBT 插件中,我正在尝试访问子项目的托管资源。

这是构建文件:

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)

}

我正在使用 plugins.sbt 中定义的 SBT 插件。

现在,我需要在编译父项目(演示)期间使用子项目(小部件)中的资源文件。

到目前为止,我最接近的是 buildDependencies 设置键 - 但我只获取 ProjectRef 对象,唯一的信息是构建基础和项目 ID。我找不到进入该项目资源目录的方法。

【问题讨论】:

    标签: scala plugins sbt


    【解决方案1】:

    我不熟悉编写插件,但至少在你的build.sbt 中你可以define the resource file

    或者,你可以再次在build.sbtcreate a "common" project that others reference,比如:

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

    然后其他代码(例如任务)可以这样引用:

    lazy val doSomething = taskKey[Seq[File]]("Does something useful")
    lazy val doSomethingSetting = doIt := {
    
      val resourceDir = (resourceDirectory in common in Compile).value
      println(resourceDir)
    
    }
    

    所以你的其他项目可以运行这个或reference that directory

    希望有一种直接的方式来实现插件与构建的解决方案之一?

    【讨论】:

      【解决方案2】:

      不幸的是,我不相信这是可能的。我正在尝试类似的东西,但在文档中发现了以下内容:

      注意:在运行时,所有构建的所有插件都加载到构建类加载器的单独的父类加载器中。这意味着插件不会从构建定义中看到类或资源

      见:SBT Plugins

      【讨论】:

      • 引用是指插件类加载器中可用的类和资源。这意味着Class.forName(..., getClass.getClassLoader)getClass.getClassLoader.getResource(...)。此外,它只讨论为构建定义定义的类和资源。也就是说,project/ 中的那些,而不是主项目。插件仍然可以像往常一样使用设置/任务来获取与项目的资源和类对应的Files。
      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多