如何将sbt-native-packager与多个项目一起使用

时间:2016-06-20 07:41:19

标签: scala sbt sbt-assembly sbt-native-packager

我正在将几个大型scala项目合并为一个。目前,他们使用剪切和粘贴(不是很多代码)共享代码,我正在制作它,以便有一个共同的实用工具罐。大多数这些项目都部署在亚马逊云上,并使用sbt-native-packager打包它们

我的问题是我不知道如何使用多个项目配置sbt本机管理器。在完成所有初步工作后进入项目,我也不是很熟悉它。

我把问题减少到最小的我可以得到它。在https://github.com/stave-escura/multiprojectissue.git有一个git repo,用于演示问题

在项目/ plugins.sbt中,重要的行是

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.6")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.0")

build.sbt太长了,无法放入此处,但它的粗略形状是

import com.typesafe.sbt.SbtNativePackager
import com.typesafe.sbt.packager.archetypes.JavaServerAppPackaging

enablePlugins(SbtNativePackager)

enablePlugins(JavaServerAppPackaging)

lazy val commonSettings = Seq(
  ...
  scalaVersion := "2.11.8",
  assemblyJarName in assembly := s"${name.value}.jar",
  assemblyMergeStrategy in assembly := {
    case "BUILD" => MergeStrategy.discard
    case other => MergeStrategy.defaultMergeStrategy(other)
  }
)

lazy val project1Settings = commonSettings ++ Seq(
  rpmVendor := "someOrganisation",
  packageDescription in Rpm := "Some description1",
  rpmLicense := Some("Copyright 2016 someOrganisation. All rights reserved."),
  rpmRequirements := Seq(
    "java-1.8.0-openjdk"
  ),
  version in Rpm := "1",
  rpmRelease := version.value,
  rpmDaemonLogFile := s"${name.value}.log",
  daemonUser in Linux := "someUserName",
  daemonGroup in Linux := (daemonUser in Linux).value,
  rpmPost := Some(
    """|chkconfig --add someService1
      |chkconfig someService1 on
    """.stripMargin),
  linuxPackageMappings ++= Seq()    // <--- line 53
)


lazy val project2Settings = commonSettings ++ Seq(
  identical to project 1 settings except
  packageDescription in Rpm := "Some description2",
  rpmPost := Some(
    """|chkconfig --add someService2
      |chkconfig someService2 on
    """.stripMargin),
  linuxPackageMappings ++= Seq()   // <--- line 72
)

lazy val project1 = (project in file("components/service1")).settings(project1Settings: _*)

lazy val project2 = (project in file("components/service2")).settings(project2Settings: _*)

在真实的项目中,linuxPackageMappings相当复杂,是我的问题。它们在项目之间有所不同。当我将build.sbt加载到sbt时,我得到错误

References to undefined settings: 

  project2/*:linuxPackageMappings from project2/*:linuxPackageMappings (~/git/multiprojectissue/build.sbt:72)

  project1/*:linuxPackageMappings from project1/*:linuxPackageMappings (~/git/multiprojectissue/build.sbt:53)

    at sbt.Init$class.Uninitialized(Settings.scala:265)
    ...
    at xsbt.boot.Boot.main(Boot.scala)
[error] References to undefined settings: 
[error] 
[error]   project2/*:linuxPackageMappings from project2/*:linuxPackageMappings (/Users/ricep02/git/multiprojectissue/build.sbt:72)
[error] 
[error]   project1/*:linuxPackageMappings from project1/*:linuxPackageMappings (/Users/ricep02/git/multiprojectissue/build.sbt:53)

第53和72行标记在我上面包含的build.sbt片段中。

我不知道的事情 我在这方面不够熟练,不知道为什么该项目有sbt-assembly和sbt-package管理器。我已经通过plugins.sbt对sbt-assembly进行了评论,但仍然遇到了这个问题,所以我现在还没想到它的贡献

我尝试过的事情 我已经尝试将子项目设置放入与实际组件关联的build.sbt文件中。我得到同样的问题。 我也很好地看了https://github.com/muuki88/sbt-native-packager-examples/tree/master/multi-module-build这样的示例项目,并且非常仔细地阅读了http://www.scala-sbt.org/sbt-native-packager/的文档,尽管我没有经验丰富的#39;用它:只需阅读文档&#39;水平。我还查看了How to setup sbt-native-packager in a single module project with multiple mainsHow to create a basic project setup using sbt-native-packager

等问题

有效的方法 将packageMappings留在&#39;主体&#39; build.sbt没有引起加载问题&#39;但我还不知道它是否会真正构建包裹!

软件堆栈

  • 操作系统:MacOs
  • Java:1.8.0_74-b02
  • Scala:2.11.8
  • sbt:0.13.8
  • sbt-native-packager 1.0.6

1 个答案:

答案 0 :(得分:0)

看起来需要在其设置中为SbtNativePackagerproject1启用project2

尝试添加enablePlugins(SbtNativePackager)

来修改设置
lazy val project1Settings = commonSettings ++ Seq(
    rpmVendor := "someOrganisation",
    packageDescription in Rpm := "Some description1",
    rpmLicense := Some("Copyright 2016 someOrganisation. All rights reserved."),
    rpmRequirements := Seq(
        "java-1.8.0-openjdk"
    ),
    version in Rpm := "1",
    rpmRelease := version.value,
    rpmDaemonLogFile := s"${name.value}.log",
    daemonUser in Linux := "someUserName",
    daemonGroup in Linux := (daemonUser in Linux).value,
    rpmPost := Some(
        """|chkconfig --add someService1
           |chkconfig someService1 on
        """.stripMargin),
    enablePlugins(SbtNativePackager),
    linuxPackageMappings ++= Seq()    // <--- line 53
)

也类似于项目2。

我建议您购买“SBT in Action”这样的书,以帮助您开始使用“简单”构建工具。