如何使用sbt-native-packager定义多项目构建,为每个子项目创建RPM

时间:2016-03-29 11:56:59

标签: sbt sbt-native-packager

在我见过的关于多模块构建和sbt-native-packager的所有示例中,它们都将子项目聚合到一个包中。我有子项目,每个子项目提供微服务。我相信每一个都应该拥有它自己的原生包,但我不知道如何做到这一点,并为所有子项目建立一个命令。

2 个答案:

答案 0 :(得分:3)

事实证明这很简单。只需为要打包的每个子项目提供native-packager设置,并且不在聚合项目中提供任何子项目。

我通过相应修改https://github.com/muuki88/sbt-native-packager-examples/tree/master/multi-module-build进行了测试:

import NativePackagerKeys._

name := "mukis-fullstack"

// used like the groupId in maven
organization in ThisBuild := "de.mukis"

// all sub projects have the same version
version in ThisBuild := "1.0"

scalaVersion in ThisBuild := "2.11.2"

// common dependencies
libraryDependencies in ThisBuild ++= Seq(
    "com.typesafe" % "config" % "1.2.0"
)

// this is the root project, aggregating all sub projects
lazy val root = Project(
    id = "root",
    base = file("."),
    // configure your native packaging settings here
//    settings = packageArchetype.java_server++ Seq(
//        maintainer := "John Smith <john.smith@example.com>",
//        packageDescription := "Fullstack Application",
//        packageSummary := "Fullstack Application",
        // entrypoint
//        mainClass in Compile := Some("de.mukis.frontend.ProductionServer")
//    ),
    // always run all commands on each sub project
    aggregate = Seq(frontend, backend, api)
) dependsOn(frontend, backend, api) // this does the actual aggregation

// --------- Project Frontend ------------------
lazy val frontend = Project(
    id = "frontend",
    base = file("frontend"),
    settings = packageArchetype.java_server++ Seq(
       maintainer := "John Smith <john.smith@example.com>",
       packageDescription := "Frontend appplication",
       mainClass in Compile := Some("de.mukis.frontend.ProductionServer")
    )
) dependsOn(api)


// --------- Project Backend ----------------
lazy val backend = Project(
    id = "backend",
    base = file("backend"),
    settings = packageArchetype.java_server++ Seq(
        maintainer := "John Smith <john.smith@example.com>",
        packageDescription := "Fullstack Application",
        packageSummary := "Fullstack Application",
        // entrypoint
        mainClass in Compile := Some("de.mukis.frontend.ProductionServer")
    )
) dependsOn(api)

// --------- Project API ------------------
lazy val api = Project(
    id = "api",
    base = file("api")

结果:

debian:packageBin
...misc messages elided...
[info] dpkg-deb: building package `frontend' in `../frontend_1.0_all.deb'.
[info] dpkg-deb: building package `backend' in `../backend_1.0_all.deb'.

答案 1 :(得分:1)

对于刚到这里的人来说,最新的答案可能是:

lazy val root = (project in file("."))
    .aggregate(common, frontend, backend)

lazy val common = (project in file("common"))
lazy val frontend = (project in file("frontend"))
   .enablePlugins(JavaServerAppPackaging)
lazy val backend = (project in file("backend"))
   .dependsOn(common)
   .enablePlugins(JavaAppPackaging)
   .settings(javaPackagingSettings)

lazy val javaPackagingSettings = Seq(
   // follow sbt-native-packager to identify settings you need
)

说明

以下是支持上述配置的方案

  • 项目root是父项,我们不想打包。它会汇总其他子项目。
  • 项目common是一种库,我们也不想打包
  • 项目backend取决于common的库。
  • 项目frontend是一个独立的项目,打包为具有默认配置的Java服务器应用程序