0

我创建了一个使用 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 或类似的东西,解决方案也不可靠。

4

1 回答 1

0

好的,我尝试了两种方法,它们都有效:

  • 创建了一个 ASM 代理拦截器来拦截 的所有输入PrintStream.print(),但这太难了,而且难以控制结果。您可以在http://download.forge.objectweb.org/asm/asm4-guide.pdf的 ASM 指南中查看如何操作

  • 另一种选择是继续重新分配 System.out 和 System.err。它好多了,因为它更容易实现,而且结果可以更好地控制,因为我们可以管理 maven 目标。

于 2016-08-06T19:59:13.140 回答