sbt - exclude certain dependency only during publi

2019-04-21 20:08发布

I'm building a utility library that can be used together with one of 1.0, 1.1, 1.2 version of Apache Spark.

Since they are all binary backwards-compatible, I'd like to let the user decide which spark version to use (by manually adding spark-core with preferred version as a dependency along with my library), and do not impose any version restriction in the library's POM. Otherwise it will annoy users with dependency eviction warnings.

Is it possible to make sbt omit a library dependency in the published POM while not changing any compilation behaviour?

2条回答
Bombasti
2楼-- · 2019-04-21 20:32

The following is the sbt setting I wrote using sjrd's help.

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
}
查看更多
Viruses.
3楼-- · 2019-04-21 20:35

Yes, the provided configuration is specifically designed for that:

libraryDependencies += "org" %% "artifact" % "1.0" % "provided"

will put said library on the classpath during compilation, but not in the POM file.

查看更多
登录 后发表回答