简体   繁体   English

如何从SBT Play Framework 2.3项目的源中排除目录

[英]How to exclude directory from source in an SBT Play Framework 2.3 project

I have multiple Play Framework 2.3.8 projects that share a common set of models and other library files from a different repository. 我有多个Play Framework 2.3.8项目,它们共享一组公共模型和来自不同存储库的其他库文件。 This other "shared" repo is symlinked into the app directories of each of the projects during development, and copied there before deploy. 在开发过程中,另一个“共享”存储库被符号链接到每个项目的app目录中,并在部署之前复制到该目录中。

I want to have all of the unit tests associated with this set of shared libraries in the shared repo, so that they get executed regardless of which project they're being compiled in. However, because these files are all then buried in the app directory, which is defined as sources by default in build.sbt, the project won't compile because the package is defined from the viewpoint of the test directory. 我想在共享存储库中拥有与此共享库集相关的所有单元测试,以便无论在哪个项目中进行编译都可以执行它们。但是,由于这些文件随后全部埋在了app目录中,它在build.sbt中默认定义为源,因此该项目不会编译,因为该包是从测试目录的角度定义的。

The structure roughly looks like this: 结构大致如下所示:

app/
  controllers/
  shared -> ../../shared
test/
  shared -> ../app/shared/test

Is there a way in my build.sbt to tell it to ignore the test directory inside of app/shared ? 我的build.sbt中是否有办法告诉它忽略app/shared内部的test目录? Or, if someone has a better suggestion for how to structure this, any ideas are welcome. 或者,如果有人对如何构建此结构有更好的建议,则欢迎提出任何想法。 Thanks! 谢谢!

To answer your first question, of excluding test : 要回答您的第一个问题,即排除test

excludeFilter in (Compile, unmanagedSources) := {
  val testDir = ((sourceDirectory in Compile).value / "shared" / "test").getCanonicalPath
  HidderFileFilter | SimpleFileFilter(_.getCanonicalPath.startsWith(testDir))
}

To answer your second question, a better way to structure this is to use project refs: 为了回答您的第二个问题,一种更好的结构化方法是使用项目引用:

lazy val shared = ProjectRef(file("../shared"), "shared-name")

lazy val myProject = (project in file(".")).dependsOn(shared)

In this setup, ../shared is a stand alone build with it's own build.sbt that you can publish etc, and shared-name is the name (set by name := "shared-name" of the project in that build). 在此设置中, ../shared是具有其自己的build.sbt独立构建,您可以将其发布等,并且shared-name是名称(按name := "shared-name"设置name := "shared-name"中的项目的name := "shared-name" ) 。

An even better way would be to have the shared project be the root project, and myProject (and other projects that depend on it) be sub projects in the same build. 更好的方法是让shared项目成为根项目,而myProject (以及其他依赖该项目的项目)成为同一构建中的子项目。

My solutions for problems like this is to define a "unit test JAR", ie a JAR that is like junit.jar in that you can add it to your project as test dependency and that it contains support code for tests. 对于此类问题,我的解决方案是定义“单元测试JAR”,即类似于junit.jar的JAR,您可以将其作为测试依赖项添加到项目中,并且其中包含测试支持代码。

To use the support code, you need to write a test which extends the unit test support code or kicks off some kind of runner that runs the test in the support JAR. 要使用支持代码,您需要编写一个扩展单元测试支持代码的测试,或者在支持JAR中启动某种运行测试的运行程序。

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

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