用于sbt中多个测试包的多个jar

问题描述

sbt(自定义任务或插件)中是否有任何方法可以将软件包打包在单独的jar中。

alt

例如:- 在这个示例项目中,应该为包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

解决方法

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

Here是一个更大的例子。

希望这会有所帮助。欢呼快乐,编码愉快! :)