我创建了一个使用 mojo-executor 运行另一个 Mojo 的 maven Mojo。执行此操作后,该 Mojo 执行的输出将变为默认输出。因此,当它被执行时,它会打印以下内容:
[INFO] Compiling 1 source file to /Users/sergio/hello-world/target/classes
[INFO]
[INFO] --- utqg-maven-plugin:1.1.0-SNAPSHOT:errorprone (default) @ my-app ---
/Users/sergio/hello-world/src/test/java/com/mycompany/App2Test.java:30: warning: [UTQG:HelperMethodCannotBePublic] Helper Methods of test classes cannot be public.
public String getName() {
^
(see https://www.mycompany.com/utqg/HelperMethodCannotBePublic)
/Users/sergio/hello-world/src/test/java/com/mycompany/service/MyServiceTest.java:14: warning: [UTQG:HelperMethodCannotBePublic] Helper Methods of test classes cannot be public.
public void myHelperMethod(){
^
(see https://www.mycompany.com/utqg/HelperMethodCannotBePublic)
/Users/sergio/hello-world/src/test/java/com/mycompany/service/MyServiceTest.java:170: warning: [UTQG:E:UseDeprecatedStatementsNotAllowed] Usage of Deprecated Classes, Methods or Variables Not Allowed
final URL url = new File(".").toURL();
^
(see https://www.mycompany.com/utqg/HelperMethodCannotBePublic)
Note: /Users/sergio/hello-world/src/test/java/com/mycompany/service/MyServiceTest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 warnings
好的,所有这些警告都是意料之中的,因为我已经创建了一些带有 Errorprone 的错误检查器。但问题是我必须从标准输出中删除此输出并将其放入文件中。但只有那一部分。
到目前为止,我尝试的是在执行时将输出重定向到文件:
PrintStream originalStream = System.out;
try {
PrintStream ps = new PrintStream(new FileOutputStream(ERRORPRONE_FILE, false));
System.setOut(ps);
} catch (FileNotFoundException e){
LOGGER.error("ErrorProneRunMojo", e);
throw new MojoExecutionException(e.getMessage());
}
// do my logic of calling the plugin here
System.out.flush();
System.setOut(originalStream);
前提: - 我不能使用mvn --logFile
,或者-l
因为他们从标准输出中删除所有内容并将其放在文件中,而且用户放置 --logFile 或类似的东西,解决方案也不可靠。