Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share code between project and build definition project in SBT

Tags:

scala

sbt

If I have written some source code in my build definition project (in /project/src/main/scala) in SBT. Now I want to use these classes also in the project I am building. Is there a best practice? Currently I have created a custom Task that copies the .scala files over.

like image 296
reikje Avatar asked Apr 10 '14 11:04

reikje


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


2 Answers

Those seem like unnecessarily indirect mechanisms.

unmanagedSourceDirectories in Compile += baseDirectory.value / "project/src/main"
like image 119
psp Avatar answered Sep 21 '22 23:09

psp


Sharing sourceDirectories as in extempore's answer is the simplest way to go about it, but unfortunately it won't work well with IntelliJ because the project model doesn't allow sharing source roots between multiple modules.

Seth Tisue's approach will work, but requires rebuilding to update sources.

To actually share the sources and have IntelliJ pick up on it directly, you can define a module within the build.

The following approach seems to only work in sbt 1.0+

Create a file project/metabuild.sbt:

val buildShared = project
val buildRoot = (project in file("."))
  .dependsOn(buildShared)

and in your build.sbt:

val buildShared = ProjectRef(file("project"), "buildShared")
val root = (project in file("."))
  .dependsOn(buildShared)

Then put your shared code in project/buildShared/src/main/scala/ and refresh. Your project will look something like this in IntelliJ:

enter image description here

Full example project: https://github.com/jastice/shared-build-sources

like image 20
Justin Kaeser Avatar answered Sep 21 '22 23:09

Justin Kaeser