sbt - 仅在发布期间排除某些依赖关系

lyo*_*omi 9 scala pom.xml sbt apache-spark

我正在构建一个实用程序库,可以与1.0,1.1,1.2版本的Apache Spark一起使用.

由于它们都是二进制向后兼容的,我想让用户决定使用哪个spark版本(通过手动添加spark-core首选版本作为依赖项和我的库),并且不要在库的POM中强加任何版本限制.否则,它将使用户依赖驱逐警告骚扰用户.

是否可以在不更改任何编译行为的情况下使sbt在已发布的POM中省略库依赖项?

sjr*_*jrd 5

是的,provided配置是专门为此设计的:

libraryDependencies += "org" %% "artifact" % "1.0" % "provided"
Run Code Online (Sandbox Code Playgroud)

将在编译期间将所述库放在类路径上,但不在POM文件中.


lyo*_*omi 5

以下是我使用sjrd帮助编写的sbt设置.

import scala.xml.{Node => XmlNode, NodeSeq => XmlNodeSeq, _}
import scala.xml.transform.{RewriteRule, RuleTransformer}

pomPostProcess := { (node: XmlNode) =>
  new RuleTransformer(new RewriteRule {
    override def transform(node: XmlNode): XmlNodeSeq = node match {
      case e: Elem if e.label == "dependency" && e.child.exists(child => child.label == "scope" && child.text == "provided") =>
        val organization = e.child.filter(_.label == "groupId").flatMap(_.text).mkString
        val artifact = e.child.filter(_.label == "artifactId").flatMap(_.text).mkString
        val version = e.child.filter(_.label == "version").flatMap(_.text).mkString
        Comment(s"provided dependency $organization#$artifact;$version has been omitted")
      case _ => node
    }
  }).transform(node).head
}
Run Code Online (Sandbox Code Playgroud)