How to read resource in scala project when deployed in docker - scala

im new to scala and sbt and im building a project and reading a model file (.h5) using the org.nd4j.linalg.io.ClassPathResource class. The h5 file is located under the resources folder and it works well when running the project locally using Intelij.
However, when the app runs on the docker i get the following error
java.io.FileNotFoundException: class path resource [scala/Product.class] cannot be opened because it does not exist
when i print the path which the app looking for i see that it is the root of my jar(when local), and root root folder of the machine in the docker environment.
i understand that whatever runs my app in the docker can't access the resources inside my jar.
My question is how do i use resources without changing the code before i deployment.
I have tried adding the following to the build.sbt file
mappings.in(Universal) += {((baseDirectory.value / "main" / "resources" / "my_model.h5"), "my_model.h5")}

Related

Unable to find file under resources folder

When I run my app in sbt console my-app/run (my-app is a module), I get this error
java.io.FileNotFoundException: file:.../target/bg-jobs/sbt_eea980c4/job-3/target/aaa32a5e/b2227e4f/my-app_2.13-0.1.0-SNAPSHOT.jar!/my_file.csv
My directory structure is my-app->src->main->resources->my_file.csv. The way I am accessing the file in my code is:
val file = new File(getClass.getResource("/my_file.csv").getPath)
What am I missing here?
sbt stuck your resources in a jar, so you can't access them as a file. You can use getClass().getResourceAsStream("/my_file.csv") instead.

SBT native packager does not copy file to Docker image

I have an Akka SBT project. It consists of multiple SBT submodules:
common contains resources/logback-prod.xml
app depends on two other submodules
postgres
I build a docker image of the project. The only command which does not affect the image is:
...
dockerPackageMappings in Docker +=
((resourceDirectory in Compile).value / "logback-prod.xml") -> "/opt/docker/conf/logback-prod.xml"
...
This line of code does not copy the logback-prod.xml from the common subproject to the docker /opt/docker/conf/ path
Instead it creates a directory with name "logback-prod.xml" by the path I mentioned above.
What I'm doing wrong?
Usually when you get an empty directory instead of the file in Docker, it means that the source file doesn't exist. I.e. in your case it could mean that your source path is wrong. Try it with copying a file with an absolute path, and see if it gets copied in correctly.
The problem is solved by this line of code:
mappings in Docker += (resourceDirectory in common in Compile).value / "logback-prod.xml" -> "/opt/docker/conf/logback-prod.xml",

Some paths don't exist in "typesafe" repository when I use command ”sbt package“

When I package the scala project using "sbt package", an error occur to me ,like this:
Server access Error: Connection timed out: connect url=https://repo.typesafe.com/typesafe/ivy-releases/com.typesafe.sbt/sbt-git/scala_2.10/sbt_0.13/0.8.5/ivys/ivy.xml
and I input path
https://repo.typesafe.com/typesafe/ivy-releases/com.typesafe.sbt/sbt-git/scala_2.10/sbt_0.13/0.8.5/ivys
into browser I use which browse sites through proxy, it will redirect to
https://dl.bintray.com/typesafe/ivy-releases/com.typesafe.sbt/sbt-git/scala_2.10/sbt_0.13/0.8.5/ivys
I find the following path exists:
https://dl.bintray.com/typesafe/ivy-releases/com.typesafe.sbt/"
but
sbt-git/scala_2.10/sbt_0.13/0.8.5/
Doesn't the project is "marathon" which is written in Scala, the site is "https://github.com/mesosphere/marathon". I download the project, then switch into the root directory of "marathon",and using "sbt package" commder. Are there some problem in repositories?

Debugging OfBiz in Eclipse gives MissingResourceException

I downloaded the OfBiz Java application and the following line throws an MissingResourceException:
ResourceBundle res = ResourceBundle.getBundle(settingsResourceName);
The value of settingsResourceName is "cache", but I cannot find any file called cache.properties or cache_en.properties.
Where should I be looking? I'm new to Java. All my research on SO says there should be such a file.
I imported OfBiz in Eclipse using the Import menu option and selecting Existing Project from File System (I'm not in front of my dev machine so I don't remember the exact wording). But I chose the root folder of the downloaded OfBiz.
I then added the appropriate VM Arguments in the Run Configuration to get it to run properly at least. And that's it, on the first Run I got the above error. I think it has to do with a missing class path but I don't know what to add to class path.
Here is the stack trace:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.ofbiz.base.util.Debug.<clinit>(Debug.java:68)
at org.apache.ofbiz.base.container.ContainerLoader.load(ContainerLoader.java:61)
at org.apache.ofbiz.base.start.StartupControlPanel.loadStartupLoaders(StartupControlPanel.java:202)
at org.apache.ofbiz.base.start.StartupControlPanel.start(StartupControlPanel.java:69)
at org.apache.ofbiz.base.start.Start.main(Start.java:84)
Caused by: java.util.MissingResourceException: Can't find bundle for base name cache, locale en
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:773)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:174)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:170)
at org.apache.ofbiz.base.util.cache.UtilCache.setPropertiesParams(UtilCache.java:166)
at org.apache.ofbiz.base.util.cache.UtilCache.<init>(UtilCache.java:124)
at org.apache.ofbiz.base.util.cache.UtilCache.createUtilCache(UtilCache.java:769)
at org.apache.ofbiz.base.util.UtilProperties.<clinit>(UtilProperties.java:75)
... 5 more
UPDATE:
My mistake, I found two files both called cache.properties in the following folders:
ofbiz-trunk/build/resources/main
ofbiz-trunk/framework/base/config
But these are folders, not packages. I tried putting them in the .classpath but that did not work, I still kept getting the same error.
As suspected, I knew it was because of a missing reference to a class path. After looking at a section on this page: http://www.opensourcestrategies.com/ofbiz/ofbiz_eclipse.php, I learned that I was supposed to go to the Java Build Path and in the Libraries tab, click on Add Class Folder, then point that to ofbiz-trunk/framework/base/config. Which is where I have one of the cache.properties files.

Custom deployment with sbt

I have a Scala application and i want to setup a deployment process result similar to one Akka sbt plugin gives, but i it requires project to be a microkernel. Sbt-assembly is a very cool plugin, but i want to have a free access to my config files, basically i want to have the following app structure:
/app/bin - start script bash file
/config - all my application .conf files
/deploy - my project .jar with classes
/lib - all my dependencies .jar files
/logs - log files
I we checked typesafe sbt-native-packager, it's better, but it could place my .conf file and logs out of jars. All those settings in SBT looks confusing to me, what can i do to with this?
Actually this is not hard to update akka-sbt-plugin to make it work in general, add this file to your project folder and somewhere in your build the following settings:
.settings(Distribution.distSettings: _*)
.settings(mappings in Compile in packageBin ~= { _.filter(!_._1.getName.endsWith(".conf")) })
The first line adds all dist settings to your project and the second one excludes all .conf files from your .jar and reads them from config folder.
Now you have to commands: dist - creates a folder with a structure you've discribed and zipDist which packs it all into .zip file, later you can add this to you publish setting:
addArtifact(Artifact(someName, "zip", "zip"), zipDist)