Logback external configuration on Tomcat - spring-boot

I have Spring Boot application deployed as WAR file on standalone Apache Tomcat.
In order to configure Logback on Windows machine I've created setenv.bat file with the following content:
set logging.config=<absolute path to logback.xml>
I was trying the same for Tomcat deployed on Mac, but the following setenv.sh doesn't work:
logging.config=<absolute path to logback.xml>
I've also tried set logging.config variable using export command:
export logging.config=<absolute path to logback.xml>
but result is the same - log files aren't created.
Any ideas what is wrong with my configuration?

I hope you already have found your answer somewhere. But what I have done is that in conf/catalina.properties, point to the logback config file as follows:
logging.config=${catalina.base}/conf/logback-spring.xml
I don't do anything in setenv.bat.

Related

Using jasypt-spring-boot when deploying to Apache Tomcat

I'm trying to use the jasypt-spring-boot and deploy it to a Tomcat server as war.
How to pass the encryptor password, in this case, to ensure the encrypted values could be read?
All the provided example are about running a jar file or a Spring Boot app as follows:
java -Djasypt.encryptor.password={my-password-to-decrypt} -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar
May be add some settings to catalina.properties file in the Tomcat conf folder as we do for example when defining active profile?
I figured out how to achieve that:
create a setenv.sh file in CATALINE_HOME/bin folder
add the following entry to set the environment variable on the Tomcat startup:
export JASYPT_ENCRYPTOR_PASSWORD=your-password
save and restart Tomcat.

Spring Boot - Externalize Database Settings

I have a JAVA project developed with Spring Boot.
The database settings are in the default resources/application.properties file.
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:#//172.********:1521/BDHML
spring.datasource.username=********
spring.datasource.password=********
The application will run through the command:
java -jar **CONFIGURAÇÃO** app-cosolidar.jar
I need to put the database settings in a .properties file external to the project.
How can I do this?
What should the .properties file look like?
Should I change any .java files?
What setting should I put in java -jar?
Regards, Diego
You can provide command line arguments while running jar.
java -jar app.jar --spring.config.location=file://<path>/application.properties
You can also pass a folder location where the application will search for the files.
java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file://<path to config folder>
Refer this link for more understanding. https://www.baeldung.com/spring-properties-file-outside-jar
You would be able to launch your spring boot application with the external properties file path as follows:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

how to set profile-specific applicaiton.properties(spring boot) outside war(jboss)?

I have spring boot application that I am running in jboss(instead of tomcat).
I am using spring profile for loading environment specific application.properties.
Profile specific application{env}.properties is working fine when placed under "src/main/resources/" however, it is not working when placed externally.
I tried setting property in jboss standalone.xml but application fails to start in jboss.
<property name="spring.config.location" value="C:/Dev/config/rt" />
Please suggest how to load the environment specific application.properties files that are not placed inside the war file.
I was expecting spring to pick the profile specific file from the folder but looks like that's not the case.
It appears that spring.config.location needs to have the absolute file location instead of folder location. so, updated spring.config.location in standalone.xml and it worked :
<property name="spring.config.location" value="C:/Dev/config/rt/application-dev.properties" />
You can provide externalized configuration file using below command when you are initially starting the application,
java -jar <your-jar-name.jar> --spring.config.location=<path><external property>
example :
java -jar mySample.jar --spring.config.location=./application-external.properties
read more
Generally it was picked very easily when it's placed under the 'src/main/resources' folder. suppose you have to different files for profiles like - application-dev.properties and application-prod.properties, you need to set only the current working profile in the application.properties like
spring.profiles.active=dev
and it will be picked easily. If it's doesn't you need to create a workaround by creating a new bat or sh file like run.bat and run.sh in the bin folder of the jboss and pass the file location like
--spring.config.location=
The complete command to be added in the bat/sh file will be
java -jar appName.jar --spring.config.name=application-dev --spring.config.location=c:/Dev/application-dev.properties

Spring Boot : log4j2.xml next to app.jar not read in production environment

I'm new to Spring Boot.
Under 'resources' there are 2 files :
-- resources
-- application.properties
-- log4j2.xml
In development environment everything works fine.
In production environment, I copy both files and put them next to the app.jar :
-- app_folder
-- my-app.jar
-- application.properties
-- log4j2.xml
When I start the my-app.jar, :
application.properties is read from app_folder, as intended
log4j2.xml is read from 'resources', the one under app_folder is ignored
Shouldn't it work this way out of the box ? What am I doing wrong ?
All I had to do was putting a file named
name-of-my-spring-boot-jar-file.conf
in the same directory as the jar file itself.
Content of conf file :
JAVA_OPTS="-Dlog4j.configurationFile=/home/<user>/log4j2.xml"
Why do you assume it works that way?
It is true that according to its documentation Spring Boot will detect application.properties correctly if it is placed in the same directory as the jar file.
That being said log4j2.xml is not read by Spring Boot but by Log4J2 Framework and according to its documentation that framework oly looks for files on the classpath.
If the file is elsewhere you need to specify the path like this:
java -Dlog4j.configurationFile=path/to/log4j2.xml -jar my-app.jar
Edit:
my-app.jar is an executable, so I don't invoke the 'java' command when starting my-app.
Yes you do. Even if you're invoking it from a GUI (eg. double clicking in Explorer in Windows) it still runs java -jar my-app.jar under the hood.
Isn't the same folder the app.jar resides in considered classpath ?
Again, why would you assume that it is?
In the Java documentation (here for Java 8) in section Folders and Archive Files it clearly says that if the classes are stored in a jar, then the classpath includes only stuff from the jar (although in case of Spring Boot due to the custom classloader it also includes jars embedded in the jar, whch would normally not be the case - see Executable Jar Format).
You really should read the documentation (or at least relevant parts of it) before attempting to use any framework/library/programming language you have not used before - it will save you a lot of time in the long run.

Build Spring project for run on another system

I Successfully create a spring boot project on my own local system. I want to build a jar file so I can install it on remote server. so I had to configure server address and mySql address of remote server but I can not Build and it have many errors, and they all right cause my system can not see the remote server address and database.
this is my .properties file:
spring.datasource.url=jdbc:mysql://localhost:8081/aths
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
server.address=192.168.24.250
server.port=8080
how can handle it for running on another configurations? ( another IP, datasource, and ...)
Am I doing it right or not? thanks
You can use spring profiles here :
Create different property files for different profiles using application-{profile}.properties format, e.g. application-dev.properties for dev, and application-prod.properties for production put your profile specific configurations in them. Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE environment variable or spring.profiles.active system property.
and at the end, you will run your jar file with command
java -jar -Dspring.profiles.active=prod application.jar
You can have different application.properties within your resources folder and use spring profiles for example application-{profile}.properties and run the application with the specified profile. However this still limits the configuration items to what has been hard coded within the properties files. When running the application, if it was to be distributed to other people, where non of the profiles are supported you can provide a properties file at start up.
So in the same directory for example as the .jar file create a file named application.properties with empty place holders for all the variables required for the application so the admin can enter the details correct for them. Then they will be required to start the application with the following command
java -jar 'applicaitonname.jar -Dspring.config.name="file:/path/to/application.properties"
Or springboot will load properties from application.properties files in the following locations:
A /config subdirectory of the current directory.
The current directory
Failing that the default application.properties from the resources folder will be loaded.

Resources