Blair Zajac
Blair Zajac

Reputation: 4625

How to exclude library dependencies with explicit URL from generated pom?

I'm moving the Scala Migrations project from ant/ivy to sbt. It optionally uses log4jdbc as a library dependency that doesn't exist in any public Maven repository (from what I can find).

libraryDependencies +=
  "log4jdbc" % "log4jdbc" % "1.1" from "http://log4jdbc.googlecode.com/files/log4jdbc4-1.1.jar"

I'd like the generated POM to not include log4jdbc, since it's not in any repository. Is this a correct assumption that the POM will be better without listing log4jdbc? Also, will not listing it work better for Scala Migrations users using sbt?

I wrote the following setting to remove the log4jdbc dependency from the POM. Is there a better, easier way? Could a setting be added to sbt to do this automatically?

// Do not include log4jdbc as a dependency.
pomPostProcess := { (node: scala.xml.Node) =>
  val rewriteRule =
    new scala.xml.transform.RewriteRule {
      override def transform(n: scala.xml.Node): scala.xml.NodeSeq = {
        val name = n.nameToString(new StringBuilder).toString
        if (name == "dependency") {
          if ((n \ "groupId").text == "log4jdbc")
            scala.xml.NodeSeq.Empty
          else
            n
        }
        else {
          n
        }
      }
    }
  val transformer = new scala.xml.transform.RuleTransformer(rewriteRule)
  transformer.transform(node)(0)
}

Upvotes: 5

Views: 1641

Answers (1)

Mark Harrah
Mark Harrah

Reputation: 7019

Because you mention a POM, I assume you want to support Maven users or you want to publish to a Maven repository. If that isn't true, you don't need to publish to a POM and you can just work with Ivy metadata like in the Ant/Ivy setup.

Since you know Ivy, the from(URL) method is essentially implemented by declaring a custom artifact with its from property set to the URL. Independent of Maven/POMs, Ivy doesn't include custom artifacts in the delivered Ivy file. (At least, I believe this is standard Ivy behavior and not something sbt configures Ivy to do.)

There isn't a way to provide the URL for a dependency in a pom.xml either, though. How you handle this might depend on what you expect clients to do, but one fairly general solution is to declare the dependency as optional:

libraryDependencies +=
  "log4jdbc" % "log4jdbc" % "1.1" % "compile,optional" from
    "http://log4jdbc.googlecode.com/files/log4jdbc4-1.1.jar"

Clients would need to explicitly declare the dependency in order to use it. Because it isn't a repository, sbt users would still need to duplicate the from "..." declaration. Maven users can only use dependencies in a repository, although they can install it in their local repository manually fairly easily.

Upvotes: 5

Related Questions