Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SBT IntegrationTest configuration from Scala objects

To make our multi-project build more manageable we split up our Build.scala file into several files, e.g. Dependencies.scala contains all dependencies:

import sbt._

object Dependencies {
  val slf4j_api = "org.slf4j" % "slf4j-api" % "1.7.7"
  ...
}

We want to add integration tests to our build. Following the SBT documentation we added

object Build extends sbt.Build {
  import Dependencies._
  import BuildSettings._
  import Version._
  import MergeStrategies.custom

  lazy val root = Project(
    id = "root",
    base = file("."),
    settings = buildSettings ++ Seq(Git.checkNoLocalChanges, TestReport.testReport)
  ).configs(IntegrationTest).settings(Defaults.itSettings: _*)
  ...
}

where Dependencies, BuildSettings, Version and MergeStrategies are custom Scala objects definied in their own files.

Following the documentation we want to add some dependencies for the IntegrationTest configuration in Dependencies.scala:

import sbt._

object Dependencies {

  val slf4j_api = "org.slf4j" % "slf4j-api" % "1.7.7"

  val junit = "junit" % "junit" % "4.11" % "test,it"
...
}

Unfortunately this breaks the build:

java.lang.IllegalArgumentException: Cannot add dependency 'junit#junit;4.11' to configuration 'it' of module ... because this configuration doesn't exist!

I guess I need to import the IntegrationTest configuration. I tried importing the IntegrationTest configuration in Dependencies.scala:

import sbt.Configurations.IntegrationTest

IntegrationTest is a lazy val defined in the Configurations object:

object Configurations {
  ...
  lazy val IntegrationTest = config("it") extend (Runtime)
  ...
 }

But that did not solve the problem.

Does someone has an idea how to solve this?

like image 298
Michael Thaler Avatar asked Nov 01 '22 10:11

Michael Thaler


1 Answers

You need to add the config to the Project object before you add the dependency to the Project object.

Your code quotes show you doing the former, but you don't show where you are doing the latter in your quoted code.

Please could you post the full config, or try moving those two around each other?

Here is where the config is added to the Project object in the SBT docs you linked to:

lazy val root = (project in file(".")).
  configs(IntegrationTest).

Your quoted code above which declares a lazy val but does not use it is not sufficient to get the "it" config into use:

lazy val IntegrationTest = config("it") extend (Runtime)
like image 185
Rich Avatar answered Nov 09 '22 11:11

Rich