log4j, maven jar plugin, and properties file
I'm cr开发者_JAVA百科eating a jar with the maven-jar-plugin. I can't for the life of me get this jar to read a log4j properties file which is in the same directory as my jar file. My steps are:
- mvn package
- copy log4j.properties to target dir
- java -Dlog4j.configuration = log4j.properties -cp . -jar Myjar
I've tried multiple variations of this. It seems like this should be an easy thing to do.
FWIW here's the relevant part of my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>loadtester.TestClient</mainClass>
<packageName>loadtester</packageName>
<addClasspath>true</addClasspath>
<classpathPrefix>dependency</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
When you call java -jar
, -cp
entries are ignored. Thus log4j.properties is not read.
As @Dave has mentioned, you can include log4j.properties
in the jar. Alternately, you can try the following
java -Dlog4j.configuration=file://full/path//of/log4j.properties -jar Myjar
try copying it to target/classes
Is there any reason you don't have that in your source under src/main/resources/log4j.properties?
精彩评论