Omid
Omid

Reputation: 1989

Add Java source folder containing Symlinks to build.sbt file

I am working on a legacy code repo that I want to add my Scala project to it. Here is the directory tree:

LegacyFolder
|
--JavaSourceDir1
|---SymLink to "LegacyFolder"
--JavaSourceDir2
|---SymLink to "LegacyFolder"
--MyProject

I want to use the Java files in JavaSourceDir1 and JavaSourceDir2 in my project (as a dependency). The first idea was to add them as unmanaged source files. Something like this:

unmanagedSourceDirectories in Compile += baseDirectory.value / "../JavaSourceDir1"

unmanagedSourceDirectories in Compile += baseDirectory.value / "../JavaSourceDir2"

The problem is with all those Symlinks inside the JavaSourceDir1/2, they make SBT crazy and it hangs on Run/Compile commands. It makes sense because SBT is in an infinite loop to find the files.

So, how can I resolve this? Is there any way to only get Java files in those folders non-recursively and somehow add them as dependencies?

Upvotes: 1

Views: 436

Answers (1)

Eugene Yokota
Eugene Yokota

Reputation: 95624

You can define a function that retrieves *.java files directly under a given directory called legacyJavaSources, and feed the result into unmanagedSources in Compile:

lazy val root = (project in file("."))
  .settings(
    unmanagedSources in Compile ++=
      legacyJavaSources(baseDirectory.value.getParentFile / "JavaSourceDir1"),

    unmanagedSources in Compile ++=
      legacyJavaSources(baseDirectory.value.getParentFile / "JavaSourceDir2")
  )


def legacyJavaSources(dir: File): Vector[File] = {
  (dir * "*.java").get.toVector
}

Upvotes: 2

Related Questions