spring boot jar not picking profile specific properties outside jar - spring-boot

I have a spring boot jar(name : myjar) located in the directory structure
D:/hello/myjar
The fat jar contains profile specific application.properties in src/main/resources for e.g. application-local.properties
I want to override the attributes defined in the application-local.properties inside the jar with application-local.properties outside the jar
Hence I created application-local.properties and kept it in the same folder as jar i.e. D:/hello
However when I run my jar using the command :
java -jar -Dspring.profiles.active=local D:/hello/myjar.jar
it still picks the properties which is inside the jar. Am I missing something ?

Try this use -D before -jar
java -Dspring.profiles.active=local -jar D:/hello/myjar.jar
let's see https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

I think it was more of point that from where I was running my jar from the command prompt.
If I run the jar from the folder where my jar is present, it does picks the profile specific files present outside the jar.

Related

Adding external jars in classpath to an executable WAR file

I have a spring-boot executable WAR file which I launch using java -jar . Now I want to add the flexibility of having it reference JAR files outside of WAR file so as to enable customization of code.
So e.g. I want to have to flexibility of having a jar being present in d:\mylib\my.jar and reference the classes in my.jar from code within the WAR file.
Is this possible?
If yes, how?
I tried adding Class-Path and even editing the spring-boot-lib path in manifest.mf but no use.

Could not open ServletContext resource in Spring boot application [duplicate]

I have a Spring Boot application using Google Pub Sub API. I need to inject Google credentials and other properties using file credentials.json. I put the file in my src/main/resources (otherwise, it will not put the file in the built jar) like this:
spring.cloud.gcp.credentials.location=file:src/main/resources/credentials.json
However, when I build the jar, this file is placed in the root directory and this path is no longer valid. So I am able to run my application from Eclipse, since by the that time, the file is still in my resources directory but I can't run it as a standalone jar after built because the path is suddently just file:credentials.json.
Is there some easy way how to specify the path as relative so it works both in IDE and when running my jar? I can inject the path through env. variables but I would do so only if absolutely necessary.
If you use the classpath prefix then Spring will look for the file on your classapth.
If you put the file in src/main/resources then Maven will, by default, copy it to the root of your classpath and it will then be addressable as follows:
spring.cloud.gcp.credentials.location=classpath:credentials.json
This should hold true whether ...
You are running in your IDE; your IDE's Maven integration will copy the file from src/main/resources to the root of your classpath - typically target/classes
You are running a built JAR; Maven will copy the file from src/main/resources to the root of your JAR

how to move spring boot config file bootstrap.yml out of resource package

I'm using spring boot, the config file bootstrap.yml located at src/main/resources. When I build a jar file, the file bootstrap.yml was also included in the jar. That caused I can not modify the file once the jar was build.
So how should I move the bootstrap.yml file out of the jar, I tried remove it out and using -classpath to indicate the file, but it doesn't work. I tried command as following:
java -classpath /user/local/bootstrap.yml -jar spring_test.jar
You can place bootstrap.yml outside the jar simply, and startup the application with regular command such as
nohup java -jar xx.jar >nohup.log &
It will work as you wish, and config in this file is loaded with higher precedence than that one (also name as bootstrap.yml) inside the jar.

How to Run the spring boot jar

I have spring boot jar. It contains boot-inf folder with that folder it contains classes and lib folder. I need to run the certain class which is having main method. But I don't know how to run it. For normal jar we can use the below format to run the class from the jar.
java -cp "sample.jar;dependecy.jar" com.sample.ClassName
But in spring boot jar what is the format to run the class. Because it contains boot-inf folder. I am using gradle script to build the project
It should be very easy, just:
java -jar sample.jar
where sample.jar is your spring boot jar.
See https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html
You can try with the distribution to run the class in a jar. Build the project to create the dist. Then you can run the class as you mentioned in the question.
java -cp "sample.jar;dependecy.jar" com.sample.ClassName

How to put a directory first on the classpath with Spring Boot?

I am building my Spring Boot application using Maven, so I can start it with:
java -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
I want to have a directory first on the classpath that would allow me to place some files on the filesystem without having to unzip the jar to change them.
I have tried using loader.path, but it does not seem to work.
java -Dloader.path="config/*" -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
The config dir is a subdirectory of where the jar is located. I am trying to load a keystore file which is injected as a Resource in my application. There is such a file in the src/main/resources, but that only works in my IDE, not when packaged as a jar. So I want to put a file first on the classpath so that that one is found first on the classpath.
You can use loader.path but only if the Main-Class is PropertiesLauncher (so it depends how you built the JAR file). Maybe you need to re-build the JAR with packaging=ZIP in the Boot plugin (e.g. docs here)? Can you not set the path to the keystore as a "file:" URL?

Resources