Setting up sbt environment to hack on multiple libraries at once

前端 未结 1 722
孤街浪徒
孤街浪徒 2020-12-09 21:49

Is there an equivalent to Leiningen\'s \"checkouts\" feature in sbt?

Here is what I want to accomplish:

I have two projects, an application Foo

相关标签:
1条回答
  • 2020-12-09 22:25

    You can declare a source dependency from Foo to Bar via a project reference:

    import sbt._
    
    object FooBuild extends Build {
      lazy val root = Project(
        id = "foo",
        base = file(".")
      ) dependsOn(theBarBuild)
    
      lazy val theBarBuild = ProjectRef(
        base = file("/path/to/bar"),
        id = "bar")
    }
    

    This should also recompile Bar (if it has changed) whenever you compile Foo. Please note that the id of the project reference must match the actual id of the Bar project, which might be something like e.g. default-edd2f8 if you use a simple build definition (.sbt files only).

    This technique is especially useful for plug-ins (see my blog post about this topic).

    Edit:

    You can kind of re-code the checkout behaviour like this:

    import sbt._
    
    object FooBuild extends Build {
      lazy val root = addCheckouts(Project(id = "foo", base = file(".")))
    
      def addCheckouts(proj: Project): Project = {
        val checkouts = proj.base.getCanonicalFile / "checkouts"
        if (! checkouts.exists) proj
        else proj.dependsOn(IO.listFiles(DirectoryFilter)(checkouts).map { dir =>
          ProjectRef(base = dir, id = dir.name): ClasspathDep[ProjectReference]
        }:_*)
      }
    }
    

    This checks your project directory for a checkouts directory, and if it exists, adds the directories therein (which should be symlinks to other projects) as project references to the project. It expects the symlink to be named like the actual ID of the linked project (e.g. default-edd2f8 or bar). If the directory doesn't exist, the build just works as before.

    When you add or remove a symlink in the checkouts directory (or the directory itself), you must reload the Foo project to pick up the changes.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题