简体   繁体   中英

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. This other "shared" repo is symlinked into the app directories of each of the projects during development, and copied there before deploy.

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.

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 ? 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 :

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).

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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