【问题标题】:How to prevent SBT to include test dependencies into the POM如何防止 SBT 将测试依赖项包含到 POM 中
【发布时间】:2017-05-30 21:58:20
【问题描述】:

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

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

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

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>

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

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

【问题讨论】:

  • 这是% "test",而不是% Test
  • 它应该可以工作scala-sbt.org/1.0/docs/…
  • 但无论如何,您可以尝试使用“test”以确保它不是任何奇怪的 sbt 错误
  • @pedrorijo91 对“test”的更改不会更改包含在 POM 中。

标签: scala sbt


【解决方案1】:

这里有一个类似的问题:sbt - exclude certain dependency only during publish

在 lyomi 提供的 the answer 上重复,这里是您可以排除所有包含子 &lt;scope&gt; 元素的 &lt;dependency&gt; 元素的方法,包括 testprovided

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:

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

【讨论】:

    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 2015-03-05
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多