How to intercept maven plugin build log

371 Views Asked by At

I've created a maven Mojo that runs another Mojo using mojo-executor. When this get executed, the output of that Mojo execution is going to default output. So, when it get executed, it prints the following:

[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, all those warnings are expected because I've created some bugcheckers with Errorprone. But the problem is that I must remove this output from the standard output and put it into a file. But only that part.

What I've tried so far was to redirect my output to a file when it get executed:

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

Premises: - I can not use mvn --logFile or -l because they remove everything from standard output and place it inside the file, also the solution should be not dependable by user placing a --logFile or something like it.

1

There are 1 best solutions below

0
On

Ok, I've tried two approaches and both of them worked:

  • Created an ASM agent interceptor that intercepted all inputs to PrintStream.print(), but that was too hard and was hard to control results. You can check how to do it in ASM guide at http://download.forge.objectweb.org/asm/asm4-guide.pdf

  • The other option was staying with reassignment of System.out and System.err. It was so much better because it's easier to implement and the results can be better controlled since we can manage the maven goals.