Maven – Exclude log4j.properties in Jar file

This example shows you how to use Maven to exclude the log4j.properties file from your Jar file.

Note
Please, DO NOT include the log4j.properties into the final Jar file, it will cause multiple log4j.properties files in the classpath, if someone is depending on your Jar, you may accidentally override their logging configurations, depends which Jar is loaded first.
pom.xml

<project>

  <build>
    <plugins>

	<!-- Make this jar executable -->
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-jar-plugin</artifactId>
		<configuration>
		        <!-- exclude log4j.properties -->
			<excludes>
				<exclude>**/log4j.properties</exclude>
			</excludes>
			<archive>
			    <manifest>
				<addClasspath>true</addClasspath>
				<mainClass>com.mkyong.core.utils.testing</mainClass>
				<classpathPrefix>dependency-jars/</classpathPrefix>
		            </manifest>
			</archive>
		</configuration>
	</plugin>

    </plugins>
  </build>

</project>

Please pass in your log4j.properties file via log4j.configuration system property like this


$ java -jar -Dlog4j.configuration=file:/full_path/log4j.properties final.jar

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Yusuftomcatgeek Recent comment authors
newest oldest most voted
Yusuf
Guest
Yusuf

When I run this, I get:

Error: Unable to access jarfile .configuration=file:D:devProjectsrrm_biptargetconflog4j.properties

I try to execute:
java -jar -Dlog4j.configuration=file:fullpathtolog4j.properties -jar my_jar.jar

I think after -jar, it directly waits for a jar, not options.

Thanks.

tomcatgeek
Guest
tomcatgeek

How would you provide log4j for tomcat container?