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

Posted

技术标签:

【中文标题】Maven:如何防止 exec 插件显示其堆栈跟踪?【英文标题】:Maven: how to prevent exec plugin to display its stack trace? 【发布时间】:2019-05-06 07:50:19 【问题描述】:

下面的简单 maven exec 插件显示了一个堆栈跟踪,这让我的用户很难理解正在发生的事情。如何防止堆栈跟踪并获得更清晰的输出?

这是一个简单的 pom.xml,执行命令 false 强制失败:

<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】:

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

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

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

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

如果您想进一步格式化输出,请考虑使用更高级的记录器。

https://maven.apache.org/maven-logging.html#Configuring_logging https://***.com/a/33629327/3656963

【讨论】:

@neves 在 Windows 主机上的 3.6.0 上为我工作。您可以尝试将其设置为 DEBUG 并查看是否获得详细输出以确认其正确的属性文件?

以上是关于Maven:如何防止 exec 插件显示其堆栈跟踪?的主要内容,如果未能解决你的问题,请参考以下文章

防止 Mongoose 堆栈跟踪错误

Maven错误:无法执行目标org.codehaus.mojo:exec-maven-plugin:1.2.1:exec

如何在 Maven 2 exec 插件的参数中使用空格

React Native显示加密堆栈跟踪而不是文件名

VueJS 堆栈跟踪不显示错误发生的位置

Maven exec 插件 - 执行 python 脚本