如何在 SBT 中的项目之间共享资源 [英] How to share resources between projects in SBT

查看:14
本文介绍了如何在 SBT 中的项目之间共享资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在工作的项目是一个基于 Lift 框架的网络应用程序.我们也在使用 xsbt 网络插件.有一个核心"项目,其中包含绝大多数功能;我当前的目标是创建两个分发"项目,将一组不同的类路径资源添加到核心"项目.问题是我要么 1) 无法运行分发"项目,要么 2) 让它们运行,但所需的资源似乎不存在.

The project I'm working on at work is a webapp on the Lift Framework. We're using xsbt web plugin as well. There's a "core" project, which contains the vast majority of the functionality; my current goal is to create two "distribution" projects that add a different set of classpath resources to the "core" project. The problem is that I either 1) can't get the "distribution" projects to run, or 2) Get them to run, but the required resource doesn't seem to be there.

我的尝试

这是我的 project/Build.scala 的精简版:

Here's an abridged version of my project/Build.scala:

lazy val core = Project("Core", file("core"))
  .settings( /*some dependencies, resolvers, webSettings */ )

lazy val app1 = Project("App1", file("app1"))
  .aggregate(core)
  .settings( /*the same settings as core */ )

lazy val app2 = Project("App2", file("app2"))
  .aggregate(core)
  .settings( /*the same settings as core*/ )

然后在 app1app2 的目录结构中,我在 src/main/resources/aFileINeed 处有一个文件.核心应用程序使用 class.getResource 方法从类路径加载文件.

Then in the directory structure for both app1 and app2, I have a file at src/main/resources/aFileINeed. The core application is using the class.getResource approach to load the file from the classpath.

问题

如果我尝试使用 container:start 运行其中一个分发项目,它不会在类路径中检测到所需的文件.此外,它声称 src/main/webapp 不是现有目录(该文件夹包含在核心项目中,因为它是 xsbt Web 插件所必需的).

If I try to run one of the distribution projects, using container:start, it does not detect the required file in the classpath. Also, it claims that src/main/webapp is not an existing directory (that folder is included in the core project, as it is required by the xsbt web plugin).

我怎样才能让这些项目合并"它们的资源?我希望在 Build 中使用 aggregatedependsOn.scala 项目定义会为我处理这个问题,但它显然没有.

How can I get these projects to "merge" their resources? I expected that using aggregate or dependsOn in the Build.scala project definition would handle that for me, but it apparently doesn't.

推荐答案

这就是我正在做的.我在项目的根目录中创建了一个全局资源"目录,然后是以下变量:

This is what I'm doing. I create a global 'resources' directory in the root of the project, and then the following variable:

    lazy val globalResources = file("resources")

然后在每个项目的设置中:

And then in every project's settings:

    unmanagedResourceDirectories in Runtime += globalResources 

对于使用 xsbt-web-plugin 或其他导入它的库的项目,您必须使用更强大的

For projects that use the xsbt-web-plugin or some other library that imports it you'll have to use the stronger

    unmanagedResourceDirectories in Compile += globalResources

请注意,使用此解决方案,您的项目可能会有多个资源目录,如果您两次定义同一个文件,AFAIK compile:copy-resources 会生您的气.

Beware that using this solution your projects will potentially have muliple resource directories and AFAIK if you define the same file twice compile:copy-resources is going to be angry at you.

这篇关于如何在 SBT 中的项目之间共享资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆