代码之家  ›  专栏  ›  技术社区  ›  neves

如何防止exec插件显示其堆栈跟踪?

  •  0
  • neves  · 技术社区  · 7 年前

    下面这个简单的maven exec插件显示了一个堆栈跟踪,这使得我的用户很难理解发生了什么。如何防止堆栈跟踪并获得更干净的输出?

    下面是执行命令时出现强制失败的简单pom.xml :

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>br.com.comp</groupId>
        <artifactId>Bdm</artifactId>
        <version>1.0</version>
        <description>Validating</description>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
    
                    <executions>
                        <execution>
                            <id>Validate</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <executable>false</executable>
                        <includeProjectDependencies>false</includeProjectDependencies>
                        <includePluginDependencies>false</includePluginDependencies>
                    </configuration>
    
                </plugin>
            </plugins>
        </build>
    </project>
    

    这里是脏输出:

    $ mvn compile
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Bdm 1.0
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Bdm ---
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory D:\projetos\hudson\ws\bdm\a01_bdm\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Bdm ---
    [INFO] No sources to compile
    [INFO]
    [INFO] --- exec-maven-plugin:1.6.0:exec (Validate) @ Bdm ---
    [ERROR] Command execution failed.
    org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
            at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
            at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
            at org.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:804)
            at org.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:751)
            at org.codehaus.mojo.exec.ExecMojo.execute(ExecMojo.java:313)
            at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
            at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
            at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
            at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
            at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
            at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
            at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
            at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
            at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
            at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
            at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
            at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.615 s
    [INFO] Finished at: 2018-12-04T17:53:35-02:00
    [INFO] Final Memory: 10M/145M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (Validate) on project Bdm: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
    

    这个堆栈跟踪是maven exec插件的,没有对我的用户有用的信息。 如何防止在第一个 [ERROR] 下一个 [INFO] ?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Revive    7 年前

    Maven 3.1+使用 slf4j-simple 默认情况下实现slf4j api。这不是很可定制的。通过编辑simple.logger.properties,可以尝试完全禁用此插件的日志记录。通常位于: ${maven.home}/conf/logging/simplelogger.properties

    添加以下行以禁用该插件的日志记录。这仍将导致输出中的初始错误,但隐藏完整堆栈跟踪

    org.slf4j.simpleLogger.log.org.codehaus.mojo.exec.ExecMojo=OFF
    

    这不是一个非常干净的解决方案,因为这可能隐藏有用的输出。

    如果要进一步格式化输出,请考虑移动到更高级的记录器。