Extern log4.properties file with Spring Boot - spring

I am running my app with executable jar.
I have log4j.properties inside /resources folder
In prod I would like to override it and have it within external dir
How I could do that using Spring-Boot?

-Dlogging.config=/path/to/log4j.properties
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

If you don't like to add command line arguments, you can make additional application.properties on the directory where you start the application. like:
# log4j configuration for product
logging.config=log4j-prod.properties
Then, this application.properties will override the /resources/application.properties and, the log4j-prod.properties will be used on the product environment. Please read more details on:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

try this:
java -Dlog4j.configuration=file:/log4j.properties -jar XX.jar
the -D configuration before the -jar configuration.
work for me.

-Dlog4j.configuration=file:/path/to/log4j.properties to the command line works in Spring Boot 1.5.6. Don't forget file: .

Related

application.properties value is not being evaluated correctly across platforms

In my spring boot batch (2.7.3) application.properties file I have:
rs.input.path=/opt/ingestiondata/rs
Afterthat, when I do a mvn clean install on my windows machine, I get the jar file in my target folder. When I try to do java -jar myjar.jar on my local windows command prompt, it give (as expected, as there is no such path) exception - java.nio.file.NoSuchFileException: \opt\ingestiondata\rs
Then I move the same jar file to the linux box there when I do java -jar myjar.jar my key rs.input.path get evaluated to a windows path - c:\users\ajay\some\dir.
What can be wrong here? As I am using the same jar. Its odd but its what happening since last couple of hours. Tried and verified killall java etc etc and now running out of options. Any pointers/help will be greatly appreciated. Must be something trivial and something horrible I am expecting.
Update:
As asked by Abhijit, this is how this is being used.
#Value("${rs.input.path}")
private String inputPath;
#Bean
ItemReader<File> myReader() throws IOException {
List<File> files = Files.walk(Paths.get(inputPath))
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
return new IteratorItemReader<>(files);
}
I don't think in a spring application,
for any property,
there can't be multiple values;
unless you are using multiple profile specific 'application.properties' files which are specific to each environment.
Option 1:
"""
Check where your property is residing:
'rs.annual.path=/your/linux/path'
Suppose, let's assume it's inside
application-dev.properties
So, you need to select "dev" as Spring profile.
Try running the jar again, by pass passing profile.
(Spring_Property)
java -jar myjar.jar --spring.profiles.active=dev
(Vm_Argument)
2.java -jar -Dspring.profiles.active=dev myjar.jar
"""
Option 2:
"""
Since you are running your jar file inside linux.
Try passing path directly as argument.
(Spring_Property)
java -jar myjar.jar --rs.annual.path=/your/linux/path
You can pass the property as argument. So the Argument-property will taken precedence over Application-property.
"""

Externalize password in Spring Boot properties file

i have application.properties file
which has
spring.profiles.active=local
and i have application-local.properties which has many fields including
api.password = password123
As you can see i have hard coded password123 in properties file.
Suppose i am on windows and i have app.properties file on windows which has
api.password = password123
And i want to read api.password in spring boot properties file through app.properties
How can i achieve it ?
Startup parameters
If you want to read properties from a non-standard location, you can start your application with --spring.config.location or with --spring.config.additional-location parameters.
$ java -jar app.jar --spring.config.location=file:./app.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
#PropertySource
If you don't control startup parameters, you can use #PropertySource annotation.
Simply annotate your main class (or any other configuration):
#PropertySource("file:.app.properties")
You can set ignoreResourceNotFound=true, so the application will start, even if the file is not there.
https://docs.spring.io/spring/docs/5.1.9.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html
Read the documentation
There are literally 17 ways to pass properties to a Spring-Boot application.
I suggest, you get familiar with the convention, as it is crucial to understand which file takes a precedence:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config
Probably you can use --spring.config.location like below
$ java -jar myApp.jar --spring.config.location=file:/directoryof file/app.properties
i used jasypt : https://github.com/ulisesbocchio/jasypt-spring-boot
to encrypt the password, then you supply a decryption string (jasypt.encryptor.password) as startup parameter. That way no useabel password is in the configuration and on the git-repos.....

spring-boot - how to specify path of application.properties in envrionment or system properties?

If I want to run spring boot application, and want to use difference application.properties in difference path (other than using profile)
How can I specify path for application.properties?
If you want to use difference application.properties in difference path, use this command to start the jar file
nohup java -jar project.jar --spring.config.location=file://{file-path}/application.properties
environment variable SPRING_CONFIG_LOCATION can be also used.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
#PropertySource annotation is used to provide properties file into the environment and it is used with #Configuration classes.
#PropertySource({ "classpath:config.properties" })
use bellow code above main class:
#PropertySource(value = {"file:///${HOMEDIR}/application.properties"})

Running bootRun and use a different application.properties file

I'd like to be able to run bootRun for spring boot, but use a different application.properties file besides the one in src/main/resources/. Is that possible? I'd prefer to not overwrite the file in src/main/resources/, as it would dirty the file.
Is this possible?
You can use profile based configuration selection.
Just set a system environment property:
spring.profiles.active=dev
And now provide application-dev.properties in application resources(src/main/resources/)
By this way you can use different properties for different environment.
If you want to provide files at a different location the use this environment property
spring.config.location=<path>
If you wish to use different name then application in property file name the use this environment property:
spring.config.name=<new_name>
For more info check this link:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
Here is what my bootRun task looks like that does this
bootRun {
systemProperty "spring.config.location", "file:$projectDir/spring-config/"
main = springBootAppClass
}
Which specifies to use the spring-config directory in the project's root folder.
If using in conjunction with #PropertySource it looks like this
#PropertySource("${spring.config.location}/persistence.properties")
Try this command,
./gradlew bootRun --args='--spring.profiles.active=dev'
I too was looking for the solution, found it in official documentation
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#running-your-application
I got it to work like this:
tasks.named("bootRun") {
systemProperty "spring.config.location", "file:$projectDir/myConfigFolder"
mainClass = "my.project.MyMainClass"
}

Spring Boot and external configurations

I am trying to make a Spring Boot application. Everything is fine once I deploy to the fat jar file with everything contained in it. But, what I actually want is the configuration files to be located externally. for example I have the following directory structure:
bin - contains startup and shutdown scripts
conf - all configurations. i.e. application.properties, logback.xml i18n.properties
logs - log files
libs - app.jar
If I use this directory structure and execute the jar using
java -cp ./conf -jar ../libs/app.jar
then the properties in the conf directory are not loaded or recognized. Is there a better way to do this maintaining the directory structure above? Or, what is the alternative/best practice?
Boot external config is what you are looking for.
Especially it mentions:
SpringApplication will load properties from application.properties
files in the following locations and add them to the Spring
Environment:
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
So I would say adding the config folder on classpath is good step. Them it should find application.properties and load it automatically.
For different config files I use:
#Configuration
#EnableAutoConfiguration
#PropertySource({
"classpath:path/some.properties",
"classpath:another/path/xmlProperties.xml"
})
public class MyConfiguration {
// ...
}
Edit:
As Dave pointed out (Thank Dave!) there is either -cp or -jar, so you can't add it to classpath like that. But there are options. This should help you to solve the problem: Call "java -jar MyFile.jar" with additional classpath option.
Additionally #PropertySource doesn't require the resources to be classpath resources if I'm not mistaken.
It should also be mentioned that there is a spring.config.location parameter that allows one to specify a file system / classpath location for externalized configuration files. This is documented in the following section of the Spring Boot reference guide:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Resources