Search code examples
playframeworkplayframework-2.4

Reading Files from public folder in play framework in production


My play app is in 2.4.2. In Developer Mode,I used to read files from public folder at back-end controllers using Source.fromFile("./public/files/abc.json")

When I converted the application to production mode, I am getting File Not Found Exceptions. I found that the public folder is packed in an assets jar in production mode.What can i do so that it works in both development and production mode??


Solution

  • Have you tried out this Play Documentation https://www.playframework.com/documentation/2.5.x/AssetsOverview ? This is Ok with Play 2.4 and even with 2.3 also, I have tried out.

    In there you can find this, you can simply do this in your conf/routes file.

    GET  /assets/*file        controllers.Assets.at(path="/public", file)
    

    file -> refers to the file name. Ex: myFile.json

    To make this work in production mode you have to do some little more work. As explained in this answer, add these lines into your /build.sbt file.

    import com.typesafe.sbt.packager.MappingsHelper._
        mappings in Universal ++= directory(baseDirectory.value / "public")
    

    That would include your 'public' directory inside the dist file (You can include any directory like that way). Then your application would work in the production environment.