Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sbt - exclude certain dependency only during publish

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?

like image 944
lyomi Avatar asked Jan 08 '15 08:01

lyomi


2 Answers

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
}
like image 85
lyomi Avatar answered Sep 27 '22 17:09

lyomi


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.

like image 26
sjrd Avatar answered Sep 27 '22 16:09

sjrd