如何防止 SBT 将测试依赖项包含到 POM 中 [英] How to prevent SBT to include test dependencies into the POM

查看:43
本文介绍了如何防止 SBT 将测试依赖项包含到 POM 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在专用测试文件夹下有一个带有测试类的小型实用程序 scala 构建.编译然后 publish-local 在我的本地存储库中创建包.

I have a small utilities scala build with test classes under a dedicated test folder. Compiling and then publish-local creates the package in my local repository.

正如预期的那样,test 文件夹会自动从实用程序包的本地 jar 中排除.

As expected, the test folder is automatically excluded from the local jar of the utilities package.

但是,生成的 POM 仍然包含 sbt.xml 文件中定义的相关依赖项.SBT 依赖项:

However, the resulting POM still contains the related dependencies as defined in the sbt. The SBT dependencies:

libraryDependencies ++= Seq(
  "org.scalactic" %% "scalactic" % "3.0.0" % Test,
  "org.scalatest" %% "scalatest" % "3.0.0" % Test
)

POM 的片段:

<dependency>
    <groupId>org.scalactic</groupId>
    <artifactId>scalactic_2.11</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest_2.11</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

显然需要测试范围,以防止使用此库的另一个项目(主)出现问题.尤其是主项目的测试另外包含了这些测试库,会导致版本冲突等.

The scope clearly needs to be test in order to prevent issues in another project (main) that uses this library. In particular, the testing of the main project otherwise includes these test libraries, which causes version conflicts etc.

由于这些依赖项仅用于未包含的测试包,将它们列在 POM 中似乎很愚蠢.我如何告诉 SBT 不要将这些测试范围依赖项包含到最终的 POM 中?

As these dependencies are only for the not included test package, having them listed in the POM seems silly. How do I tell SBT to not include these test scope dependencies into the final POM?

推荐答案

这里有一个类似的问题:sbt - 排除某些依赖仅在发布期间.

There was a similar question asked here: sbt - exclude certain dependency only during publish.

参考 lyomi 提供的 答案,您可以通过以下方法排除所有 包含子 元素的 code> 元素,包括 testprovided.

Riffing on the answer provided by lyomi, here's how you can exclude all <dependency> elements that contains a child <scope> element, including test and provided.

import scala.xml.{Node => XmlNode, NodeSeq => XmlNodeSeq, _}
import scala.xml.transform.{RewriteRule, RuleTransformer}

// skip dependency elements with a scope
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") =>
        def txt(label: String): String = "\"" + e.child.filter(_.label == label).flatMap(_.text).mkString + "\""
        Comment(s""" scoped dependency ${txt("groupId")} % ${txt("artifactId")} % ${txt("version")} % ${txt("scope")} has been omitted """)
      case _ => node
    }
  }).transform(node).head
}

这应该生成一个看起来像这样的 POM:

This should generate a POM that looks like this:

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.12.5</version>
    </dependency>
    <!-- scoped dependency "org.scalatest" % "scalatest_2.12" % "3.0.5" % "test" has been omitted -->
</dependencies> 

这篇关于如何防止 SBT 将测试依赖项包含到 POM 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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