Blackcat123
Blackcat123

Reputation: 71

sbt package separate jars for different test types

We have just upgraded to using sbt 12.2 on our project. I would like to have our tests packaged in separate jars such as: project-unit.jar, project-integration.jar

Our current folder structure is:

src
 main
   ...
 test
   scala
     com.company.unit
     com.company.integration
     com.company.functional

Any suggestion is very much appreciated.

Thanks

Upvotes: 1

Views: 229

Answers (1)

Blackcat123
Blackcat123

Reputation: 71

I have found a way to do this by:

  1. Create a packaging task for the appropriate test type:

    val integrationPackaging = TaskKey[File]("package-integration")
    
  2. Add package settings to the test settings:

    lazy val integrationTestSettings = createTestSettings("integration", IntegrationTests) ++ createPackageSettings("integration", IntegrationTests, integrationPackaging)
    
    private def createPackageSettings(testType: String, testConfiguration: Configuration, packagingTaskKey: TaskKey[sbt.File]) = {
      inConfig(testConfiguration)(Defaults.testSettings) ++
        Defaults.packageTaskSettings(packagingTaskKey, mappings in (Test, packageBin)) ++
        (artifactName in packagingTaskKey := {(scalaversion, moduleId, artifact) => "%s_%s-%s-%s.jar" format (moduleId.name, scalaversion.binary, buildVersion, testType)}) ++
        (mappings in packagingTaskKey ~= { (ms: Seq[(File,String)]) =>
          ms.filter { case (file, path) =>{ !file.getName.endsWith(".class") || file.getAbsolutePath.contains(testType) }
          }
        })
    }
    

Note in the create package settings, I had to:

  • add default test settings to ensure all test settings are inherited
  • add default test package task settings (using mappings in Test packageBin)
  • add artifactName (what you want the jar name to be)
  • add specific mappings for the package task key (created in step 1 above), this is the crux of it, I had to define a filter that ensures only files with the path containing the testType (e.g. "integration", "unit" or "functional") are selected and resource files (e.g. files that do not end with ".class")

Upvotes: 1

Related Questions