【问题标题】:Multiple jars for multiple test packages in sbtsbt中多个测试包的多个jar
【发布时间】:2020-12-23 03:48:58
【问题描述】:

在 sbt(自定义任务或插件)中有什么方法可以将包打包到单独的 jar 中。

例如:- 在这个示例项目中,应该为包eg1、eg2、eg3、eg4、eg49、eg50生成6个jar文件

鉴于,任何包之间都没有相互依赖关系。 简化项目供参考:- https://github.com/moglideveloper/MultipleSpecJars


最终编辑:-

我设法使用正则表达式名称(其中正则表达式指向某个包名称)创建了 jar 文件,任务如下:-

sbt createCompactJar

现在,我不知道如何将它重用于一系列正则表达式。

下面是 createCompactJar 的简化逻辑:-

lazy val multipleSpecJars = (project in file("."))
  .settings(libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.2")

//A different configuration which would redefine the packageBin task within this new configuration
//so it does not change the original definition of packageBin
lazy val CompactJar = config("compactJar").extend(Compile)
inConfig(CompactJar)(Defaults.compileSettings)

val allRegexes = List("eg1", "eg2", "eg3", "eg4", "eg49", "eg50")

/*
Below logic only works for one regex (regexToInclude),
How to convert this logic for a sequence of regexes(allRegexes) ?
 */
val regexToInclude = "eg3"

lazy val createCompactJar = taskKey[Unit]("create compact jar based on regex")

/*
createCompactJar
1. Depends on packageBin sbt task.
2. call packageBin and then rename jar that is generated by
   packageBin in same directory to <regex>.jar
 */
createCompactJar := {
  println("now creating jar for regex : " + regexToInclude)
  (packageBin in CompactJar).value

  //Logic to rename generated jar file to <regex>.jar
  val jarFileDir = (baseDirectory).value + "/target/scala-2.12"
  val jarFilePath = jarFileDir + "/multiplespecjars_2.12-0.1.0-SNAPSHOT-compactJar.jar"
  val sourceFile = new File(jarFilePath)
  val destinationFile = new File(jarFileDir + "/" + regexToInclude + ".jar")
  println(s"renaming $sourceFile to $destinationFile")
  sourceFile.renameTo(destinationFile)
}

mappings in(CompactJar, packageBin) := {
  val original = (mappings in(CompactJar, packageBin)).value
  original.filter { case (file, toPath) => toPath.startsWith(regexToInclude) }
}

unmanagedSourceDirectories in CompactJar := (unmanagedSourceDirectories in Compile).value

【问题讨论】:

    标签: sbt sbt-native-packager sbt-plugin


    【解决方案1】:

    我会选择subprojects。我不得不承认我并不完全理解你的用例(你为什么要首先打包测试?),但是子项目允许你独立地打包东西。您甚至可以获得开箱即用的并行编译,甚至可以在子项目之间建立依赖关系,但您似乎不需要任何依赖。

    Here 是一个更大的例子。

    希望这会有所帮助。干杯和快乐的编码! :)

    【讨论】:

    • 是的,我也只使用子项目。但是对于这个特定的要求,我只能使用包。
    猜你喜欢
    • 1970-01-01
    • 2015-07-30
    • 2017-07-01
    • 2016-04-10
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    相关资源
    最近更新 更多