积累子项目' `discoveredMainClasses`

时间:2017-08-21 13:41:22

标签: scala sbt sbt-native-packager

如何累积项目的所有discoveredMainClasses及其在SBT中的相关子项目?

例如,我有一个类似于

的项目
├── bar
│   └── src
│       └── main
│           └── scala
│               └── BarMain.scala
├── build.sbt
├── foo
│   └── src
│       └── main
│           └── scala
│               └── FooMain.scala
├── project
│   └── build.properties
└── root

使用一个root项目aggregate(foo, bar)discoveredMainClasses获得以下内容:

[info] foo/compile:discoveredMainClasses
[info]  List(MainFoo)
[info] bar/compile:discoveredMainClasses
[info]  List(MainBar)
[info] root/compile:discoveredMainClasses
[info]  List()

只有一个root我只有dependsOn(foo, bar)

> show discoveredMainClasses
[info] *  

我如何show root/discoveredMainClasses同时包含MainFooMainBar

对于上下文,我还有其他任务取决于discoveredMainClasses的输出,即native-packager中的makeBashScripts

2 个答案:

答案 0 :(得分:1)

核心思想是创建一个模块,该模块依赖于您要包含的所有子模块,并配置此模块上的所有设置。

这会产生build.sbt这样的

lazy val root = project.in(file("."))
  // package the root module, but not the sub modules
  .enablePlugins(JavaAppPackaging)
  .settings(
    name := "application",
    // add the discoveredMainClasses to this project
    discoveredMainClasses in Compile ++= (discoveredMainClasses in (client, Compile)).value,
    discoveredMainClasses in Compile ++= (discoveredMainClasses in (server, Compile)).value
  )
  // include these modules in the resulting package
  .dependsOn(client, server)

lazy val client = project.in(file("client"))
  .settings(
    name := "client"
  )

lazy val server = project.in(file("server"))
  .settings(
    name := "server"
  )

(discoveredMainClasses in (client, Compile)).value从Compile范围内的客户端项目访问discoveredMainClasses。

您可以使用

构建和运行应用程序
$ sbt universal:stage
$ ./target/universal/stage/bin/client-app
$ ./target/universal/stage/bin/server-app

A running example can be found here

欢呼声, 缪奇

答案 1 :(得分:0)

@ Muki回答的另一种方法是定义一个ScopeFilter,它包含除root之外的所有内容,并以这种方式累积主类。这样做的好处是无需在任何地方重复clientserver

结果build.sbt

lazy val allCompileButRootFilter = 
  ScopeFilter(inAggregates(ThisProject, includeRoot = false), inConfigurations(Compile))

lazy val root = project.in(file("."))
  .aggregate(client, server)
  .enablePlugins(JavaAppPackaging)
  .settings(
    discoveredMainClasses in Compile ++= 
      discoveredMainClasses.all(allCompileButRootFilter).value.flatten,
    ...
  )