【问题标题】:No verbose output when fork option is used for maven-compiler-plugin当 fork 选项用于 maven-compiler-plugin 时没有详细输出
【发布时间】:2016-08-16 12:56:25
【问题描述】:

如果我想查看编译器输出,通常我会为 maven-compiler-plugin 启用 verbose 选项。

但是,当它与fork 选项一起使用时,这将不起作用。 编译正在另一个进程中运行,maven 似乎没有将输出重定向到控制台。

我的代码示例如下所示

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <fork>true</fork>
                <verbose>true</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>

有谁知道我怎样才能看到编译过程中发生的事情?

在这个link 中,我看到鼓励同时使用这两个选项(fork 和 verbose)。

但是,正如我已经提到的,实际上这两个选项在使用时效果不佳 在一起。

【问题讨论】:

  • 可以确认这一点。奇怪的是,我找不到关于此的错误报告。最接近的似乎是issues.apache.org/jira/browse/MCOMPILER-81,但它是自动关闭的=/。
  • @Tunaki 我会在其他开发人员同意的情况下每年执行一次自动关闭以清理 JIRA。票至少三年不碰,无论重要与否,都不会再碰。
  • @Michael-O 我正在写一个答案,它看起来像是 plexus 编译器中的一个错误。

标签: maven maven-compiler-plugin


【解决方案1】:

更新 (25/06/2016)This issueplexus-compiler 已修复,问题中的给定代码将从 2.8 版开始工作。


通读源码,这是plexus-compiler 2.7 的一个bug,这是maven-compiler-plugin 3.5.1 内部用来编译Java 源代码的库。

所以它在代码中是这样的:maven-compiler-plugin 填充一个名为 CompilerConfiguration 的对象,并根据 POM 中给定的配置元素设置其 forkverboseverbose 元素is correctly read and added to the compiler arguments

if ( config.isVerbose() )
{
    args.add( "-verbose" );
}

然后,有一个开关depending on whether we're forking or not


如果不是,我们最终会归结为调用 javac 的代码及其输出 is stored in the compilation result

ok = (Integer) compile.invoke( null, new Object[]{ args, new PrintWriter( out ) } );

messages = parseModernStream( ok.intValue(), new BufferedReader( new StringReader( out.toString() ) ) );

终于returned with:

return new CompilerResult( success, messages );

到目前为止一切顺利,maven-compiler-plugin 将循环这些消息并将它们输出到控制台。由于设置了verbose 参数,我们将拥有所有消息。


如果是,则检索一个可执行文件(默认为 PATH 中的 javac)和 the compiler arguments are correctly set

for ( String key : config.getCustomCompilerArgumentsAsMap().keySet() )
{
    if ( StringUtils.isNotEmpty( key ) && key.startsWith( "-J" ) )
    {
        cli.addArguments( new String[]{ key } );
    }
}

我们可以通过使用-X 在调试模式下运行 Maven 来确认这一点,我们将看到以下消息:

[DEBUG] Excutable: 
[DEBUG]  "C:\Program Files\Java\jdk1.8.0_74\bin\javac.exe"
[DEBUG] Command line options:
[DEBUG] -d ... -classpath ... -sourcepath ... -s ... -g -target 1.8 -source 1.8 -verbose

注意最后的-verbose

然后,这就是错误。标准输出将存储在out 变量中

CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();

正确用作编译方法的参数,but completely ignored afterwards

returnCode = CommandLineUtils.executeCommandLine( cli, out, err );

messages = parseModernStream( returnCode, new BufferedReader( new StringReader( err.getOutput() ) ) );

请注意,稍后将形成插件输出的消息是如何仅填充错误而不是标准输出的。简单来说:标准输出在详细模式下被正确设置,但它被忽略并且没有翻译成正确的编译结果。


我没有看到解决方法(除了不分叉)。我在他们的 GitHub 中创建了 issue 20 来跟踪这个。

【讨论】:

  • 您可能希望使用 Plexus 编译器报告此问题。欢迎 PR,我渴望合并。
  • @Michael-O 我创建了它。当我有时间时会尝试开发补丁:)!
  • 问题已在上游修复,您可能需要更新您的帖子。
猜你喜欢
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 2017-05-24
  • 2020-01-01
  • 2013-01-14
  • 2013-12-14
相关资源
最近更新 更多