where to keep external files in play framework while using scala - scala

Where should I keep external files in play framework? I tried adding it to conf folder, it didn't work. Then I added it in project folder, it didn't work either. I defined the dependency in build.scala, but that didn't work as well. That file is supposed to be used in file input stream.

The contents of conf, including subdirectories, will be available on classpath. Then you can access it in your application.
For example, if you create a resources directory in conf and put a file in it:
-- app
-- bin
-- conf
- resources
- myfile
...
Then you can get the contents as:
val stream = getClass.getResourceAsStream("/resources/myfile")
val content = io.Source.fromInputStream(stream).mkString
You can inspect what gets copied and is made available on classpath by going to <play root>/target/scala-xx/classes/.

Related

Load external files in Drools

I'm new to drools and i have setup my application with the kmodule.xml file and the rules files under /rules folder. I package the app as a jar and everything works.
My use case though involves new rules being generated at runtime so have new files .gdst being generated. So my question is how can i make my .jar file use those files without them being in the resources folder, but loading them from an external folder at runtime?
Thanks
You can use Kie API to define KieContainer programatically - this includes adding externally loaded rules. On high level it can look like this:
KieFileSystem kfs = ...
kfs.write( "src/main/resources/KBase1/ruleSet1.drl", stringContainingAValidDRL )
.write( "src/main/resources/dtable.xls",
kieServices.getResources().newInputStreamResource( dtableFileStream ) );
See this class which contains unit tests illustrating this end-to-end:
https://github.com/kiegroup/drools/blob/9e335b0ff3b04d30316a9b75fba090784d6967d7/drools-mvel/src/test/java/org/drools/mvel/compiler/builder/KieBuilderTest.java

Using Finatra with SBT, where are the files searched for?

Where does Finatra search for the files? When I write
response.ok.file(...)
in which folder should the files be placed to be found?
In the docs it says the "classpath root" which is nowhere to be found (as it doesn't exist, strictly speaking).
It will search inside the resources folder
src/main/resources
In the example below I have a web folder inside the resources
get("/:*") { request: Request =>
response.ok.fileOrIndex(
s"web/${request.params("*")}",
"web/index.html")
}
If you are using intellijIdea you have to make sure you all the resources types defined in the configuration:
compiler->resource patters
Otherwise when compiling it won't copy it over to the output folder
The files taken from the resources folder will be copied under:
target/scala-2.12/classes
for Scala 2.12, BUT for files which will be created during runtime this path won't be available, i.e. the process will find only the files created before doing sbt run. This is true for both root projects and subprojects (it depends on where the run was executed).
If you need to have files loaded at all times, you need to set the flag -local.doc.root .

How to write a file to "target" directory of Scala SBT project?

I want to create folder and write files in the \target\ sub directory of my project, but the only thing I have been able to accomplish is providing a string of the file name and the file will be created in \.idea\modules\.
There is not enough info on this at all. Everybody only talks about accessing the \resources\ directory.
Can anybody help me out?
Thanks.
UPDATE: I managed to hack a way to accomplish making a folder under the "target" directory. It may look ugly, I wonder if this is the only way.
import java.io.File
import java.nio.file.{Files, Path, Paths}
val targetDir: Path = Paths.get(getClass.getClassLoader.getResource("").toURI).getParent.getParent
val newFolderString = targetDir.toString() + "/myNewFolder"
val directory: File = new File(newFolderString)
directory.mkdir()
If you use the default directory layout of an SBT project (http://www.scala-sbt.org/0.13/docs/Directories.html) then when you compile the project, the files you have under src/main/resources will be copied to target/scala-2.xx.
If you need to keep your resource files in a non-standard location see Nyavro's comment.
To write an output file from within your application, at runtime, you can look at Writing data generated in scala to a text file

How to set up prod mode to have configuration file in conf directory (not inside jar)?

I'm using activator to create my project and I use the command universal:packageBin to generate a .zip file.
I was wondering if it's possible to set up the application to read the configuration file from conf folder instead of the configuration from classpath ( the .conf file inside the jar).
Is it possible? If it's, how can i do it?
See https://www.playframework.com/documentation/2.3.x/ProductionConfiguration for the mechanism of telling a Play application where to pickup its config from.
To tie that in with the universal:packageBin, just pass -Dconfig.file to the script inside the bin directory. e.g. if your application is called foo:
unzip foo-1.0-SNAPSHOT.zip
foo-1.0-SNAPSHOT/bin/foo -Dconfig-file=/path/to/app.conf

XML parsing in scala

I am parsing XML file in scala using
val data = XML.loadFile("changes.xml")
I have the changes.xml under the src folder. When I run this the code throws a FileNotFoundException. Any idea how to solve it or any insight on how scala looks for the
files in the classpath would be helpful.
See what the current directory is using
new java.io.File(".").getCanonicalPath()
Since you're opening the file with a relative path, it looks for the file in the process's working directory.
Since you're placing your file in the source tree, I assume you want to ship it with the application jar.
Then tell your IDE to copy the file to the ouput folder (maybe this is already happening) so it lies in the classpath. If you place your file in the same folder as the class from which you want to load you can simply do the following. Use in Java/Scala
Class.getResourceAsStream("changes.xml")
Link to API doc.
Edit
You can use XML.load("changes.xml") and I think it will load the file in the same way as Class.getRessourceAsStream. So try putting your xml file into the same folder as the class and make sure the build process copies it into your binary output folder.