gpt4 book ai didi

build - 使用 'aggregate' 后跟 'dependsOn' 是否与相同的模块冗余?

转载 作者:IT老高 更新时间:2023-10-28 11:13:06 25 4
gpt4 key购买 nike

在 SBT 中,如果它们都包含相同的子模块,那么在 dependsOn 之后使用 aggregate 是否是多余的?根据文档,似乎是这样,但我以前见过这种行为,但我不明白有什么好处。如果一个项目是用依赖项定义的,那是否已经暗示聚合对这些相同的依赖项做了什么?我注意到使用此冗余聚合后我的项目构建速度要慢得多,我想知道是否可以安全地删除它。

lazy val module = sbt.Project(...) dependsOn (foo, bar) aggregate (foo, bar)

或者只是...

lazy val module = sbt.Project(...) dependsOn (foo, bar)

我正在使用 SBT 0.13.6

最佳答案

tl;博士 aggregate导致任务在聚合模块中执行,所有 aggregate d 一会儿 dependsOn设置一个 CLASSPATH 依赖,所以库对 aggregate 可见ing 模块(取决于示例中的 compile 又名 default 的配置)。

展示差异的示例。

我正在使用以下 build.sbt (没什么有趣的):

lazy val a = project

lazy val b = project

lazy val c = project dependsOn b aggregate (a,b)

构建定义了三个模块 a , b , 和 c最后 c项目将成为 a 的聚合和 b .还有第四个模块——一个隐含的模块——聚合所有模块a , b , 和 c .

> projects
[info] In file:/Users/jacek/sandbox/aggregate-dependsOn/
[info] a
[info] * aggregate-dependson
[info] b
[info] c

当我在 aggreate 中执行任务时ing 模块,任务将在 aggregate 中执行d 模块。

> compile
[info] Updating {file:/Users/jacek/sandbox/aggregate-dependsOn/}b...
[info] Updating {file:/Users/jacek/sandbox/aggregate-dependsOn/}a...
[info] Updating {file:/Users/jacek/sandbox/aggregate-dependsOn/}aggregate-dependson...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Updating {file:/Users/jacek/sandbox/aggregate-dependsOn/}c...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[success] Total time: 0 s, completed Oct 22, 2014 9:33:20 AM

当我在 c 中执行任务时也会发生同样的情况这将依次针对 a 执行它和 b ,但不在顶级项目中。

> show c/clean
[info] a/*:clean
[info] ()
[info] b/*:clean
[info] ()
[info] c/*:clean
[info] ()
[success] Total time: 0 s, completed Oct 22, 2014 9:34:26 AM

当一个任务在 a 中执行时或 b ,它只在项目内运行。

> show a/clean
[info] ()
[success] Total time: 0 s, completed Oct 22, 2014 9:34:43 AM

任务是否在aggregate中执行项目由 aggregate 控制项目和/或任务的键。

> show aggregate
[info] a/*:aggregate
[info] true
[info] b/*:aggregate
[info] true
[info] c/*:aggregate
[info] true
[info] aggregate-dependson/*:aggregate
[info] true

按照 Aggregation 中的说明进行更改:

In the project doing the aggregating, the root project in this case, you can control aggregation per-task. (...) aggregate in update is the aggregate key scoped to the update task.

下面我正在更改 c 的 key 模块和 clean任务所以clean不再在 aggregate 中执行d 模块 ab :

> set aggregate in (c, clean) := false
[info] Defining c/*:clean::aggregate
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to aggregate-dependson (in build file:/Users/jacek/sandbox/aggregate-dependsOn/)
> show c/clean
[info] ()
[success] Total time: 0 s, completed Oct 22, 2014 9:39:13 AM

c 的其他任务不受影响,仍在执行 c 中的任务将在 aggregate 中运行它模块:

> show c/libraryDependencies
[info] a/*:libraryDependencies
[info] List(org.scala-lang:scala-library:2.10.4)
[info] b/*:libraryDependencies
[info] List(org.scala-lang:scala-library:2.10.4)
[info] c/*:libraryDependencies
[info] List(org.scala-lang:scala-library:2.10.4)

aggregate为 sbt 任务设置依赖关系,以便它们在另一个 aggregate 中执行d 模块,dependsOn设置 CLASSPATH 依赖关系,即 dependsOn 中的代码ed 模块在 dependsOn 中可见ing 一个(对不起"new"词)。

假设 b有一个主要对象如下:

object Hello extends App {
println("Hello from B")
}

保存Hello反对b/hello.scala ,即在 b 下模块。

自从 c被定义为 dependsOn b (见 build.sbt 以上),Hello对象在 b 中可见(因为它属于模块),而且在c .

> b/run
[info] Running Hello
Hello from B
[success] Total time: 0 s, completed Oct 22, 2014 9:46:44 AM
> c/runMain Hello
[info] Running Hello
Hello from B
[success] Total time: 0 s, completed Oct 22, 2014 9:46:58 AM

(我不得不在 runMain 中使用 c 作为 run 一个人看不到我无法解释的类)。

尝试在 a 中运行任务以 java.lang.ClassNotFoundException: Hello 结尾因为该类在模块中可见。

> a/runMain Hello
[info] Updating {file:/Users/jacek/sandbox/aggregate-dependsOn/}a...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Running Hello
[error] (run-main-6) java.lang.ClassNotFoundException: Hello
java.lang.ClassNotFoundException: Hello
at java.lang.ClassLoader.findClass(ClassLoader.java:530)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[trace] Stack trace suppressed: run last a/compile:runMain for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last a/compile:runMain for the full output.
[error] (a/compile:runMain) Nonzero exit code: 1
[error] Total time: 0 s, completed Oct 22, 2014 9:48:15 AM

重新定义 adependsOn bbuild.sbt异常消失了。

您应该阅读 Multi-project builds在官方文档中。

关于build - 使用 'aggregate' 后跟 'dependsOn' 是否与相同的模块冗余?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com