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 ...)
Related
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.
I'm using Intellij.
I have a jndi.properties file (used for connecting to a Wildfly instance).
By default, Maven includes this file inside the JAR , after build. This is not good, as the jndi.properties contains the URL and credentials to the Wildfly instance and in production these will be different.
I managed to exclude the jndi.properties file by placing it in a separate folder (called config), and then in Intellij -> Module Settings -> Sources I marked the config folder as Resources and Excluded. This way, when I run the application inside Intellij, it works fine. But when I build the JAR, even if I place a folder called config outside the JAR, I still get an exception because no jndi.properties file is found in the classpath.
The same happens if I declare the config folder in the Dependencies tab in the Module Settings.
I also tried specifying the classpath when I run the java cmd line tool, but it seems that -cp does not work along with -jar.
I know two alternatives, but I don't really like them: have jndi.properties in JAVA_HOME/lib (not good, it will be common for all applications) or at runtime (-Djava.naming.provider.url etc, not as clear as a configuration file in my opinion).
Is there any way I could specify the jndi.properties file location at runtime?
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.
I have a jgroups dependency which has a log4j2.xml in its JAR which is getting picked up by log4j instead of my config file.
How can I get Maven to exclude this file from the JAR dependencies?
...before its suggested, I can't use the system property to set the log4j2.xml file explicitly as I want to be able to start my project without setting any JVM variables in the IDE.
The shade plugin can exclude specific files - see here. Also I'd try if having your config file before the unwanted one on the classpath would make log4j pick yours up.
Also according to the docs log4j2-test.xml and log4j2.json on the classpath have precedence over log4j2.xml unless log4j.configurationFile system property is set, which btw you can also do from maven, so you don't have to set anything in your IDE.
I have a Java maven project where I need to use logback as the logging framework. I have placed the logback.xml configuration file in the src/main/conf folder. However, it looks like it is not getting picked up because src/main/conf is not in the class path. On building the project I copy all the contents of that folder to target/root/conf and wanted to have that directory as part of my classpath. Can anyone let me know how can I force that?
In a Maven project the default directory for resource file is src/main/resources. This is the layout directory,
if you still want to change this default, follow these instructions.