Travis Kaufman
Travis Kaufman

Reputation: 2937

sbt native packager: cannot locate resource when executable is run

I am working on an application that uses sbt-native-packager with the akka_application archetype.

I have a directory structure that looks like:

src/
  main/
    resources/
      application.conf
      somePrivateKey.p12

Within my code, I have a method to return a file handle to the p12 file

def getSomePrivateKey: java.io.File = {
  val cl = this.getClass.getClassLoader
  val fileUrl = cl.getResource("somePrivateKey.p12")
  new java.io.File(fileUrl.getFile())
}

When I use this from the command line via sbt console, it works perfectly. I can easily work with the file, as expected.

However, when I run sbt stage and then run the executable it creates, I get a FileNotFoundException when trying to open the file.

How can I tell sbt-native-packager to copy all of the files within resources onto the created JAR's ClassPath? I've read and re-read the sbt-native-packager documentation, tried adding the resource to Universal, and it still happens every time. Is there another way I should be approaching this?

Upvotes: 4

Views: 1758

Answers (2)

Alexey Novakov
Alexey Novakov

Reputation: 764

Update to what Muki wrote above for option #2

mappings in Universal ++= directory("src/main/resources")

By default this folder should appear in /opt/docker/resources

Upvotes: 0

Muki
Muki

Reputation: 3641

First of all, I recommend against using getClassLoader to load resouces. Try always to use relatives paths, like baseDirectory.value / "src" / "main" / "resources" or better "resourceDirectory.value / "your-resources".

For sbt-native-packager there are several ways to include files.

#1 default folders

Just place your somePrivateKey.p12 in on of the following folders

  • src/universal/conf/ For all distributions (zip, deb, msi, etc.)
  • src/linux/conf/ Only for linux distributions
  • src/windows/conf/ Only for windows distributions

You can read about this in more detail at Adding Configuration and Generating Files.

#2 manually with helper methods

The simple way for one file is very basic

mappings in Universal += resourceDirectory.value / "somePrivateKey.p12"

Furthermore you have helpers for directory operations. See the Mappingshelper Docs for more information.

Upvotes: 1

Related Questions