I am planning to use a native image with Spring Boot 3. My environment-specific properties are stored in the application.properties file. Sample file
spring.config.activate.on-profile=dev
server.port=9092
#---
spring.config.activate.on-profile=local
server.port=9093
I build the native executable using the following command
./mvnw -Pnative native:compile -Dspring.profiles.active=local
and run the executable using the below command
./target/<app-executable> -Dspring.profiles.active=local
In the logs I see
No active profile set, falling back to 1 default profile: "default"
Am I doing something wrong or Are profiles not supported with Spring Boot 3 native image?
Able to make it work by passing the arguments as -- instead of using -D.
For building the native image, no need to pass -D.
./mvnw -Pnative native:compile
And to run the executable with system properties, use the below command
./target/<app-executable> --spring.profiles.active=local
This picked up the server.port=9093.
Related
I'm trying to run my JAR file with the java -jar sample.jar command and I'm using application.yml configuration file with the following setup
spring:
profiles:
active:
- dev
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: test
But the application doesn't recognize the profile giving back the information of
No active profile set, falling back to default profiles: default and setting up the port to default 8080
I tried things like
java -jar sample.jar -Drun.jvmArguments=-Dspring.profiles.active=dev or
java -jar sample.jar -Dspring.profiles.active=dev
In my pom.xml file the only change I made was adding <packaging>jar</packaging> property.
From what I've learned maven profiles are completely different things and shouldn't influence spring profiles.
I don't know where to look for the problem as when I start the app by Run button in Intellij IDEA it works perfectly and recognizes every profile set up as active.
The actual question here is how to set up default profile to run while building an artifact so there's no need to place extra flags in the terminal command running the jar.
I know there's a lot of questions like mine but most of their accepted answers doesn't work in my case and the rest is unanswered. If you know what might be an issue here, please let me know. Thank you in advance for any help!
You can activate profile through command line argument. In your trying, you are defining the active profile after the name of the jar. You need to provide the program argument before the name of the jar you are running.
java -jar -Dspring.profiles.active=dev sample.jar
An alternative way to set the active profile is to create a application.properties file with the following property:
spring.profiles.active=prod
I have a Spring Boot application, which runs in an Apache Tomcat server. In application.yaml I have, among others, following entries:
mail:
pop3Host: ${MAIL_HOSTNAME}
inboxFolder: ${MAIL_INBOX}
hostName: ${MAIL_HOSTNAME}
port: ${MAIL_PORT}
userName: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}
The application is deployed to Tomcat from within IntelliJ Idea so I can debug it.
I start Tomcat using the following command:
export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start
However, after I
start Tomcat using the above script,
deploy the Spring Boot application from IntelliJ Idea, and
make sure that the code where those values are used is executed,
I get the exception indicating that the placeholders have not been substituted.
How can I fix it, i. e. make sure that I can specify some information (like user name and password) in application.yaml via environment variables (so that I don't include the actual credentials in application.yaml)?
export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start
Add export MAIL_HOSTNAME= etc. to the above lines, or create a setenv.sh file with such lines (in the same directory as catalina.sh file).
Using setenv.sh is documented in RUNNING.txt file of Apache Tomcat.
It is not possible to pass JVM arguments to a process running in a remote machine from an IDE. The Spring Boot params will be loaded from the JVM params when the process is started in the remote machine. I am sorry to say that it won't work.
An alternate solution i suggest is, use spring config server to create a separate profile for remote debugging config ( like we will have for Dev, QA environments etc ). When you try to debug the application, please restart the remote application using jenkins job ( I am assuming you don't have remote acccess to the box where the app is running) by passing the profile name in the jenkins job so that the values you wanted will be picked up. Please let me know if you need more details.
I have spring boot application with executable jar-file
So, I tried to start to from cmd in windows by command
java -jar -Dspring.config.location="application.yaml" MyService.jar
and it sucessfully started
(I try to use external application.yaml to start my application)
In my application all properties.yaml like datebase mocked like this
url: ${DATA_BASE_URL}
and external yaml contains all values.
But, when I try to start application using this command on centOS (or other linux), there is fail when try to start, spring boot used ${DATA_BASE_URL} instead external application.yaml values.
How to fix it?
On linux I used
nohup java -jar -Dspring.config.location="application.yaml" MyService.jar
According to the official documentation https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files
you should use double dashes
nohup java -jar --spring.config.location="application.yaml" MyService.jar
I have created a symlink for a spring boot executable jar and i am able to start the application. I want to customize the logs path, pid folder etc.
Went through the customizing the startup of this script.
But I could not find where to store the custom script and how it can be linked to executable application jar. Could you please assist?
Create a script with name your-app.service, place this script in /etc/systemd/system directory.
Installation as a systemd Service, using Java System Properties (VM Arguments):
[Unit]
Description= Spring Boot App
After=syslog.target
[Service]
User=myapp
ExecStart=java -Dspring.application.name=example -Dlogging.file=/opt/spring-boot-app/log/app.log -Dspring.pid.file=/opt/spring-boot-app/app.pid -jar /opt/spring-boot-app/app.jar
SuccessExitStatus=200
[Install]
WantedBy=multi-user.target
Or pass throught via program arguments:
java -jar /opt/spring-boot-app/app.jar --spring.application.name=example --logging.file=/opt/spring-boot-app/log/app.log --spring.pid.file=/opt/spring-boot-app/app.pid
References in here and here.
logging.file= # Log file name (for instance, myapp.log). Names can
be an exact location or relative to the current directory.
spring.pid.file= # Location of the PID file to write (if
ApplicationPidFileWriter is used).
I'm a beginner to docker and hence the doubt. I have a simple spring boot application fetching properties from a config server. I want to dockerize my application and have a static image that can be used across environments. Assuming the config server url will change in higher environments , is it possible to pass the config server url (spring.cloud.config.uri) along with the docker run command? My docker file has an ENTRYPOINT mentioning java -jar for starting the application.
As explained in the Spring Boot reference guide Spring Boot will read properties from different sources. One of those sources is the environment.
It is also documented that relaxed binding can be used to bind properties. The latter means that spring.profiles.active and SPRING_PROFILES_ACTIVE will both work to specify the active profile. This applies to ALL properties that Spring Boot tries to resolve, so it also applies to spring.cloud.config.uri.
Using Docker you can specify environment variables those will be available in the container as environment variables.
Those 2 things combined make it possible to supply values to Spring Boot through Docker Environment variables.
docker run --env SPRING_CLOUD_CONFIG-URI=<your-environment-url-here> -d -p 8080:8080 <container-id>
In my case I use docker service create command to launch my container. Along with this I pass my argument ex: CONFIG_URI as an argument.
docker service create --name <image_name> -e CONFIG_URI="<your URI>" ......
In my Spring Boot application I would accept CONFIG_URI as a environment variable into my application properties.
spring.cloud.config.uri=${CONFIG_URI}
In my entrypoint file I simply mention something like the following
java -jar -Dspring.profiles.active=qa