设置 sbt 环境以同时破解多个库 [英] Setting up sbt environment to hack on multiple libraries at once

查看:41
本文介绍了设置 sbt 环境以同时破解多个库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 sbt 中是否有与 Leiningen 的结帐"功能等效的功能?

这是我想要完成的:

我有两个项目,一个应用程序 Foo 和库Bar".我想独立发布这些项目中的每一个.Foo 依赖于 Bar,每当第三方尝试时,sbt 项目将指示 sbt 从存储库下载Bar"的 jar构建Foo"(这是典型的行为).

现在,假设我想同时破解 FooBar.例如,在处理 Foo 时,我想直接编辑和调试 Bar 的一些源代码,以便编辑影响 Foo(然后稍后在方便时重建).

在此 hack 会话期间,我如何指示 sbt 满足其对 Bar 的依赖,从其在我的机器(而不是本地存储库)上的源代码?

(PS 我问了一个类似的问题对于 Clojure/Leiningen.Leiningen 有 结帐" 功能可以实现这一点.我想知道 sbt 中是否有类似的东西...)

解决方案

您可以通过项目引用声明从 Foo 到 Bar 的源依赖:

import sbt._对象 FooBuild 扩展构建 {懒惰的 val root = 项目(id = "foo",基 = 文件(.")) 依赖 (theBarBuild)懒惰的 val theBarBuild = ProjectRef(base = file("/path/to/bar"),id = "条")}

每当您编译 Foo 时,这也应该重新编译 Bar(如果它已更改).请注意,项目引用的 id 必须与 Bar 项目的实际 id 匹配,这可能类似于例如default-edd2f8 如果您使用简单的构建定义(仅限 .sbt 文件).

这种技术对插件特别有用(参见我的博客发布关于这个话题).

您可以像这样重新编码结帐行为:

import sbt._对象 FooBuild 扩展构建 {懒惰的 val root = addCheckouts(Project(id = "foo", base = file(".")))def addCheckouts(proj: 项目): 项目 = {val checkouts = proj.base.getCanonicalFile/"checkouts"if (!checkouts.exists) 项目else proj.dependsOn(IO.listFiles(DirectoryFilter)(checkouts).map { dir =>ProjectRef(base = dir, id = dir.name): ClasspathDep[ProjectReference]}:_*)}}

这会检查您的项目目录中是否存在 checkouts 目录,如果存在,则添加其中的目录(应该是其他项目的符号链接)作为项目的项目引用.它期望符号链接的命名类似于链接项目的实际 ID(例如 default-edd2f8bar).如果该目录不存在,则构建将像以前一样工作.

当您在 checkouts 目录(或目录本身)中添加或删除符号链接时,您必须重新加载 Foo 项目以获取更改.

希望这会有所帮助.

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 and library "Bar". I want to publish each of these projects independently. Foo depends on Bar, and the sbt project will direct sbt to download the jar for "Bar" from a repository whenever a third-party tries to build "Foo" (which is typical behavior).

Now, say I want to hack on both Foo and Bar at the same time. For example, while working on Foo, I want to directly edit and debug some of the source for Bar so the edits affect Foo (and then later rebuild Bar when it is convenient).

How can I instruct sbt to satisfy its dependency on Bar from its source code on my machine (rather than my local repository) during this hack session?

(P.S. I asked a similar question for Clojure/Leiningen. Leiningen has the "checkouts" feature which accomplishes this. I am wondering if there is something similar in sbt...)

解决方案

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.

这篇关于设置 sbt 环境以同时破解多个库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆