I'm trying to access a .jpg file that is inside my src folders with this line of code:
getClass().getResource("/teste.jpg")
That leads to NullPointerException.
I tried to put the file in the app/ root directory. I also tried to put it in test/ directory and also test/resources as well.
When I run the test from Eclipse it works fine, but it does not from command line using play test.
How to solve this?
I believe that you can achieve it by using
Play.application().getFile("file-relative-to-root")
It is dependent on the running application but for the unit test you can create your own application. Take a look into API docs.
http://www.playframework.com/documentation/api/2.1.0/scala/index.html#play.api.Application
Place the file under public or app/assets as described in Anatomy of a Play application. You can also have the test resources under test/resources.
[root]> help resourceDirectory
Default unmanaged resource directory, used for user-defined resources.
[root]> show test:resourceDirectory
[info] root/test:resourceDirectory
[info] /Users/jacek/dev/sandbox/play-new-app/test/resources
With the resource file(s) in one of the directories, you can get at them as follows:
io.Source.fromInputStream(getClass.getResourceAsStream("/teste.jpg"))
Related
I have a Scala Play framework 2.7.x application which I deploy in Heroku. I use Lucene to index the WebApp and since there is no JdbcDirectory in Lucene I need to use their FSDirectory instead and that leads to issues with Heroku because I can't generate the index files under $APP_HOME/lucene-index/* in Heroku otherwise it will be wiped out each time. This leads me to two possible solutions and this is the simpler one:
Generate the $APP_HOME/lucene-index locally before deployment and save it in GIT, this folder will be at the same level as $APP_HOME/app and $APP_HOME/public.
Integrate the new nonstandard Play folder $APP_HOME/lucene-index so that it gets copied by Heroku (the purpose of this OP).
Upon startup the application checks for this folder and if doesn't exist (local case) gets generated otherwise it opens it (Heroku case).
Do I need to do something special on #2 to have Heroku recognize $APP_HOME/lucene-index/ as a folder that needs to be packaged together with the application? e.g. I would not like to put the $APP_HOME/lucene-index/ under $APP_HOME/conf/ for this to work.
Here I find the Anatomy of a Play 2.7.x application but there is no word on how to add extra path folders to it.
The solution I was after was to include the ./lucene-index folder as part of the Play dist. This is accomplished by changing the build.sbt file adding:
//********************************************************
// Add lucene-index to the dist
//********************************************************
import com.typesafe.sbt.packager.MappingsHelper._
mappings in Universal ++= directory(baseDirectory.value / "lucene-index")
Now it deploys to Heroku and it all works nicely.
I want to integrate newrelic in my flink project. I have downloaded my newrelic.yml file from my account and have changed the app name only and I have created a folder named newrelic in my project root folder and have placed newrelic.yml file in it.
I have also placed the following dependency in my buld.sbt file:
"com.newrelic.agent.java" % "newrelic-api" % "3.0.0"
I am using the following command to run my jar:
flink run -m yarn-cluster -yn 2 -c Main /home/hadoop/test-assembly-0.2.jar
I guess, my code is not able to read my newrelic.yml file because I can't see my app name in newrelic. Do i need to initialize newrelic agent somewhere (if yes, how?). Please help me with this integration.
You should only need the newrelic.jar and newrelic.yml files to be accessible and have -javaagent:path/to/newrelic.jar passed to the JVM as an argument. You could try putting both newrelic.jar and newrelic.yml into your lib/ directory so they get copied to the job & task managers, then adding this to your conf/flink-conf.yaml:
env.java.opts: -javaagent:lib/newrelic.jar
Both New Relic files should be in the same directory and you ought to be able to remove the New Relic line from your build.sbt file. Also double check that your license key is in the newrelic.yml file.
I haven't tested this but the main goal is for the .yml and .jar to be accessible in the same directory(the yml can go into a different directory but other JVM arguments will need to be passed to reference it) and to pass -javaagent:path/to/newrelic.jar to as a JVM argument. If you run into issues try checking for new relic logs in the log folder of the directory where the .jar is located.
I have a lot of tests (classes) and they are placed to different folders.
It looks like this:
I want to run all the tests under a specific folder including all sub-folders.
Please help me to do it using NUnit 3 console commands.
Here is a manual for this https://github.com/nunit/docs/wiki/Test-Selection-Language
The thing is: if I use: --where "test==LottoSend.com.TestCases.BackOffice" then it runs only tests under sub-folders in BackOffice folder (CMS, Packages, Raffle, etc.) but it doesn't run tests placed directly in "BackOffice" folder (such as BlackListTests.cs etc.)
Maybe I need to use another parameter for this?
NUnit knows nothing about the location of your source code. It doesn't look need or look at source code at all, but at the compiled test assembly.
If it's running the tests in the BackOffice folder, it's because they are all defined in the namespace "LottoSend.com.TestCases.BackOffice" - not because of what folder they are in.
What namespaces are used in your subfolders? Common practice would put the code under CMS in "LottoSend.com.TestCases.BackOffice.CMS" but it's up to you how you write the code.
So your choice is to either change the namespaces to match the folders or move the code to a folder that matches the namespace.
I would like to be able to run the java program in a specific directory. I think, that it is quite convenient to parametrize working directory, because it allows to easily manage configurations.
For example in one folder you could have configuration for test, in other you could have resources needed for production. You probably think, that there is option to manipulate classpath for including/exluding resources but such solution works only if you are interested in resources stored in classpath and referencing them using Classloader.getResource(r). But what if you have some external configuration and you want to access it using simple instructions like File file = new File("app.properties");?
Let's see ordinary example.
Your application uses app.properties file, where you store credentials for external service. Application looks for this file in working directory, because you uses mentioned File file = new File("app.properties"); instruction to access it. In your tests you want to use app.properties specific to your tests. In you integration tests you want to use app.properties specific to another environment. And finally when you build and release application you want to provide other app.properties file. All these resources you want to access always in the same way just by typing File file = new File("app.properties"); instead of (pseudo code):
if(configTest)
file = File("testWorkDir/app.properties");
else if(config2)
file = File("config2WorkDir/app.properties");
else
file = File("app.properties");
or instead of using resources in classpath
this.getClass.getClassLoader.getResource("app.properties");
Of course you are clever programmer and you use build tool such maven, gradle or sbt :)
Enought at the outset. At least The Question:
Is there a way to set working directory in java and if yes how to configure it in build tools (especially in sbt)?
Additional info:
changing 'user.dir' system property is not working (I've tried to change it programaticly).
In sbt changing 'working directory' via baseDirectory setting for test changes baseDirectory which is not base dir in my understangind and it is not equal new java.io.File(".").getAbsolutePath.
Providing environment variable like YOUR_APP_HOME and referencing resources from this path is feasible but require to remember about this in your code.
In sbt changing 'working directory' via baseDirectory setting for test changes baseDirectory which is not base dir in my understangind and it is not equal new java.io.File(".").getAbsolutePath.
I'm not sure what the above statement means, but with sbt you need to fork to change your working directory during the run or test. This is documented in Enable forking and Change working directory.
If you fork, you can control everything, including the working directory.
http://www.scala-sbt.org/0.13.5/docs/Detailed-Topics/Forking.html
Example code:
fork in run := true
baseDirectory in run := file("/path/to/working/directory/")
I am building a Scala Play 2.4 application which uses the typesafe activator.
I would like to run my tests 2 times with a different configuration file for each run.
How can I specify alternative config files, or override the config settings?
I currently run tests with the command "./activator test"
You can create different configuration files for different environments/purposes. For example, I have three configuration files for local testing, alpha deployment, and production deployment as in this project https://github.com/luongbalinh/play-mongo
You can specify the configuration for running as follows:
activator run -Dconfig.resource=application.conf
where application.conf is the configuration you want to use.
You can create different configuration files for different environments. To specify the configuration to use it with activator run, use the following command:
activator "run -Dconfig.resource=application.conf"
where the application.conf is the desired configuration. Without the quotes it did not work for me. This is using the same configuration parameters as you use when going into production mode as described here:
https://www.playframework.com/documentation/2.5.x/ProductionConfiguration#Specifying-an-alternate-configuration-file
Important to know is also that config.resource tries to locate the configuration within the conf/ folder, so no need to specify that as well. For full paths not among the resources, use config.file. Further reading is also in the above link.
The quotes need to be used because you do not want to send the -D to activator, but to the run command. Using the quotes, the activator's JVM gets no -D argument but it interprets "run -Dconfig.file=application.conf" and sets the config.file property accordingly, also in the activator's JVM.
This was already discussed here: Activator : Play Framework 2.3.x : run vs. start
Since all the above are partially incorrect, here is my hard wrought knowledge from the last weekend.
Use include "application.conf" not include "application" (which Akka does)
Configs must be named .conf or Play will discard them silently
You probably want -Dconfig.file=<file>.conf so you're not classpath dependent
Make sure your provide the full file path (e.g. /opt/configs/prod.conf)
Example
Here is an example of this we run:
#prod.conf
include "application"
akka.remote.hostname = "prod.blah.com"
# Example of passing in S3 keys
s3.awsAccessKeyId="YOUR_KEY"
s3.awsSecretAccessKey="YOUR_SECRET_KEY"
And just pass it in like so:
activator -Dconfig.file=/var/lib/jenkins/jenkins.conf test
of if you fancy SBT:
sbt -Dconfig.file=/var/lib/jenkins/jenkins.conf test
Dev Environment
Also note it's easy to make a developer.conf file as well, to keep all your passwords/local ports, and then set a .gitignore so dev's don't accidentally check them in.
The below command works with Play 2.5
$ activator -Dconfig.resource=jenkins.conf run
https://www.playframework.com/documentation/2.5.x/ProductionConfiguration