SpringBoot can't read application.properties when running outside jar folder - java

I have created a SpringBoot Application, and I placed the application.properties in the same folder of the jar file. This is the content of the file:
server.port=8081
When I run /bin/java -jar /opt/apl/org.web.exemplo-java-maven.1.0.0-90/org.web.exemplo-java-maven-1.0.0-90.jar outside the jar folder it doesn't read the application.properties and runs the program in port 8080.
When I run /bin/java -jar /opt/apl/org.web.exemplo-java-maven.1.0.0-90/org.web.exemplo-java-maven-1.0.0-90.jar inside the jar folder (or inside any folder with the application.properties) it reads the file and runs in port 8081.
What is happening? I thought the properties file needed to be in the same place of the jar file. But it happens that it needs to be in the same folder where I'm running the command.

By convention at application startup Spring Boot looks for the application.properties file at classpath root, e.g. top-level of directory structure in your JAR file. You can customize this behaviour and configure a different application.properties location using a Spring specific environment property when starting the application from cmdline:
/bin/java -jar /opt/apl/org.web.exemplo-java-maven.1.0.0-90/org.web.exemplo-
java-maven-1.0.0-90.jar --spring.config.location=classpath:/other-
application.properties
Check this: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Related

How to override Java resources when you run Spring Boot fat-jar?

How to override Java resources when you run Spring Boot fat-jar?
I've created a Spring Boot far-jar that contains inside as resource a log4j.xml configuration file. Now when I run the fat-jar I'm trying to override it in this way
$ java -cp conf/ -jar target/myapp.jar
and I've put in the conf/ folder a new log4j.xml. But nothing, it continues to use the resource inside the jar.
If your goal is only to define your own log4j.xml configuration file, this could help:
java -Dlogging.config='/path/to/log4j2.xml' -jar target/myapp.jar
(this was mentioned already in How can I change the default location of log4j2.xml in Java Spring Boot? )
If you just want to add resources by classpath addition you could refer to
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-executable-jar-format.html#executable-jar-property-launcher-features
where I found loader.path :
loader.path can contain directories (which are scanned recursively for
jar and zip files), archive paths, a directory within an archive that
is scanned for jar files (for example, dependencies.jar!/lib), or
wildcard patterns (for the default JVM behavior). Archive paths can be
relative to loader.home or anywhere in the file system with a
jar:file: prefix.

How do I use a given application.properties file when deploying a .war on Apache Tomcat?

I have been tasked to deploy a ROOT.war app on Apache Tomcat. Here's what the ROOT.war file looks like on the inside:
The app I was told uses a Postgresql as its database. I already have sorted that one out.
I just want to know how to run this app on Apache Tomcat using this application.properties configuration file they have provided. Here's what it looks like:
Thank you very much and regards,
Jeremy
There are may ways to specify the location of the application.properties file. See Spring Boot Documentation for full list of how properties are loaded.
As described in section 2.3. Application Property Files:
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
A /config subdirectory of the current directory
The current directory
A classpath /config package
The classpath root
It goes on to say:
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths).
Of the many options, I'd highlight:
Recommended: According to this answer, you can specify the location in the Tomcat context XML file, although that is not explicitly specified in the Spring documentation:
<Context>
<Parameter name="spring.config.location" value="file:/path/to/config/folder" />
</Context>
Modify the .war file and place the file in the WEB-INF/classes/config or WEB-INF/classes folder.
If you can specify the working directory for the running Tomcat, place the file there or in a config sub-folder.
If you can specify a JVM option for the running Tomcat, add -Dspring.config.location=file:/path/to/config/folder, and place the file there.

How to specify a profile when deploying a Spring boot war file to Tomcat?

At the moment, all my properties are defined in the file src/main/resources/application.properties. However, I would like to have properties files relating to different profiles in the src/main/resources/config folder, and I want to be able to choose any of them. such as:
application-DEV.properties
application-TEST.properties
application-SERVER1.properties
So, the question is how to select these properties. If I was compiling to a jar file, I could do that easily by specifiying the profile when running the jar file, but here I just copy the generated war file to a Tomcat webapps directory.
Well, I've found a way to do that. In the conf directory of Tomcat, add this line to the file catalina.properties there.
spring.profiles.active=<YOUR_PROFILE>
Replace <YOUR_PROFILE> here of course with your profile's name. For example if you are using application-TEST.properties, it would be the following.
spring.profiles.active=TEST
You can define Jvm Argument -Dspring.profiles.active=<PROFILE> on server start up file (.bat/.sh) depending on your environment.

Take logback.xml to outside of the jar

I am using logback with slf4j in my Maven Java project. Currently logback config file (logback.xml) is in src -> main -> resources folder. And it is working fine.
My issue is, I need to give my client the ability to configure logging as he prefers. For that logback.xml should be outside the jar when I build it. But as xml is inside src folder it is inside the jar and no one can change it after build.
How to achieve this?
Specifying the location of the default configuration file as a system property
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml -jar myapp.jar
From offcial docs
Logback config file location can be specified in application.properties or application.yml.
application.yml
logging:
config: logback-spring.xml
This allows you to place jar and log-back.xml at the same folder.
Please note that logback-spring.xml file in your project folder should not be included in your jar. This can be achieved setting on build.gradle or pom.xml.
build.gradle
bootJar {
archiveName 'your-project.jar'
exclude("*.xml")
}
The logback.xml file needs to be on the classpath, but it doesn't need to be inside any specific jar. The details of how you want to do this depend on the exact deployment mechanism that's being used: How does whatever's starting this application set the classpath? Whatever that mechanism is, you should be able to configure it to include wherever you're putting the logback.xml file, and then just don't include in in the src/main/resources to be embedded in the jar file.
Depending on the complexity of what you're going for, you may find the maven-assembly-plugin useful for creating your distribution of dependencies.
Using Scala SBT (1.2.1) on Windows:
Batch file:
#cd %~dp0
#set JAVA_OPTS=-Dlogback.configurationFile=logback.xml
#sbt clean run
worked for me (strange ...)

Run java -jar and also specify the configuration path

A standalone(spring boot)jar can be run like this:
java -jar springboot.jar
How can I configure the configuration path as well in the command above ?
Like this below: ?
java -jar springboot.jar --configuration:/some-path-here
n.b. My configuration is external.
Thanks!
By default, Spring Boot will look in the working directory for external application.properties and application.yml files. For example, if you start your Spring Boot app in the same directory as the JAR:
java -jar myapp.jar
Then this will look for application.properties (or application.yml or application-{profile}.properties etc.) in the same directory as your JAR file. This means if you set up your directory like so:
myapp/
| - myapp.jar
| - application.properties
And start the app within that directory, then it will automatically pick up the application.properties there.
If this isn't an option for you, the spring.config.location property can be specified on the command to give it additional locations to use when looking for configuration files, e.g.:
java -jar myapp.jar --spring.config.location=file:/etc/myapp/
For more info, read up on Spring Boot's Externalized Configuration documentation.
As per Spring docs:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Use file instead of classpath to specify external configuration file.

Categories

Resources