Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share resources between projects in SBT

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.

What I've tried

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*/ )

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.

Problems

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

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.

like image 278
Dylan Avatar asked Sep 27 '12 16:09

Dylan


People also ask

What is ThisBuild in sbt?

Typically, if a key has no associated value in a more-specific scope, sbt will try to get a value from a more general scope, such as the ThisBuild scope. This feature allows you to set a value once in a more general scope, allowing multiple more-specific scopes to inherit the value.

What is the difference between sbt and Scala?

SBT is tied to a specific project defined by a build. sbt file in a way that $ sbt console will load up the same REPL environment as $ scala but with the addition of all of the project code and dependencies defined in the build available for import. Also, it will use the version of Scala defined by build.

What does the src folder contain in an sbt project?

The project folder contains sbt configurations and settings, while src contains all your source code. sbt creates the target folder after you compile your code; it includes the JVM bytecode of your application.

What does sbt clean do?

clean – delete all generated sources, compiled artifacts, intermediate products, and generally all build-produced files. reload – reload the build, to take into account changes to the sbt plugin and its transitive dependencies.


1 Answers

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 

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

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.

like image 177
fedeoasi Avatar answered Sep 28 '22 12:09

fedeoasi