如何在 SBT 中定义另一个编译范围? [英] How to define another compilation scope in SBT?

查看:41
本文介绍了如何在 SBT 中定义另一个编译范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,SBT 将 src/mainsrc/tests 下的源代码编译为 target/scala-[version]/classestarget/scala-[version]/test-classes,分别.我想定义另一个名为 core 的组,我可以将其放入 src/core/javasrc/core/scala 并拥有它编译为单独的类路径.我该怎么做呢?

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?

我的动机:我想要有单独的类文件组,因为我想在开发过程中重新编译和重新加载新的应用程序代码,而不需要为正在运行的应用程序重新启动 JVM 进程.所以核心类将在应用程序启动时加载,它们将使用自定义类加载器从 src/main 加载其他所有内容.后面的那些类将是可重新加载的.我需要这样做是因为我正在编写一个音乐程序,它通过 JNI 加载一个软件乐器,加载需要很长时间.在开发过程中重新启动应用程序浪费了太多时间.

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.

我主要是需要把class文件分开.如果我要生产 jar,我会想要 myapp-core.jar 和 myapp-main.jar,但这并不重要,因为这比最终产品更适合开发.

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.

第一次尝试:

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

给出这个错误:

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

我现在要去阅读有关范围的信息...

I'll go read about scopes now...

推荐答案

有一个 高级配置示例 在 sbt 文档中显示了自定义编译配置的许多方面.

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

一个基本的例子是:

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
     )
}

通过fullClasspath in Core 任务访问编译后的核心类路径.

Access the compiled core classpath via the fullClasspath in Core task.

这篇关于如何在 SBT 中定义另一个编译范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆