2013-08-06 21 views
1

我正在使用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[...]] 

如何關閉這些功能?

回答

3

這是我解決它的方式。問題在於將編譯設置爲冗長(例如java -verbose)。在我的pom.xml,我在編譯/插件/插件,我的pom.xml的部分由

<verbose>true</verbose> 

<verbose>false</verbose> 

,以下是我現在有。

<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> 

所以這些「無用」的輸出,沒有什麼做的萬無一失插件,而是與編譯器插件的配置。

-1

這取決於如何從您的代碼打印這些消息。如果它們使用System.out.println()打印,則可能無法抑制它們。如果使用log4j,您可以把以下的log4j.xml的src /測試/資源或您的項目:

<?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爲單元測試環境選擇正確的配置文件。

+0

我沒有做任何System.out.print的。它似乎是這些消息是從Maven surefire插件輸出,當它掃描類和罐子運行測試。但我必須同意,它們似乎是System.out.print消息,因爲它們的輸出與log4j.properties或logback.xml中定義的內容不相似。順便說一下,我試圖把這個log4j.xml文件放到我的src/test/resources文件夾中,但這沒有幫助。現在我不得不懷疑,如果這是真的,他們爲什麼要使用System.out.println以及爲什麼這樣看起來毫無用處的消息。 –

+0

好吧,我只是發現那些消息可以由「javac -verbose」生成。我根據http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html關閉了它,現在這些輸出消失了。 –

+0

Goot知道你已經解決了它。我認爲在這裏作出回答並接受它是值得的,所以其他人看到了正確的答案。 –