在SBT中的“package”时从发布的应用程序中排除插件的libraryDependencies

时间:2014-01-08 04:18:19

标签: sbt scoverage

在我的SBT项目中,我使用sbt-scoverage插件。我做了文档说的内容,并将ScoverageSbtPlugin.instrumentSettings添加到build.sbt。到目前为止一切都很好。

当我package我的应用时,我可以在pom.xml中看到有一个不应该存在的依赖:

<dependency>
  <groupId>com.sksamuel.scoverage</groupId>
  <artifactId>scalac-scoverage-plugin</artifactId>
  <version>0.95.4</version>
</dependency>

这是sbt-scoverage插件的库依赖项,我不想在我发布的应用程序中作为依赖项。

我相信这种依赖关系是由ScoverageSbtPlugin.scala中的以下代码创建的:

libraryDependencies += "com.sksamuel.scoverage" %% "scalac-scoverage-plugin" %
    ScalacScoveragePluginVersion % scoverage.name

任何人都可以告诉我如何在运行sbt scoverage:test时添加此依赖项吗?

2 个答案:

答案 0 :(得分:2)

正如您所注意到的那样,scoverage的编写方式会强制将此依赖项添加到Compile中的libraryDependencies中。但是,一种解决方法是在sbt中使用makePomConfiguration设置。您可以对构造的POM执行转换以删除添加的依赖项,而不会影响scoverage的工作方式。下面我做了一个构建,将过滤掉你POM中的scoverage依赖。我使用了.scala文件,因为你无法在build.sbt pre 0.13中定义对象。所以这个文件将位于project / Build.scala。

import sbt.Keys._
import sbt._
import scala.xml.{Elem, Node}
import scala.xml.transform.{RuleTransformer, RewriteRule}

object theBuild extends Build {

  object FilterBadDependency extends RewriteRule {

      override def transform(n: Node): Seq[Node] = n match {
        /**
         * When we find the dependencies node we want to rewrite it removing any of
         * the scoverage dependencies.
         */
        case dependencies @ Elem(_, "dependencies", _, _, _*) =>
          <dependencies>
            {
              dependencies.child filter { dep =>
                (dep \ "groupId").text != "com.sksamuel.scoverage"
              }
            }
          </dependencies>

        /**
         * Otherwise we just skip over the node and do nothing
         */
        case other => other
      }

  }

  object TransformFilterBadDependencies extends RuleTransformer(FilterBadDependency)

  val project = Project(
    id = "test-build",
    base = file(".")
  ).settings(
      ScoverageSbtPlugin.instrumentSettings: _*
  ).settings(
      /**
       * Here we alter our make pom configuration so that our transformation is applied to
       * the constructed pom
       */
    makePomConfiguration ~= { config =>
      config.copy(process = TransformFilterBadDependencies)
    })
}

答案 1 :(得分:2)

我来了解决方案。我已经取代了这个:

ivyConfigurations ++= Seq(scoverage, scoverageTest)

用这个:

ivyConfigurations ++= Seq(scoverage hide, scoverageTest hide)

这是变更集: https://github.com/scoverage/sbt-scoverage/commit/6d7ebe07482933f588e9feb23f80eeed2aa14f62

我很感激任何人对此的看法。它“在我的机器上”工作。