Run Spring Boot JAR with predefined profile - java

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

Related

Customize Spring Boot executable-jar startup script

I'm trying to run Spring Boot executable-jar built using spring-boot-maven-plugin on a Linux machine. The machine has multiple jdks installed, the one on PATH is jdk8 and changing it is unfortunately not an option. My executable-jar however needs jdk17, so when I just launch it as is I get UnsupportedClassVersionError.
I was following the official documentation and created the corresponding .conf file to override JAVA_HOME. But this does not seem to solve the issue:
[root#ios-maket updater-new]# ls
updater-new-3.0-SNAPSHOT.conf updater-new-3.0-SNAPSHOT.jar
[root#ios-maket updater-new]# cat updater-new-3.0-SNAPSHOT.conf
JAVA_HOME=/opt/jdk-17/bin/java
[root#ios-maket updater-new]# ./updater-new-3.0-SNAPSHOT.jar
Application is running as root (UID 0). This is considered insecure.
Exception in thread "main" java.lang.UnsupportedClassVersionError...
On the other hand if I run it manually everything works fine:
[root#ios-maket updater-new]# /opt/jdk-17/bin/java -jar ./updater-new-3.0-SNAPSHOT.jar
[main] INFO com.icl.ios.fias.updaternew.UpdaterNew - Starting UpdaterNew using Java 17.0.6
What am I doing wrong?
Setting JAVA_HOME is not enough, you also need to set PATH to point to JAVA_HOME/bin.
JAVA_HOME=/opt/jdk-17
PATH=${JAVA_HOME}/bin:$PATH
java -jar updater-new-3.0-SNAPSHOT.jar
Try running the jar with -Dloader.path to specify the config manually.
java -Dloader.path=./updater-new-3.0-SNAPSHOT.conf -jar ./updater-new-3.0-SNAPSHOT.jar
If this still does not work, then probably there is an issue with your config file, but from what I can see, your config file looks okay, unless the java path is incorrect.

Profile specific application properties not picked by Spring Boot 3 native executable

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.

How to choose quarkus profile when running app under Idea?

When running my app from within Idea, I see command line ends with
... -Ddebug=38605 quarkus:dev
and dev profile is active. It doesn't depend on whether I press debug button or run button.
How to switch to prod profile? If I go to run configuration and enter
the command line changes to
... -Ddebug=38605 quarkus:dev -P prod
and the profile remains dev
This is not a Maven profile but a Quarkus profile.
You need to use -Dquarkus.profile=prod or the QUARKUS_PROFILE environment variable.
See https://quarkus.io/guides/config#configuration-profiles for more details.
You should use Runner - VM Options = -Dquarkus.profile=prod to specify quarkus profile in IntelliJ IDEA Run Configuration.
There is an issue in IntelliJ IDEA tracker, probably it should be simpler: https://youtrack.jetbrains.com/issue/IDEA-259900

How to make sure that environment variable placeholders are substituted in a Spring Boot application running in Apache Tomcat?

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.

Springboot standalone application deploy in production

I am trying to deploy my springboot standalone application in production in linux server.I am trying to use the out of the box tomcat server.
'java -jar jarname.jar'
This works fine , but if I close my putty session the application is unistalled.
Now I used
'nohup java -jar jarname.jar &'
this works good and my application is not shutdown even if my putty session is closed.But the logs will not roll correctly(I have configured log4j to create a new log file for each day) in this case. So was wondering if this is the right way to do this.
I have searched several documentation but was not able to find a correct solution for this problem.
Please help.
Thanks
Well I think it's better to use Linux services for running application, you can read here for example
And if you want to collect logs - better write them to the file.
Spring provides build-in me mechanics to do that
logging:
level:
root: INFO
file:
clean-history-on-start: false
max-history: 7
max-size: 10MB
name: some-name
path: /path/log/dir
total-size-cap: 0B
Why not package it into a Docker image and run that on the server?
Here are a few ideas:
https://medium.com/swlh/deploying-spring-boot-applications-15e14db25ff0
You can run your spring boot application as jar, but you need to create a service so you can execute your spring boot as daemon.
https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu
With this, you can start or stop your application like
$ sudo service myspringbootapp stop
$ sudo service myspringbootapp start

Categories