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

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

わかりました、私は2つのアプローチを試しましたが、どちらもうまくいきました:

  • へのすべての入力をインターセプトする ASM エージェント インターセプターを作成しましたPrintStream.print()が、それは難しすぎて、結果を制御するのが困難でした。その方法は、 http://download.forge.objectweb.org/asm/asm4-guide.pdfの ASM ガイドで確認できます。

  • もう 1 つのオプションは、System.out と System.err の再割り当てを維持することでした。実装が簡単で、maven の目標を管理できるため、結果をより適切に制御できるため、はるかに優れていました。

于 2016-08-06T19:59:13.140 に答える