How to define a different compilation area in SBT?

By default, SBT compiles the source to src/mainand src/testsbefore target/scala-[version]/classesand, target/scala-[version]/test-classesrespectively. I would like to define another group called the kernel, which I can place in src/core/javaor src/core/scalaand compile it into a separate class path. How should I do it?

My motive: I want to have separate groups of class files because I want to recompile and reload the new application code during development without restarting the JVM process for the running application. Thus, the main classes will be loaded at application startup, and they will use the custom class loader to load everything else from src / main. These last classes will be recharged. I need to do this because I am writing a music program that downloads a software instrument through JNI, which takes a lot of time to load. Rebooting the application during development takes too much time.

I basically need to separate class files. If I were producing jars, I would like to get myapp-core.jar and myapp-main.jar, but this is not so important, since it is more for development than the final product.

First try:

val Core = config("core")
...
classDirectory in Core <<= crossTarget(t => file(t.getAbsolutePath + "core-classes"))

gives this error:

Reference to undefined setting: 
{.}/*:cross-target from {.}/core:class-directory
    Did you mean *:cross-target ?

Now I will talk about areas ...

+5
source share
1 answer

The sbt documentation has an extended example of configurations that shows many aspects of custom compilation configurations.

Basic example:

object MyBuild extends Build {

  lazy val root = Project(...,
    settings = Defaults.defaultSettings ++ coreSettings
  )

  // Declare the custom configuration.
  lazy val Core = config("core")

  lazy val coreSettings: Seq[Setting[_]] = 
     // Add the src/core/scala/ compilation configuration.
     // This configures sources, classpaths, output directories, REPL, scalac, ...
     inConfig(Core)(Defaults.configSettings) ++ Seq(
        // example dependency just for Core
        libraryDependencies += "org.example" % "demo" % "1.0" % Core,
        // register the custom core configuration
        ivyConfigurations += Core
     )
}

Access a compiled kernel class class using a task fullClasspath in Core.

+5
source

All Articles