How to make sbt-native-packager refrain from putting my resources in jar file?

I use the sbt-native-packager plugin to create a startup script for my application, which is very convenient, because this plugin generates the correct specification of the class path with all the dependencies of my library. I do not distribute this application, so I do not collect all this in one archive. I just use the lib directory generated by sbt-native-packager, which contains all the jar files my project depends on, both third-party libraries and a jar file containing my own class and resource files.

In my src/main/resources directory, I have files that I want to edit without using sbt-native-packager to restore the entire installation, such as configuration files. This is complicated because these files are archived in a jar file with all my classes.

Question: how can I tell sbt-native-packager not to put resource files in a jar file, but generate a start-script with the correct class path for these resource files, which will be located and read by me like they are now from the jar file? If this means that all my class files from the jar file are fine, if the files from src/main/resources remain as files that I can change without calling the sbt stage again and until the start-script works.

+5
source share
1 answer

While you can filter these resources, I would suggest placing them in a different directory and adding them to the class path.

Changing the initial script generated by sbt-native-packager is a bit cumbersome since the class com.typesafe.sbt.packager.archetypes.JavaAppBashScript , which generates the class path, prefix all paths with $lib_dir/ . The cleanest approach would probably be to provide your own implementation and use it to generate bashScriptDefines .

A simple but hacky way would be to simply add the following lines to 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) 

This will add $lib_dir/../config to the beginning of the script path. If your application should run on Windows, you will need to provide similar settings for batScriptDefines .

+1
source

Source: https://habr.com/ru/post/1201374/


All Articles