21

我正在使用以下插件配置调用“jetty:run”目标:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>7.4.4.v20110707</version>
  <configuration>
    <scanIntervalSeconds>5</scanIntervalSeconds>
    <connectors>
      <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
        <port>80</port>
      </connector>
    </connectors>        
  </configuration>
</plugin>

尽管我的项目将 slf4j 声明为依赖项,但 Jetty 拒绝将任何内容记录到 slf4j。如果我将“-Dorg.eclipse.jetty.util.log.DEBUG=true”传递给 JVM,Jetty 会输出大量日志,但它们似乎会转到 stderr 而不是 slf4j。有任何想法吗?

4

2 回答 2

14

回答我自己的问题:

  1. 插件看不到项目依赖项。您需要<dependencies><plugin>.

  2. 您需要指定一个具体的 slf4j 实现,例如 logback。指定 slf4j 是不够的。

最终结果应如下所示:

  <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.4.4.v20110707</version>
    <configuration>
      <scanIntervalSeconds>5</scanIntervalSeconds>
      <connectors>
        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
          <port>80</port>
        </connector>
      </connectors>        
    </configuration>
    <dependencies>
      <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>0.9.29</version>
      </dependency>
    </dependencies>
  </plugin>
于 2011-08-30T21:45:06.433 回答
6

稍微扩展吉利的答案;使用 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>
于 2013-09-15T19:05:18.467 回答