Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

sbt

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)
}
like image 309
Blair Zajac Avatar asked Sep 10 '12 04:09

Blair Zajac


People also ask

How do you exclude a dependency in Pom?

Exclude a dependency You can use a diagram to exclude a dependency from the project's POM. Select a dependency in the diagram window. From the context menu, choose Exclude. From the list, select the module (if any) where the exclusion definition will be added.

How do you exclude a transitive dependency in Maven?

To avoid this, you can use Maven's <exclusions> tag. Consider below Maven dependency of spring-json library. Adding this dependency of spring-json library would include following jar files automatically. Note how we specified <exclusion> tag to remove spring and cglib dependencies from the transitive dependency tree.

What are exclusions in POM xml?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

Can we exclude a class from Maven dependency?

Excluding a single class in not possible. Within <dependency> tags you can define <exclusions/> . However, these are for entire dependencies. The shade plugin should be handled with care.


1 Answers

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.

like image 85
Mark Harrah Avatar answered Sep 29 '22 06:09

Mark Harrah