Skip to content

Instantly share code, notes, and snippets.

@teigen
Created December 17, 2021 16:29
Show Gist options
  • Save teigen/16059fa6413a023d939d7ed35925a613 to your computer and use it in GitHub Desktop.
Save teigen/16059fa6413a023d939d7ed35925a613 to your computer and use it in GitHub Desktop.
scala-cli resourceDir does not work with package

A little project demonstrating problems with resourceDir.

16:28 λ tree
.
├── Test.scala
└── resources
    └── data.txt

1 directory, 2 files
16:28 λ cat Test.scala 
// using scala 3.0.2
// using resourceDir "./resources"
@main def main = 
  println(Thread.currentThread().getContextClassLoader.getResource("data.txt"))
16:28 λ cat resources/data.txt 
Hello!

Running it works great :-)

16:29 λ scala-cli run .
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)
file:/Users/jteigen/Development/cli-resource/resources/data.txt
16:29 λ scala-cli package .
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)
Wrote app, run it with
  ./app
16:29 λ ./app
null

Running the packaged version does not :-(

16:29 λ jar xf app coursier/bootstrap/launcher/jars/app-content.jar

Inspecting the packaged jar-file shows the file is missing

16:30 λ jar tf coursier/bootstrap/launcher/jars/app-content.jar 
META-INF/MANIFEST.MF
Test$package.tasty
main.tasty
main.class
Test$package$.class
Test$package.class

Lets export to sbt!

16:30 λ scala-cli export --sbt .
16:30 λ cd dest
16:30 λ cat build.sbt 

// Scala CLI doesn't distinguish main and test sources for now.
Test / sources ++= (Compile / sources).value

scalaVersion := "3.0.2"






Compile / unmanagedClasspath ++= Seq(file("/Users/jteigen/Development/cli-resource/resources"))
Runtime / unmanagedClasspath ++= Seq(file("/Users/jteigen/Development/cli-resource/resources"))




16:30 λ sbt run
[info] welcome to sbt 1.5.5 (Homebrew Java 11.0.12)
[info] loading global plugins from /Users/jteigen/.sbt/1.0/plugins
[info] loading project definition from /Users/jteigen/Development/cli-resource/dest/project
[info] loading settings for project dest from build.sbt ...
[info] set current project to dest (in build file:/Users/jteigen/Development/cli-resource/dest/)
[info] compiling 1 Scala source to /Users/jteigen/Development/cli-resource/dest/target/scala-3.0.2/classes ...
[info] running main 
file:/Users/jteigen/Development/cli-resource/dest/target/bg-jobs/sbt_a8cc4955/target/0020f180/a4662cdd/resources/data.txt
[success] Total time: 4 s, completed Dec 17, 2021, 4:30:56 PM

Running it works. But just like with scala-cli run it find the file on the filesystem, not in the jar. Inspecting the jar reveals why

16:30 λ jar tf target/scala-3.0.2/dest_3-0.1.0-SNAPSHOT.jar 
META-INF/MANIFEST.MF
Test$package$.class
Test$package.class
Test$package.tasty
main.class
main.tasty

If we make some minor adjustments to build.sbt to match what I thought resourceDir did (https://www.scala-sbt.org/1.x/docs/Howto-Customizing-Paths.html#Add+an+additional+resource+directory)

16:31 λ vim build.sbt 
16:31 λ cat build.sbt 

// Scala CLI doesn't distinguish main and test sources for now.
Test / sources ++= (Compile / sources).value

scalaVersion := "3.0.2"

Compile / unmanagedResourceDirectories ++= Seq(file("/Users/jteigen/Development/cli-resource/resources"))

Running shows that the resource file is now resolved from the jar-file, not the filesystem.

16:32 λ sbt run
[info] welcome to sbt 1.5.5 (Homebrew Java 11.0.12)
[info] loading global plugins from /Users/jteigen/.sbt/1.0/plugins
[info] loading project definition from /Users/jteigen/Development/cli-resource/dest/project
[info] loading settings for project dest from build.sbt ...
[info] set current project to dest (in build file:/Users/jteigen/Development/cli-resource/dest/)
[info] running main 
jar:file:/Users/jteigen/Development/cli-resource/dest/target/bg-jobs/sbt_c7561821/job-1/target/4ed69241/c326b934/dest_3-0.1.0-SNAPSHOT.jar!/data.txt
[success] Total time: 1 s, completed Dec 17, 2021, 4:32:14 PM

As our jar file contains the resource-file

16:32 λ jar tf target/scala-3.0.2/dest_3-0.1.0-SNAPSHOT.jar 
META-INF/MANIFEST.MF
Test$package$.class
Test$package.class
Test$package.tasty
data.txt
main.class
main.tasty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment