I'm pretty new to sbt-native-packager. What I want is using external conf folder like a resource folder and configure the files inside it differently on each environment such as production, dev and etc.
Project has src/universal/conf and bunch of configuration files under this directory used by libraries at runtime.
I exclude src/universal/conf during build so final jar does not has these conf files as resources. I checked bin/start.sh and only lib/ folder set as classpath.
How can I accomplish it? What's the best practices? I don't think that JavaServerPackaging is more convenient because final jar has several main methods to be used as separate applications on same node. So one dameon application looks like doesn't fit.
There are already a few questions with proper solutions for this
SBT using sbt-native-packager how to create different build files?
What is the recommended way to set JVM options...
Related
What is the best way of using static resources in an SBT based Scala project with a packaging plugin such as sbt-assembly or sbt-native-packager.
We know that by using TypeSafe-Config with sbt-native-packager's universal plugin, we can just put the configuration file in the resources directory under sources. However, what if I wanted my application to have other static sources such as JSON files containing mappings, and models?
I understand that I can just reference the resources directory and read from the file, but would that still work after packaging the application with plugins (assuming the universal or docker plugin in this case)?
If not, what is the correct way to achieve this?
You could use
unmanagedResourceDirectories += (baseDirectory in <project>).value / some / path
to add more directories that are later mapped into the jar as static resources.
Put it into the resources directory, but don't "reference the resources directory and read from the file": use ClassLoader.getResourceAsStream() (or getResources, depending on your requirements) instead. This is the same technique TypeSafe Config and innumerable other libraries use. For this it doesn't matter if you use sbt-native-packager or not.
This approach runs into problems if you need to make these resources available specifically as files (e.g. to feed them to an external process). In this case add them to mappings as shown here:
mappings in Universal in packageBin += file("README") -> "README"
(obviously replacing "README" with the file(s) you need).
I'm writing an sbt plugin to help with deployment. It depends on sbt-native-packager. Principally it adds a deploy task. However, I also need it to copy a bash script run-class.sh into the /bin folder of the package.
How do I copy a file from the sbt plugin to my project? Presently my only idea is to add the file to src/main/resources/run-class.sh in the plugin and generate a file using sbt. Then I can supply a Universal mapping to put the file in the sbt-native-packager package.
Is there an easier way to get a file from the plugin into my sbt project?
You are on the right track with Generating files, specifically Generate resources. You can keep your original file either as a resource or String, but important thing is that the files are generated into resourceManaged in Compile, which is under target. This folder is typically skipped from version control.
I built a universal tarball using sbt-native-packager.
How do I load configuration files like c3p0 and application.conf from /etc/myapp or anyother custom location when I start the app.
I don't want the config files to be part of the distribution tarball itself.
I believe you can use typesafe config's "include" feature to grab from a direct location.
See https://github.com/typesafehub/config#features-of-hocon
That said, this would require you to create different configurations based on where you're installing, if you wanted a global file as the config file.
I'm writing an SBT plugin. In this plugin, I need to add files to the resources directory within the resulting jar.
I don't want to change anything from the source directory (otherwise the user/developer may not understand why files appear to directory he's responsible for), but only in the generated jar.
How can I do that, is there a folder in target that correspond to the resource directory?
It seems like this is done by the "package" command, so if I can override the package command I might be able to do what I want.
Seems like I need to add stuff to resourceManaged.
I have a Maven project in SVN with trunk\Config\TestProjConfig folder having all the XML configuration files and all Java code is in trunk\JavaSource\TestProj. When I check out to my local Eclipse there are 2 projects created one for TestProjConfig and one for TestProj.
How do I test this project? Do I have to edit the build path to make a connection between the two projects? Do I have to write a main class which accesses the AppContext file to invoke the Spring injections?
Note:I have the m2eclipse plugin in Eclipse.
I don't understand why you separated XML configuration from Java sources. It could be a good practice only if you want to reuse your conf in other project, but in this case, I'm not sure that's suitable.
Solution 1
Move all your xml config into JavaSource\TestProj\src\main\resources
If you really don't have to share your configuration or don't have any legacy / corporate / mysterious reason, you should not separate them.
Solution 2
If you REALLY want to separate them, put them in a zip with an assembly, and add a dependency from TestProj to TestProjConfig.
Here an example : Maven - Depend on assembled zip