1

I am using an external maven project where the generated jar includes a binary file foo/bar/file.dat whose exact path in the sources is src/main/foo/bar/file.dat. This file is needed for some reason otherwise the code throws an exception.

I tried to convert it to an SBT project but the file does not get included in the jar.

I tried adding includeFilter in Compile := "parser.dat", but this gives me an error:

IO error while decoding .../src/main/foo/bar/file.dat with UTF-8: MALFORMED[1]
Please try specifying another one using the -encoding option

How do I tell SBT to include the file "as-is" in the compiled jar?

EDIT: The linked question is unrelated. The question asks "how to read resources correctly?", while I want to know "assuming that this is the way I have to read resources, how do I create the jar correctly?".

I want to use the 3rd party library as is, without changing the code.

Jus12
  • 17,824
  • 28
  • 99
  • 157
  • Possible duplicate of [Reading a resource file from within jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) – TheChubbyPanda May 12 '19 at 11:35
  • @TheChubbyPanda Its not a duplicate, as I have mentioned in the edit. – Jus12 May 12 '19 at 13:01
  • Instead of using the path "src/.../file.dat" use "/foo/.../file.dat" as mentioned in one of the answers of the potential duplicate. Gives a relative path, surely? – TheChubbyPanda May 12 '19 at 13:05
  • The response of Mario is right to patch your problem, but you must think about following the standard layout and then you will not need to do anything. Why don't you have the resource to add inside of the `src/main/resources` folder? sbt is following the same layout that maven. https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – angelcervera May 12 '19 at 22:55
  • @angelcervera Unfortunately, I am not the author of the code with the problem since its an external library. I may eventually get around to fixing their code but right now its a lower priority. – Jus12 May 13 '19 at 09:13

1 Answers1

6

Try using mappings to add a custom file to the package like so

Compile / packageBin / mappings += {
  (baseDirectory.value / "src" / "main" / "foo" / "bar" / "file.dat") -> "foo/bar/file.dat"
}

Executing sbt package should now produce a jar with file.dat included.


Note that we cannot simply write

mappings += ...

because mappings is scoped by configuration and particular task, which we can see from Related section upon executing inspect mappings

inspect mappings
[info] No entry for key.
[info] Description:
[info]  Defines the mappings from a file to a path, used by packaging, for example.
...
[info] Related:
[info]  Test / packageDoc / mappings
[info]  Test / packageSrc / mappings
[info]  Runtime / packageBin / mappings
[info]  Compile / packageDoc / mappings
[info]  Test / packageBin / mappings
[info]  Compile / packageSrc / mappings
[info]  Compile / packageBin / mappings
Mario Galic
  • 47,285
  • 6
  • 56
  • 98