You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何ng build部分输出写入错误日志?如何调整?关联exec-maven-plugin

Angular构建日志分流至错误日志的原因及解决方法

问题场景

执行Angular生产构建命令 ng build --configuration production 时,部分构建信息被写入错误日志:

- Generating browser application bundles (phase: setup)...
✔ Browser application bundle generation complete.
✔ Browser application bundle generation complete.
- Copying assets...
✔ Copying assets complete.
- Generating index html...
✔ Index html generation complete.

其余构建统计信息则写入标准日志:

Initial chunk files           | Names                                                 |  Raw size | Estimated transfer size
main.72ca292f8e263fb1.js      | main                                                  | 932.38 kB |               192.44 kB
styles.27fcf7fbc7060819.css   | styles                                                |  87.92 kB |                 9.32 kB
polyfills.033b0ddba07ded8a.js | polyfills                                             |  33.42 kB |                10.83 kB
runtime.76aa7a1b3ccad887.js   | runtime                                               |   3.02 kB |                 1.45 kB

                              | Initial total                                         |   1.03 MB |               214.05 kB

Lazy chunk files              | Names                                                 |  Raw size | Estimated transfer size
604.7a92aba1f690ea24.js       | module-a-module     |  20.59 kB |                 4.94 kB
649.ec93606de2d2b011.js       | module-b-module                         |  18.05 kB |                 4.79 kB
common.fa9b6bffd109eaa4.js    | common                                                |  12.07 kB |                 3.15 kB
295.7fd90ac08182d01c.js       | module-c-module                           |  10.53 kB |                 2.74 kB

Build at: 2024-05-08T08:39:20.285Z - Hash: 8f846dd1da30fa34 - Time: 4153ms
Done in 5.29s.

该现象仅在通过Maven的exec-maven-plugin插件执行构建时出现,问题根源在于插件对日志输出的处理逻辑。

原因分析

Angular CLI的日志输出逻辑为:将构建过程中的状态提示(如bundle生成、资产复制进度)写入标准错误流(stderr),而构建统计、最终结果等信息写入标准输出流(stdout)。

exec-maven-plugin默认会区分子进程的stderr和stdout:它将stderr的内容标记为错误日志,stdout的内容标记为普通日志。因此就出现了部分构建信息被归类到错误日志的情况。

调整方法

方法1:合并错误流与标准输出流

在exec-maven-plugin的配置中添加redirectErrorStream参数并设为true,插件会将子进程的stderr内容合并到stdout中输出,所有构建日志都会统一作为普通日志处理。

示例配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>angular-build</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>ng</executable>
                <arguments>
                    <argument>build</argument>
                    <argument>--configuration</argument>
                    <argument>production</argument>
                </arguments>
                <!-- 合并错误流到标准输出 -->
                <redirectErrorStream>true</redirectErrorStream>
            </configuration>
        </execution>
    </executions>
</plugin>

方法2:调整Angular CLI日志输出级别

通过Angular CLI的--log-level参数控制输出内容,减少stderr的输出量。例如设置日志级别为warn或error,则只有警告或错误信息会写入stderr,构建过程提示会被抑制:

ng build --configuration production --log-level warn

不过这种方法会丢失部分构建过程的状态提示,需根据实际需求选择。

内容的提问来源于stack exchange,提问作者Lorenz Pfisterer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 13:07:47