【发布时间】:2016-07-29 17:32:11
【问题描述】:
我创建了一个使用 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 或类似的东西也不应该是可靠的解决方案。
【问题讨论】:
-
我认为设置系统标准输出和错误流是唯一的方法。你遇到过这个问题吗?
-
是的,它仍然没有得到正确的字符串。它在执行之后得到了所有的字符串。但是我检查了代码,没问题。
-
MavenProject 包含您可能需要查找不同的记录器实例或设置您自己的记录器实例的记录器,具体取决于您如何调用另一个mojo?但是如果没有具体的代码,就很难提供帮助......此外,您似乎没有使用记录器在您的 mojo 中创建输出?
标签: maven logging maven-3 maven-plugin maven-mojo