稍微扩展吉利的答案;使用 properties-maven-plugin 是设置系统属性的便捷方法,而不必在命令行上指定它们。我提供了 logback 和 log4j 的示例。除了 Gili 答案中的 jetty-maven-plugin 配置之外,将此插件块添加到您的 pom.xml 中。
回溯:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<!-- makes jetty log the exception if it fails to initialize slf4j -->
<property>
<name>org.eclipse.jetty.util.log.IGNORED</name>
<value>true</value>
</property>
<!-- Location of logback config -->
<property>
<name>logback.configurationFile</name>
<value>/path/to/logback.xml</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
日志4j:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<!-- makes jetty log the exception if it fails to initialize slf4j -->
<property>
<name>org.eclipse.jetty.util.log.IGNORED</name>
<value>true</value>
</property>
<!-- this tells where the log4j configuration is -->
<property>
<name>log4j.configuration</name>
<value>file:./src/main/resources/log4j.properties</value>
</property>
<!-- this can be uncommented to debug startup log4j itself,
e.g. how it locates log4j.properties etc -->
<!--
<property>
<name>log4j.debug</name>
<value></value>
</property>
-->
</properties>
</configuration>
</execution>
</executions>
</plugin>
同样对于 log4j,自然使用 jetty-maven-plugin 的以下依赖项而不是 logback-classic:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
...
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
</plugin>