如何将并行包的设置sbt放入罐子里?

时间:2014-11-27 10:21:25

标签: jar package sbt

我的sbt项目包含几个需要打包到jar档案中的模块。正如我在日志中看到的那样,sbt将每个项目按顺序打包到jar中:

[info] Packaging C:path\target\scala-2.11\projectFirst ...
[info] Packaging C:path\target\scala-2.11\projectSecond ...
[info] Packaging C:path\target\scala-2.11\projectThrird ...

项目不依赖于彼此,可以并行打包。是否有可能将sbt设置为并行打包项目?

1 个答案:

答案 0 :(得分:0)

在sbt documentation我们可以找到以下句子:

  

默认情况下,sbt并行执行任务。

我准备实验检查一下。我用4个子项目创建了项目。这是一个代码:

main build.sbt:

lazy val sub1 = project

lazy val sub2 = project

lazy val sub3 = project

lazy val sub4 = project

Keys.`package` in Compile <<= (Keys.`package` in Compile).dependsOn(Def.task {
})

SUB1 / build.sbt

Keys.`package` in Compile <<= (Keys.`package` in Compile).dependsOn(Def.task {
    for( a <- 1 to 2){
        Thread.sleep(1000)
        println( "SubProject 1: " + a );
    }
})

在sub2中,sub3和sub4 build.sbt与sub1中的几乎相同。我只更改了迭代次数和打印数量。 我收到了以下输出:

> package
SubProject 4: 1
SubProject 1: 1
SubProject 4: 2
SubProject 1: 2
SubProject 4: 3
SubProject 3: 1
SubProject 4: 4
SubProject 3: 2
SubProject 4: 5
SubProject 3: 3
SubProject 4: 6
SubProject 3: 4
SubProject 4: 7
SubProject 2: 1
SubProject 4: 8
SubProject 2: 2
SubProject 4: 9
SubProject 2: 3
[info] Updating {file:/D:/sbt/abc/}sub1...
[info] Resolving org.scala-lang#scala-library;2.10.4 ...
[info] Resolving org.scala-lang#scala-compiler;2.10.4 ...
SubProject 4: 10
[info] Resolving org.scala-lang#scala-reflect;2.10.4 ...
[info] Resolving org.scala-lang#jline;2.10.4 ...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Updating {file:/D:/sbt/abc/}abc...
[info] Resolving org.scala-lang#scala-library;2.10.4 ...
[info] Resolving org.scala-lang#scala-compiler;2.10.4 ...
[info] Resolving org.scala-lang#scala-reflect;2.10.4 ...
[info] Resolving org.scala-lang#jline;2.10.4 ...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Updating {file:/D:/sbt/abc/}sub3...
[info] Resolving org.scala-lang#scala-library;2.10.4 ...
[info] Resolving org.scala-lang#scala-compiler;2.10.4 ...
[info] Resolving org.scala-lang#scala-reflect;2.10.4 ...
[info] Resolving org.scala-lang#jline;2.10.4 ...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Updating {file:/D:/sbt/abc/}sub2...
[info] Resolving org.scala-lang#scala-library;2.10.4 ...
[info] Resolving org.scala-lang#scala-compiler;2.10.4 ...
[info] Resolving org.scala-lang#scala-reflect;2.10.4 ...
[info] Resolving org.scala-lang#jline;2.10.4 ...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
SubProject 4: 11
[info] Done updating.
[info] Packaging D:\sbt\abc\sub1\target\scala-2.10\sub1_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[info] Packaging D:\sbt\abc\target\scala-2.10\abc_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[info] Packaging D:\sbt\abc\sub3\target\scala-2.10\sub3_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[info] Packaging D:\sbt\abc\sub2\target\scala-2.10\sub2_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
SubProject 4: 12
SubProject 4: 13
SubProject 4: 14
SubProject 4: 15
SubProject 4: 16
SubProject 4: 17
SubProject 4: 18
SubProject 4: 19
SubProject 4: 20
[info] Updating {file:/D:/sbt/abc/}sub4...
[info] Resolving org.scala-lang#scala-library;2.10.4 ...
[info] Resolving org.scala-lang#scala-compiler;2.10.4 ...
[info] Resolving org.scala-lang#scala-reflect;2.10.4 ...
[info] Resolving org.scala-lang#jline;2.10.4 ...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Packaging D:\sbt\abc\sub4\target\scala-2.10\sub4_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.

所以有四个子项目。在每个子项目包中都有&#39;任务取决于创建的任务(打印并在循环中休眠)。当我执行&#39; package&#39;然后命令然后执行wach子项目的任务。但是对于每一个,调用依赖任务。正如我们在输出中看到的那样,这些依赖任务一次只能运行,但一次只运行两次。这与以下句子相对应:

  

(虽然从未作为设置公开,但在给定时间运行的最大任务数也是内部可配置的。)

输出三行:

[info] Resolving org.scala-lang#scala-compiler;2.10.4 ...
SubProject 4: 10
[info] Resolving org.scala-lang#scala-reflect;2.10.4 ...

提供证据证明来自一个子项目的打包任务与来自其他子项目的从属任务并行运行。