I have a following property declared at my Spring Boot/Maven project:
#Value("${download.folder.path}")
private String downloadFolderPath;
where download.folder.path is a property declared at my application.properties file and at Maven profile.
Right now the property value is something like this:
<download.folder.path>d:/somedir</download.folder.path>
Everything works fine but I want to point download.folder.path property to default system temp folder for my tests instead of providing a real path.
Is it possible with Spring/Maven configuration?
Use reference to java tmp dir property (check System.getProperty("java.io.tmpdir")) and reference the property like this
<download.folder.path>{java.io.tmpdir}</download.folder.path>
See the reference properties here
If you are using Spring Boot, check out #TestPropertySource.
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#__testpropertysource
You should be able to use that to point to /tmp or wherever.
1.)Writing below in SpringBoot main class for whole application to be working :
System.setProperty("java.io.tmpdir", "C:\\Users\\abcde\\Desktop\\Temp");
2.)While Running JUnit in Spring Boot application , I have set -Djava.io.tmpdir=C:\Work\TEMP in VM Arguments and it worked for me .
Related
I have the problem with configuration file that is located in other directory than my jar file.
I use #PropertySource for loading properties.
#PropertySource(ignoreResourceNotFound = true, value = "${ext.properties.dir:classpath:}/properties.properties")
I try to run jar using following command:
java -jar import-0.0.1-SNAPSHOT.jar -Dext.properties.dir=file:/C:\Users\Admin\Desktop\
The following error pring in logs: Properties location
Properties location
[${ext.properties.dir:classpath:}/properties.properties] not resolvable:
class path resource [properties.properties] cannot be opened because it
does not exist
How can I fix this error?
I saw that you are using spring boot application, according to the spring documentation you can try to use this environment property:
--spring.config.location=file:/path/location/file-name.properties
The final instruction would be:
java -jar import-0.0.1-SNAPSHOT.jar --spring.config.location=file:C:\Users\Admin\Desktop\import.properties
Spring Boot looks for application.properties on classpath. You don't need an explicit #PropertySource annotation. And #PropertySource doesn't have the capability to resolve SPEL in the path, which should be evident from the error message you're getting.
Use src/main/resources/application.properties. When you want to use the external file, use spring.config.location like #juan-calvopina-m suggested in his answer.
I am developing a spring-boot web app, and i need to get some classpath resources, so i use the method below:
#Autowired
private Enviroment env;
String fileLocation = ResourceUtils.getFile(env.getProperty("common.cert-location")).getAbsolutePath();
or
String fileLocation = new DefaultResourceLoader().getResource("filename").getFile().getPath();
Both of them run normally in the local enviroment, but when i pack the app and run it in a Docker image, it throw an exception. How can i get the resource? Thanks!
If I understand correctly, you try to load a file from the filesystem. The file location is passed in using an environment variable.
If that is correct, you can add the file you need in your Dockerfile using ADD myresource.file /myresource.file. This puts myresource.file into the root of your container.
Now, if you run your container, you can pass your environment variable common.cert-location using docker run -e common.cert-location=/myresource.file.
One addition: you can add a Spring Boot application property instead of an environment variable. Then set an environment variable with the same name like your property in your container. Spring Boot should pick up the variable by default.
Instead of letting the container know the variable using the -e parameter, you can also use env_file. This allows you to put all your environment variables into a single file and push them into the container.
In combination with Spring Boot application properties, this allows you to override all application properties with environment variables defined in a single file. I think that is a nice, consistent way of configuring Boot apps inside containers.
I have a spring boot application that I can package in a war that I want to deploy to different environments. To automate this deployment it'd be easier to have the configuration file externalized.
Currently everything works fine with a application.properties file in src/main/resources. Then I use ´mvn install´ to build a war deployable to tomcat.
But I would like to use a .yml file that does not need to be present on mvn install but that would be read from during deployment of the war and is in the same or a directory relative to my war.
24. externalized configuration shows where spring boot will look for files and 72.3 Change the location of external properties of an application gives more detail on how to configure this but I just do not understand how to translate this to my code.
My application class looks like this:
package be.ugent.lca;
Updated below
Do I need to add a #PropertySource to this file? How would I refer to a certain relative path?
I feel like it's probably documented in there as most spring boot documentation but I just don't understand how they mean me to do this.
EDIT
Not sure if this should be a separate issue but I think it's still related.
Upon setting the os variable the error of yaml file not found went away. Yet I still get the same error again as when I had no application .properties or .yml file.
Application now looks like this:
#Configuration
**#PropertySource("file:${application_home}/application.yml")**
#ComponentScan({"be.ugent.lca","be.ugent.sherpa.configuration"})
#EnableAutoConfiguration
#EnableSpringDataWebSupport
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
The application_home OS variable
$ echo $application_home
C:\Masterproef\clones\la15-lca-web\rest-service\target
My application.yml file(part it complains about):
sherpa:
package:
base: be.ugent.lca
Error upon java -jar *.war
All variations upon:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'sherpa.package.base' in string value "${sherpa.package.base}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 142 more
Using external properties files
The answer lies in the Spring Boot Docs, I'll try to break it down for you.
First of all, no you should not use #PropertySource when working with Yaml configuration, as mentioned here under the Yaml shortcomings :
YAML files can’t be loaded via the #PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.
So, how to load propery files? That is explained here Application Property Files
One is loaded for you: application.yml , place it in one of the directories as mentioned in the link above. This is great for your general configuration.
Now for your environment specific configuration (and stuff like passwords) you want to use external property files, how to do that is also explained in that section :
If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).
So you use the spring.config.location environment property.
Imagine you have an external config file: application-external.yml in the conf/ dir under your home directory, just add it like this:
-Dspring.config.location=file:${home}/conf/application-external.yml as a startup parameter of your JVM.
If you have multiple files, just seperate them with a comma. Note that you can easily use external properties like this to overwrite properties, not just add them.
I would advice to test this by getting your application to work with just your internal application.yml file , and then overwrite a (test) property in your external properties file and log the value of it somewhere.
Bind Yaml properties to objects
When working with Yaml properties I usually load them with #ConfigurationProperties, which is great when working with for example lists or a more complex property structure. (Which is why you should use Yaml properties, for straightforward properties you are maybe better of using regular property files). Read this for more information: Type-Safe Configuration properties
Extra: loading these properties in IntelliJ, Maven and JUnit tests
Sometimes you want to load these properties in your maven builds or when performing tests. Or just for local development with your IDE
If you use IntelliJ for development you can easily add this by adding it to your Tomcat Run Configuration : "Run" -> "Edit Configurations" , select your run configuration under "Tomcat Server" , check the Server tab and add it under "VM Options".
To use external configuration files in your Maven build : configure the maven surefire plugin like this in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.config.location=file:${home}/conf/application-external.yml</argLine>
</configuration>
</plugin>
When running JUnit tests in IntelliJ:
Run → Edit Configurations
Defaults → JUnit
add VM Options -> -ea -Dspring.config.location=file:${home}/conf/application-external.yml
Yes, you need to use #PropertySource as shown below.
The important point here is that you need to provide the application_home property (or choose any other name) as OS environment variable or System property or you can pass as a command line argument while launching Spring boot. This property tells where the configuration file (.properties or .yaml) is exactly located (example: /usr/local/my_project/ etc..)
#Configuration
#PropertySource("file:${application_home}config.properties")//or specify yaml file
#ComponentScan({"be.ugent.lca","be.ugent.sherpa.configuration"})
#EnableAutoConfiguration
#EnableSpringDataWebSupport
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
There is a very simple way to achieve this.
Inside your original application.properties file you can just specify the following line:
spring.config.import=file:Directory_To_The_File/Property_Name.properties
It will automatically sync all the properties from the external property file.
Now lets say that you have a situation where you need to get properties from multiple property files. In that case, you can mention the same line in the external property file which in turn will take the remaining properties from the second property file and so on.
Consider the following example.
application.properties:
spring.config.import=file:Resources/Custom1.properties
Custom1.properties:
server.port=8090
.
.
.
spring.config.import=file:Resources/Custom2.properties
One of the easiest way to use externalized property file using system environment variable is, in application.properties file you can use following syntax:
spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/"nameofDB"
spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME}
spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PORT}
Now, declare above used environment variables,
export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost"
export OPENSHIFT_MYSQL_DB_PORT="3306"
export OPENSHIFT_MYSQL_DB_USERNAME="root"
export OPENSHIFT_MYSQL_DB_PASSWORD="123asd"
This way you can use different value for same variable in different environments.
Use below code in your boot class:
#PropertySource({"classpath:omnicell-health.properties"})
use below code in your controller:
#Autowired
private Environment env;
I'm trying to customize Spring Boot config location and config name using spring.config.name and spring.config.location properties as I've saw on Spring Boot reference guide
I've created an Spring Boot basic application to test it.
I'm able to customize it using OS environment variable like export SPRING_CONFIG_NAME=custom and/or export SPRING_CONFIG_LOCATION=classpath:/custom/location.properties. That works fine!
But I want to know, if it's possible to define spring.config.name=custom on default application.properties and then create a custom.properties file where I'll be able to define all application configuration properties.
I've checked it, and seems that it's not working defining spring.config.name property on application.properties... but I want to know if this is a valid way to do it before to create an issue on gitHub.
Regards,
From spring documentation:
spring.config.location environment property (comma-separated list of
directory locations, or file paths)
Moreover, code in ConfigFileApplicationListener shows that if there if no environment property, processing fallbacks to:
DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
And for the name:
DEFAULT_NAMES = "application";
So it is normal that what you are doing is not working.
I am trying to deploy a webapp that uses logback's groovy config files.
I saw that in xml files you can reference ${catalina.base}
Is there a way to reference that from logback.groovy? I want a reference to tomcat home or logs folder. I want it to work even if I don't have the environment variable set.
If I write it as is I get "No such property: catalina"
I am trying to make my logs go to tomcat/logs/... regardless of the hostname. I want to make it webapp specific, not in tomcat/conf/
catalina.base is a system property which is set by Tomcat startup scripts (catalina.sh, catalina.bat).
When working with logback's groovy config files you can simply read the value of this system property into a variable and use it, for example:
def catalinaBase = System.properties['catalina.base']
appender("FILE", FileAppender) {
file = "${catalinaBase}/mylog.log"
...
}