How to extract dependency jar to specific folder during compilation?

99封情书 提交于 2020-01-14 05:45:26

问题


These are my project's dependencies:

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs,
  "com.company" % "common_2.11" % "2.3.3"
)

In com.company.common_2.11-2.3.3 there is a jar file common_2.11-2.3.3-adtnl.jar.

How can I during compilation process in build.sbt tell SBT to extract its contents to specific folder in my project?


回答1:


Use the following in build.sbt:

def unpackjar(jar: File, to: File): File = {
  println(s"Processing $jar and saving to $to")
  IO.unzip(jar, to)
  jar
}

resourceGenerators in Compile += Def.task {
    val jar = (update in Compile).value
            .select(configurationFilter("compile"))
            .filter(_.name.contains("common"))
            .head
  val to = (target in Compile).value / "unjar"
  unpackjar(jar, to)
  Seq.empty[File]
}.taskValue

It assumes that "common" is a unique part amongst all of your dependencies. You'd need to fix filter otherwise.

It also assumes that you don't really want the files at compile, but a bit later when package is called. You'd need to move the code between Def.task{...} to compile in Compile block like:

compile in Compile <<= (compile in Compile).dependsOn(Def.task {
  val jar = (update in Compile).value
            .select(configurationFilter("compile"))
            .filter(_.name.contains("common"))
            .head
  val to = (target in Compile).value / "unjar"
  unpackjar(jar, to)
  Seq.empty[File]
})


来源:https://stackoverflow.com/questions/26274698/how-to-extract-dependency-jar-to-specific-folder-during-compilation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!