如何从 sbt 程序集中排除重复的类? [英] How can a duplicate class be excluded from sbt assembly?

查看:48
本文介绍了如何从 sbt 程序集中排除重复的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一种情况,其中两个依赖项具有完全相同的类(因为其中一个依赖项复制了它并包含在它们自己的源中).

We have a situation in which two dependencies have exactly the same class (because one of the dependencies copied it and included in their own source).

这导致 sbt assembly 无法通过重复数据删除检查.

This is causing sbt assembly to fail its deduplication checks.

如何从特定 jar 中排除类?

How can I exclude a class from a particular jar?

推荐答案

您需要一个 mergeStrategy,它将获取其中一个文件.

You need a mergeStrategy, which will take one of the files.

mergeStrategy in assembly := {
    case PathList("path", "to", "your", "DuplicatedClass.class") => MergeStrategy.first
    case x => (mergeStrategy in assembly).value(x)
}

更新

如果你想根据它来自的 JAR 来处理文件,我认为你不能使用程序集插件定义的合并策略.你可以做什么,你可以定义自己的策略.

Update

If you want to handle the file depending on the JAR which it came from, I don't think you can with the merge strategies that assembly plugin defines. What you could do you could define your own strategy.

不过我会颠倒你的情况.我认为问题应该是如何包含来自特定 JAR 的类?".原因是同一个类可以有两个以上的JAR,最后只能包含一个.

I would invert your condition though. I think the question should be "How can I include a class from a particular JAR?". The reason is that there can be more than two JARs having the same class, and you can only include one in the end.

您可以使用 AssemblyUtils.sourceOfFileForMerge 判断文件来自何处.

You can tell from where the file comes by using AssemblyUtils.sourceOfFileForMerge.

import sbtassembly._
import java.io.File
import sbtassembly.Plugin.MergeStrategy

class IncludeFromJar(val jarName: String) extends MergeStrategy {

  val name = "includeFromJar"

  def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
    val (tmp, path, files) = args
    val includedFiles = files.flatMap { f =>
      val (source, _, _, isFromJar) = sbtassembly.AssemblyUtils.sourceOfFileForMerge(tmp, f)
      if(isFromJar && source.getName == jarName) Some(f -> path) else None
    }
    Right(includedFiles)
  }

}

build.sbt

mergeStrategy in assembly := {
    case PathList("path", "to", "your", "DuplicatedClass.class") => new IncludeFromJar("jarname.jar")
    case x => (mergeStrategy in assembly).value(x)
}

这篇关于如何从 sbt 程序集中排除重复的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆