【问题标题】:Produce multiple zip artifacts from one sbt module从一个 sbt 模块生成多个 zip 工件
【发布时间】:2017-01-23 16:47:31
【问题描述】:

我的项目结构如下:

my-project/
  build.sbt
  ...
  app/
  ...
  config/
    dev/
      file1.properties
      file2.properties
    test/
      file1.properties
      file2.properties
    prod/
      file1.properties
      file2.properties

模块应用程序包含一些 scala 源代码并生成一个普通的 jar 文件。

问题出在配置模块上。我需要做的是在 build.sbt 中创建一些配置,它将从 config 中获取每个文件夹并将其内容放入单独的 zip 文件中。

结果应该如下:

my-project-config-dev-1.1.zip ~>
  file1.properties
  file2.properties
my-project-config-uat-1.1.zip ~>
  file1.properties
  file2.properties
my-project-config-prod-1.1.zip ~>
  file1.properties
  file2.properties

1.1 是项目的任意版本。

配置应该以这样的方式工作,当我添加新环境和新配置文件时,将生成更多 zip 文件。在另一个任务中,所有这些 zip 文件都应该发布到 Nexus。

有什么建议吗?

【问题讨论】:

    标签: scala sbt sbt-native-packager


    【解决方案1】:

    我设法通过创建一个模块config 来解决该问题,然后为每个环境创建一个单独的子模块,因此项目结构看起来与所描述的完全一样。现在一切都在build.sbt 中进行正确配置。

    以下是我为实现我想要的目标所做的总体思路。

    lazy val config = (project in file("config")).
      enablePlugins(UniversalPlugin).
      settings(
        name := "my-project",
        version := "1.1",
        publish in Universal := { },       // disable publishing of config module
        publishLocal in Universal := { }
      ).
      aggregate(configDev, configUat, configProd)
    
    lazy val environment = settingKey[String]("Target environment")
    
    lazy val commonSettings = makeDeploymentSettings(Universal, packageBin in Universal, "zip") ++ Seq(  // set package format
      name := "my-project-config",
      version := "1.1",
      environment := baseDirectory.value.getName,                                               // set 'environment' variable based on a configuration folder name
      topLevelDirectory := None,                                                                // set top level directory for each package
      packageName in Universal := s"my-project-config-${environment.value}-${version.value}",   // set package name (example: my-project-config-dev-1.1)
      mappings in Universal ++= contentOf(baseDirectory.value).filterNot { case (_, path) =>    // do not include target folder
        path contains "target"
      }
    )
    
    lazy val configDev = (project in file("config/dev")).enablePlugins(UniversalPlugin).settings(commonSettings: _*)
    lazy val configUat = (project in file("config/uat")).enablePlugins(UniversalPlugin).settings(commonSettings: _*)
    lazy val configProd = (project in file("config/prod")).enablePlugins(UniversalPlugin).settings(commonSettings: _*)
    

    UniversalPlugin 是高度可配置的,尽管起初可能并非所有配置选项都一目了然。我建议阅读它的文档并查看源代码。

    要实际打包工件,已发出以下命令:

    sbt config/universal:packageBin
    

    出版:

    sbt config/universal:publish
    

    从上面可以看出,添加新环境非常容易——只需要添加一个新文件夹和build.sbt 中的一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2014-07-01
      • 2017-10-02
      • 2021-10-03
      • 2019-08-18
      相关资源
      最近更新 更多