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.
Related
vertx starter use shadow jar plugin to package a fat-jar
log4j2.xml will be put into the jar file
How can I run the jar with different log levels? (With springboot I can set -Dspring.profile.active=test to use application-test.yml for switch on debug logging)
You can make Log4j2 use another configuration file by setting the log4j2.configurationFile system property, as explained in the configuration section of the documentation:
java -Dlog4j2.configurationFile=/path/to/log4j2.xml -jar myapp.jar
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 ...)
My firm is trying to find a way to put some type of version file or versioning on a EAR and or WAR using Maven.
What we need is as we do the Maven Build is to take a version number out of a file and somehow put it into a EAR or WAR or have some better way..
I would like to hear form others on how you do this.
We are using Maven, Git, Bamboo and more. so if somehow Git can put a version or commit number into a file that then Maven can use it would be great
You have at least to options:
Customize the manifest entries.
The war plugin allows you to add some information into the manifest file. Some examples are on the usage page: https://maven.apache.org/plugins/maven-war-plugin/examples/war-manifest-guide.html (note: today its ${project.version} and not ${pom.version}.
The ear plugin has options to specify a manifest file or set some environment settings: https://maven.apache.org/plugins/maven-ear-plugin/examples/specifying-env-entries-for-the-generated-application-xml.html
Add the values to a property file and use filters to replace the placeholders.
Create a simple text or property file in src/main/resources, something like application.properties:
app.version=${project.version}
app.description=${project.description}
and then enable resource filtering: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html for this file.
You can also access environment variables: ${env.variable_name} and using the build number from bamboo (passed as a parameter to the build).
Plugins like the http://www.mojohaus.org/buildnumber-maven-plugin/ allow you to also add the SVN revision number or Git Sha1 Commit Hash.
If you are using the release plugin maven adds some information by its own into the maven folder of the released jar or war (inside the meta-inf folder).
I have a dependency jar in my current project where i need to enable logging from that jar.
I have included logging.properties in the Home/lib folder of my project but the console is still not logging from that dependency jar. In the logging.properties file I have set the level as info for that package.
org.jitsi.impl.neomedia.level=INFO
In the dependency class which is being called I have logged using log.INFO.
You can specify the file from which you will be reading the properties using
java -Djava.util.logging.config.file=myfile
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.