scala 如何从另一个 sbt 项目引用外部 sbt 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11653435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:22:54  来源:igfitidea点击:

How to reference external sbt project from another sbt project?

scalasbt

提问by Diego

I have the following setup of a Scala application and a common core library: root

我有以下 Scala 应用程序和公共核心库的设置:root

 -> /ApplicationA
   -> /project
     -> /build.sbt
 -> /CoreLibrary
   -> /project
     -> /build.sbt

I want to add a reference from ApplicationA to CoreLibrary à la Eclipse project reference, so that every time CoreLibrary changes ApplicationA is built as well. I′ve tried the following contents of build.Scala for ApplicationA:

我想添加一个从 ApplicationA 到 CoreLibrary à la Eclipse 项目引用的引用,这样每次 CoreLibrary 更改时 ApplicationA 也会被构建。我已经为 ApplicationA 尝试了 build.Scala 的以下内容:

  val core = Project(
      id = "platform-core",
      base = file("../CoreLibrary"))

  val main = Project(id = "application, base = file(".")).dependsOn(core)

However, when compiling ApplicationA SBT complains that a dependency can only be a subdirectory!!:

但是,在编译 ApplicationA SBT 时会抱怨依赖项只能是子目录!!:

java.lang.AssertionError: assertion failed: Directory C:\git\CoreLibrary is not contained in build root C:\git\ApplicationA

This seems completely straightforward, what's the correct way of having this project dependency?

这看起来很简单,拥有这个项目依赖的正确方法是什么?

采纳答案by jwinandy

You can do a source dependency on your project like that :

您可以像这样对您的项目进行源依赖:

 lazy val core = RootProject(file("../CoreLibrary"))

 val main = Project(id = "application", base = file(".")).dependsOn(core) 

I have a working example with a multimodule play build : https://github.com/ahoy-jon/play2MultiModule/blob/master/playapp/project/Build.scala

我有一个多模块播放构建的工作示例:https: //github.com/ahoy-jon/play2MultiModule/blob/master/playapp/project/Build.scala

But I think the proper way (it depends of your context) of doing it is to create a

但我认为这样做的正确方法(这取决于您的上下文)是创建一个

 -> /project/
   -> Build.scala
 -> /ApplicationA
   -> /project
     -> /build.sbt
 -> /CoreLibrary
   -> /project
     -> /build.sbt

referencing the two projects and the dependencies between them.

引用这两个项目以及它们之间的依赖关系。

回答by Maxence

With sbt 0.12.1 it seems possible to get a simple reference to a project :

使用 sbt 0.12.1 似乎可以获得对项目的简单引用:

I used ProjectRefinstead of RootProject

我用ProjectRef而不是RootProject

http://www.scala-sbt.org/0.12.1/api/sbt/ProjectRef.html

http://www.scala-sbt.org/0.12.1/api/sbt/ProjectRef.html

ProjectRef(file("../util-library"), "util-library")

sbt-eclipse also works.

sbt-eclipse 也可以。

回答by Mustafa

Since sbt 0.13, you may create multi-project definitions directly in .sbtwithout needing a Build.scalafile.

sbt 0.13 开始,您可以直接在其中创建多项目定义.sbt而无需Build.scala文件。

So adding the following to ApplicationA/project/build.sbtwould be sufficient.

因此,将以下内容添加到 ApplicationA/project/build.sbt就足够了。

lazy val core = RootProject(file("../CoreLibrary"))

val main = Project(id = "application", base = file(".")).dependsOn(core)