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

模块化Java应用使用Playwright时maven-jlink-plugin构建报错如何解决

问题背景

我希望在模块化Java应用中使用Playwright库。

项目主类代码如下:

package group.test;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class App 
{
  public static void main(String[] args) 
  {
    try (Playwright playwright = Playwright.create()) 
    {
      Browser browser = playwright.chromium().launch();
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      System.out.println(page.title());
    }
  }
}

对应的module-info.java文件配置如下:

module App.module 
{
      requires playwright;
}

使用Maven的maven-jlink-plugin构建项目时,出现如下报错:

[INFO] --- maven-jlink-plugin:3.1.0:jlink (default-jlink) @ test ---
[INFO]  -> module: driver ( C:\Users\User\.m2\repository\com\microsoft\playwright\driver\1.17.1\driver-1.17.1.jar )
[INFO]  -> module: playwright ( C:\Users\User\.m2\repository\com\microsoft\playwright\playwright\1.17.1\playwright-1.17.1.jar )
[INFO]  -> module: com.google.gson ( C:\Users\User\.m2\repository\com\google\code\gson\gson\2.8.6\gson-2.8.6.jar )
[INFO]  -> module: driver.bundle ( C:\Users\User\.m2\repository\com\microsoft\playwright\driver-bundle\1.17.1\driver-bundle-1.17.1.jar )
[INFO]  -> module: App.module ( D:\programming\eclipse-workspace\test\target\classes )
[ERROR]
[ERROR] Error: Module driver.bundle contains package com.microsoft.playwright.impl, module playwright exports package com.microsoft.playwright.impl to driver.bundle

报错核心原因是Java模块系统的拆分包禁令:同一个包com.microsoft.playwright.impl同时出现在两个不同的模块playwright和driver.bundle中,jlink构建时会直接拦截这种不合法的模块配置。


本地修复方案

普通使用者完全可以在本地修复该问题,不需要修改官方依赖源码,操作步骤如下:

  • 第一步:排除多余的driver-bundle依赖
    该依赖是全平台聚合依赖,普通模块化项目不需要引入,直接在pom.xml的playwright依赖项中增加排除规则即可:
    <dependency>
        <groupId>com.microsoft.playwright</groupId>
        <artifactId>playwright</artifactId>
        <version>1.17.1</version>
        <exclusions>
            <exclusion>
                <groupId>com.microsoft.playwright</groupId>
                <artifactId>driver-bundle</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  • 第二步:单独引入对应运行平台的driver依赖
    排除聚合包后,单独引入当前开发/运行环境对应的driver包即可,例如Windows环境引入配置如下:
    <dependency>
        <groupId>com.microsoft.playwright</groupId>
        <artifactId>driver</artifactId>
        <version>1.17.1</version>
    </dependency>
    
    不需要引入其他平台的driver包,避免无关模块进入jlink构建路径触发冲突。
  • 第三步:按需调整module-info配置
    如果启动时出现Gson序列化相关的模块访问报错,在模块声明中增加对应的依赖即可,无报错可以跳过该步骤:
    module App.module 
    {
        requires playwright;
        requires com.google.gson;
    }
    
  • 第四步:清理缓存重新构建
    执行mvn clean package重新触发jlink构建,拆分包冲突报错会直接消失。

补充:如果不想手动调整依赖结构,直接将Playwright版本升级到1.20.0及以上即可,官方从该版本开始修复了模块路径下的拆分包问题,升级后无需额外配置即可正常通过jlink构建。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 06:09:23