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

如何缩减Java Playwright依赖体积?Jar包增170MB致流水线卡顿

解决Playwright导致Jar包体积过大的瘦身方案

你的判断完全正确——默认的playwright依赖包捆绑了Chromium、Firefox、WebKit三种浏览器的二进制文件,这正是Jar包暴增170MB的核心原因。针对你仅调用本地Chrome的场景,按以下步骤优化即可:

1. 替换Playwright依赖为playwright-core

playwright-core是Playwright的核心API包,仅包含核心逻辑,不附带任何浏览器二进制文件,体积仅几MB。直接修改POM中的依赖:

<dependency>
    <groupId>com.microsoft.playwright</groupId>
    <artifactId>playwright-core</artifactId> <!-- 替换为core版本 -->
    <version>1.49.0</version>
</dependency>

这个替换完全兼容你的现有代码,不需要修改任何业务逻辑——playwright-core和全量playwright的API完全一致。

2. 移除冗余的环境变量配置

由于playwright-core本身不会自动下载浏览器,代码中设置的PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD环境变量可以直接删除,简化代码:

// 移除原有的环境变量配置代码
// Map<String,String> env = new HashMap<>();
// env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD","1");
// Playwright playwright = Playwright.create(new Playwright.CreateOptions().setEnv(env));

// 改为直接初始化
Playwright playwright = Playwright.create();

3. 可选:Spring Boot打包额外优化

如果Jar包仍有精简空间,可以给Spring Boot Maven插件添加配置,启用分层打包或排除明确不需要的依赖:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>3.3.4</version>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- 启用分层打包,加快流水线构建/部署速度 -->
        <layers>
            <enabled>true</enabled>
        </layers>
        <!-- 示例:确认不需要自动配置时可排除,否则请勿添加 -->
        <!--
        <excludes>
            <exclude>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
            </exclude>
        </excludes>
        -->
    </configuration>
</plugin>

注意:排除spring-boot-autoconfigure前要确认你的应用确实不需要它,否则会导致Spring功能异常。

效果验证

修改后重新打包,Jar包体积会直接降到几十MB以内(具体大小取决于你的其他依赖),同时完全保留原有表单填充、提交、截图的所有功能。

内容的提问来源于stack exchange,提问作者Facundo Laxalde

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 14:22:05