7

I'm using the sbt-native-packager plugin to generate a start script for my application, which is very convenient as this plugin generates the correct classpath specification with all my library dependencies. I am not distributing this applictaion, therefore I'm not packaging the entire thing into one tarball. I just use the lib directory generated by sbt-native-packager that contains all the jar-files on which my project depends, both third-party libraries as well as the jar-file that contains my own class and resource files.

In my project's src/main/resources directory I have files that I want to be able to edit without having to use sbt-native-packager to regenerate the entire installation, for example configuration files. This is difficult because those files are zipped up in the jar file with all my classes.

Question: how can I tell sbt-native-packager not to put my resource files into a jar-file, while still generating the start-script with the correct classpath for those resource files to be located and read by my application as they are now from within the jar file? If this means leaving all my class files out of a jar file that is fine, as long as the files from src/main/resources remain as files that I can change without re-invoking sbt stage and as long as the start-script works.

4

1 回答 1

1

虽然可以过滤这些资源,但我建议将它们放入不同的目录并将它们添加到类路径中。

修改由 sbt-native-packager 生成的启动脚本有点麻烦,因为com.typesafe.sbt.packager.archetypes.JavaAppBashScript生成类路径的类在所有路径前加上$lib_dir/. 最干净的方法可能是提供您自己的实现并使用它来生成bashScriptDefines.

一种更简单但很老套的方法是将以下几行添加到您的build.sbt:

packageArchetype.java_server

// add your config files to the classpath for running inside sbt
unmanagedClasspath in Compile += Attributed.blank(sourceDirectory.value/"main"/"config")

// map all files in src/main/config to config in the packaged app
mappings in Universal ++= {
  val configDir = sourceDirectory.value/"main"/"config"
  for {
    file <- (configDir ** AllPassFilter).get
    relative <- file.relativeTo(configDir.getParentFile)
    mapping = file -> relative.getPath
  } yield mapping
}

scriptClasspath ~= (cp => "../config" +: cp)

这将$lib_dir/../config添加到您的启动脚本的类路径中。如果您的应用程序必须在 Windows 上运行,您必须为batScriptDefines.

于 2014-08-31T14:50:40.760 回答