How to define another compilation scope in SBT?

寵の児 提交于 2019-12-23 09:51:59

问题


By default, SBT compiles source under src/main and src/tests to target/scala-[version]/classes and target/scala-[version]/test-classes, respectively. I'd like to define another group called core that I can put in src/core/java or src/core/scala and have that compiled to a separate class path. How do I do this?

My motive: I want to have separate groups of class files, because I want to recompile and reload new application code during development, without restarting the JVM process for the running application. So the core classes will load when the application starts, and they will use a custom classloader to load everything else from src/main. Those latter classes will be reloadable. I need to do this because I'm writing a music program that loads a software instrument through JNI, which takes a long time to load. Rebooting the app during development wastes too much time.

I mainly need to separate the class files. If I were producing jars, I'd want myapp-core.jar and myapp-main.jar, but that's not as important, since this is for development more than the final product.

A first attempt:

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 ?

I'll go read about scopes now...


回答1:


There is an advanced configurations example in the sbt documentation that shows many aspects of custom compilation configurations.

A basic example is:

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 the compiled core classpath via the fullClasspath in Core task.



来源:https://stackoverflow.com/questions/14656592/how-to-define-another-compilation-scope-in-sbt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!