How to configure sbt to load resources when running application?

前端 未结 1 1433
庸人自扰
庸人自扰 2021-02-14 05:41

My code (Java) reads an image from jar:

Main.class.getResourceAsStream(\"/res/logo.png\")

Everything runs fine (if I start the app after packag

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 06:06

    I can't give you a full solution right now, but there is a setting called resourceDirectories to which you could add the res folder.

    [EDIT] For me it didn't work also if the resource was in the standard resource folder. Please try it that way:

    Main.class.getClassLoader().getResourceAsStream("icon.png")
    

    [EDIT2] This is the full build script (build.scala) which works if your resource is in src/main/java:

    import sbt._
    import Keys._
    
    object TestBuild extends Build {
    
      lazy val buildSettings = Seq(
        organization := "com.test",
        version := "1.0-SNAPSHOT",
        scalaVersion := "2.9.1"
      )
    
      lazy val test  = Project(
        id = "test",
        base = file("test"),
        settings = Defaults.defaultSettings ++ Seq(resourceDirectory in Compile <<= javaSource in Compile)
      )
    }
    

    0 讨论(0)
提交回复
热议问题