I have a scala project (based around unfiltered and jetty) which uses bval. I've tried everything I can think of to get custom validation messages to load from a properties file, but it just doesn't work. I have a scala class like this:
class AddName{
#NotEmpty(message = "{firstName.notEmpty}")
var firstName: String = ""
#NotEmpty(message = "{lastName.notEmpty}")
var lastName: String = ""
}
and a ValidationMessages.properties file containing the following:
firstName.notEmpty=enter a first name
lastName.notEmpty=enter a last name
But the output from the validation results is {firstName.notEmpty} not matter what I try.
I have tried placing the properties file in the project root, in a resource folder which is exported to the classpath by the run configurations. I've also tried placing it in the target/scala-2.9.1/classes folder and then running the project with sbt compile.
Related
I'm trying to get some properties from an application.properties file in Gatling-Scala. I tried.
val properties: Config = ConfigFactory.load("application.properties")
val clientId: String = properties.getString("api.clientId")
I keep getting "com.typesafe.config.ConfigException$Missing No configuration setting found for key 'api'". I put the application.properties file inside src/test/resources/application.properties and also in the root folder of the project.
I tried also to put the same information inside src/test/resources/gatling.conf as follows:
gatling {
api {
clientId = "..."
}
}
But I get the error:
com.typesafe.config.ConfigException$Missing No configuration setting found for key 'gatling'
Is there something I'm missing?
I managed to get the information in the gatling.conf file by installing the plugin HOCON for .conf files and formatting it correctly.
I read that SBT has functionality to generate source code and resource files.
In my case I want to add/modify a field in an application.conf file during compilation/packaging of the project (leaving the others in place)
For instance my application.conf file has something like:
A {
B = "Some Value"
C = "Some value to be modified"
}
I would like in the SBT to read an external file and change or add the value of A.B or A.C
So if it is possible to do something along the lines of:
build.sbt
lazy val myProject = project.in(file('myproject')
// pseudo code - How do I do this?
.sourceGenerators in Compile += "Read file /path/to/external/file and add or replace the value of application.conf A.B = some external value"
You can replace the values with environment variable values provided while compiling / building your project. For that you'd have to
A {
B = "Some Value"
B = ${?B_ENV}
C = "Some value to be modified"
C = ${?C_ENV}
}
Where B_ENV and C_ENV are the environment variables you set in your terminal either before build or within the build command (before it)
$ B_ENV=1 C_ENV=2 sbt run
Source: https://www.playframework.com/documentation/2.6.x/ProductionConfiguration#using-environment-variables
In this case you can do without sbt and this approach would also work with maven or cradle.
The *.conf support orignates from typesafe config (https://github.com/lightbend/config).
There is a feature to get environment variables to be used in the configuration which should be a good fit to solve the problem.
There are two approaches I would suggest to use
1.) Fail on missing configuration
If configuration of this vallue is important and to prevent the deplyment of misconfigurated application the startup should fail on missing environment variables.
in application.conf
key=${TEST} // expects "TEST" to be set, fails otherwise
2.) Hardcoded value with override
If there is a sensible default behaviour that only in some circumstances should be changed.
in application.conf
key="test" // hardcoded key
key=${?TEST} // override "key" with 3nv "$TEST" value, when it is given
I would like to read two scala conf file from a maven jar named myconfiguration1.conf and myconfiguration2.conf:
driver= oracle.jdbc.driver.OracleDriver
user = myUser
password = pass
url = myUrl
table = my_table
before generate my jar, i used a line like this:
val myconfig1 = ConfigFactory.parseFile(new File("./my-project/src/main/resources/myconfiguration1.conf"))
val myconfig2 = ConfigFactory.parseFile(new File("./my-project/src/main/resources/myconfiguration2.conf"))
Inside my jar, i have a tree like this
my-project-0.0.1.jar
|___ myconfiguration1.conf
|___ myconfiguration2.conf
Do you have any idea since the two lines don't work with the jar file.
Seems you are using typesafe config to load the conf file, you can directly use ConfigFactory.load("myconfiguration1.conf") and ConfigFactory.load("myconfiguration2.conf") to load the classpath configuration file.
public static load(java.lang.String resourceBasename)
I have an application that loads configuration from application.conf using ConfigFactory: lazy val myConfig = ConfigFactory.load(pathToConfig)
The application.conf is initially located in src/main/resources
When I deploy my application I want it to load the config from APP_HOME/conf/application.conf
To do so, I excluded the application.conf from the resource folder when building the Rmp and I have added my APP_HOME/conf to the class path.
jar {
exclude '*.conf'
}
and
startScripts {
classpath += files('src/main/resources')
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
println('unix script is ' + unixScriptFile.text)
windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\resources', '%APP_HOME%\\conf')
unixScriptFile.text = unixScriptFile.text.replace('\$APP_HOME/lib/resources', '\$APP_HOME/conf')
println('after unix script is ' + unixScriptFile.text)
}
}
The odd thing is that when I modify the $APP_HOME/conf/application.conf and restart the app, the changes are not picked up: ie the old configuration is still being used
Any idea what might cause this or how I can print where the config is being loaded from would be helpful
With many attempts, I got it to work by calling lazy val myConfig = ConfigFactory.load() without specifying the conf file name or path.
Although it solved my issue I still don't understand why calling load with the file name or file path didn't work
I'm writing a solr plugin by extending SearchComponent class. My new class is part of a.jar archive. Also my class depends on a jar b.jar. I placed both jars in my core/lib folder and I declared my new component in solrconfig.xml. The component is invoked correctly up to a point where a class ClassFromB from b.jar attempts to load a classpath resource stopwords.txt from classpath.
The piece of code in class ClassFromB looks like this:
...
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource("stopwords.txt"); //with cl.getResource("/stopwords.txt"); I get the same exception
String content = IOUtils.toString(curl.openConnection().getInputStream());
...
The last line above throws a NPE telling me that the resource is not found.
So, the class ClassFromB is loaded because it gets to execute the code up to the last line where it fails. Also I double checked b.jar and I can see the stopwords.txt file right in the root.
What should I do to see that resource from class ClassFromB?
This however worked:
public final class Stopwords {
static {
URL curl = Stopwords.class.getClassLoader().getResource("stopwords.txt");
....
}
....
}