【问题标题】:how to turn off or supress maven test loading messages如何关闭或抑制 Maven 测试加载消息
【发布时间】:2013-08-06 23:20:56
【问题描述】:

我正在使用 maven 运行我的测试。当测试运行时,有很多无用的(至少对我来说)消息输出到控制台。我的 pom.xml 有以下插件配置。

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.15</version>
 <configuration>
  <includes>
   <include>**/*Tests.java</include>
  </includes>
  <systemPropertyVariables>
  <log4j.configuration>file:${project.build.testOutputDirectory}/log4j.properties</log4j.configuration>
  </systemPropertyVariables>
 </configuration>
</plugin>

当我跑步时

mvn clean test

我看到一堆如下所示的消息。

[parsing started RegularFileObject[...]]
[search path for source files: ...]
[search path for class files: ...]
[loading ZipFileIndexFileObject[...]]

如何关闭这些?

【问题讨论】:

    标签: maven junit maven-3 junit4


    【解决方案1】:

    这取决于这些消息是如何从您的代码中打印出来的。如果使用 System.out.println() 打印它们,您可能无法抑制它们。如果使用了LOG4J,可以把下面的log4j.xml放到src/test/resources或者你的项目中:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        <appender name="devnull" class="org.apache.log4j.varia.NullAppender"/>
        <root>
            <priority value="info"/>
            <appender-ref ref="devnull"/>
        </root>
    </log4j:configuration>
    

    我在我的项目中使用了这种方法,效果很好。但是,它可能取决于您的类路径中是否有任何其他 log4j.xml(例如在 src/main/resources 中)。如果是 - 需要额外的工作来确保 log4j 为单元测试环境选择正确的配置文件。

    【讨论】:

    • 我没有做任何 System.out.print 的。似乎这些消息是从 maven surefire 插件在扫描类和 jar 以运行测试时输出的。但我不得不同意这些似乎是 System.out.print 消息,因为它们的输出与 log4j.properties 或 logback.xml 中定义的不同。顺便说一句,我确实尝试将该 log4j.xml 文件放入我的 src/test/resources 文件夹中,但这并没有帮助。现在我不得不怀疑,如果这是真的,他们为什么要使用 System.out.println 以及为什么在这些看似无用的消息上。
    • 好吧,我刚刚发现这些消息可以由“javac -verbose”生成。我按照maven.apache.org/plugins/maven-compiler-plugin/… 关闭了它,现在这些输出消失了。
    • 很高兴知道你已经解决了。我认为值得在这里给出答案并接受它,以便其他人看到正确的答案。
    【解决方案2】:

    这是我解决它的方法。问题在于将编译设置为详细(例如 java -verbose)。在我的 pom.xml 中,我做了

    <verbose>true</verbose>
    

    <verbose>false</verbose>
    

    在我的 pom.xml 的 build/plugins/plugin 部分,以下是我现在拥有的。

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.0</version>
     <configuration>
      <compilerArguments>
       <Xlint />
      </compilerArguments>
      <verbose>false</verbose>
      <source>${java.version}</source>
      <target>${java.version}</target>
      <showWarnings>true</showWarnings>
      </configuration>
    </plugin>
    

    所以那些“无用”的输出与 surefire 插件无关,而是与编译器插件的配置有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 2017-07-06
      • 2018-03-22
      • 2011-02-22
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多