如何从生成的 pom 中排除具有显式 URL 的库依赖项? [英] How to exclude library dependencies with explicit URL from generated pom?

查看:39
本文介绍了如何从生成的 pom 中排除具有显式 URL 的库依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Scala Migrations 项目从 ant/ivy 迁移到 sbt.它可以选择使用 log4jdbc 作为在 任何公共 Maven 存储库中不存在的库依赖项(据我所知).

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"

我希望生成的 POM 不包含 log4jdbc,因为它不在任何存储库中.这是一个正确的假设,即不列出 log4jdbc POM 会更好吗?另外,列出它对使用 sbt 的 Scala Migrations 用户来说不是更好吗?

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?

我编写了以下设置以从 POM 中删除 log4jdbc 依赖项.有没有更好、更简单的方法?是否可以将设置添加到 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)
}

推荐答案

因为您提到了 POM,所以我假设您想要支持 Maven 用户或者想要发布到 Maven 存储库.如果不是这样,您不需要发布到 POM,您可以像在 Ant/Ivy 设置中一样使用 Ivy 元数据.

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.

因为您了解 Ivy,所以 from(URL) 方法本质上是通过声明一个自定义工件并将其 from 属性设置为 URL 来实现的.独立于 Maven/POM,Ivy 不会在交付的 Ivy 文件中包含自定义工件.(至少,我相信这是标准的 Ivy 行为,而不是 sbt 配置 Ivy 要做的事情.)

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.)

不过,也没有办法为 pom.xml 中的依赖项提供 URL.您如何处理此问题可能取决于您希望客户端做什么,但一种相当通用的解决方案是将依赖项声明为可选:

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"

客户端需要显式声明依赖项才能使用它.因为它不是存储库,所以 sbt 用户仍然需要复制 from "..." 声明.Maven 用户只能使用存储库中的依赖项,尽管他们可以相当轻松地手动将其安装到本地存储库中.

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.

这篇关于如何从生成的 pom 中排除具有显式 URL 的库依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆