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

Jetty EE10 Maven Plugin路径含空格无法解析求助

Jetty EE10 Maven插件路径含空格的解决办法

问题根源

Jetty的MavenWebAppContext在解析resourceBase时直接调用java.net.URI.create(),而空格属于URI规范中的非法字符,常规的%20替换、反斜杠转义、引号包裹等方式均无法绕过该解析逻辑。


可行解决方法

1. 使用Maven内置属性替代硬编码路径

利用Maven提供的${basedir}(项目根目录)、${project.build.directory}(构建输出目录)等内置属性,自动处理路径中的空格,同时避免硬编码路径的维护问题。修改后的插件配置如下:

<plugin>
  <groupId>org.eclipse.jetty.ee10</groupId>
  <artifactId>jetty-ee10-maven-plugin</artifactId>
  <configuration>
    <scan>1</scan>
    <contextXml>${basedir}/src/main/jettyconf/context.xml</contextXml> 
    <jvmArgs>-Xms1G -Xmx4G</jvmArgs>
    <httpConnector>
      <host>127.0.0.1</host>
      <port>8888</port>
    </httpConnector>
    <webApp>
      <resourceBases>
        <resourceBase>${basedir}/src/main/webapp</resourceBase>
        <resourceBase>${project.parent.build.directory}/war/</resourceBase>
      </resourceBases>
    </webApp>
  </configuration>
</plugin>
  • 多模块项目中,确认${project.parent.build.directory}指向父项目的构建目录,若为单模块可直接使用${project.build.directory}。

2. 自定义Maven属性封装带空格路径

如果必须使用非标准目录路径,可在pom.xml的<properties>节点中定义属性,Maven会自动处理路径中的空格:

<properties>
  <webapp.root>/home/user/MyApp/Git Repository/MyApp/MyApp - Local Server/src/main/webapp</webapp.root>
  <war.output>/home/user/MyApp/Git Repository/MyApp/target/war/</war.output>
</properties>

随后在插件配置中引用这些属性:

<resourceBase>${webapp.root}</resourceBase>
<resourceBase>${war.output}</resourceBase>

3. 自定义上下文类绕过URI解析(最后手段)

若上述方法均无效,可通过自定义MavenWebAppContext子类,跳过URI.create()的解析逻辑,直接使用File路径创建资源:

  1. 编写自定义上下文类:
public class CustomMavenWebAppContext extends MavenWebAppContext {
  @Override
  public void setResourceBases(String[] resourceBases) {
    if (resourceBases == null) {
      super.setResourceBases(null);
      return;
    }
    List<Resource> resourceList = new ArrayList<>();
    ResourceFactory factory = ResourceFactory.of(getServletContext());
    for (String basePath : resourceBases) {
      try {
        resourceList.add(factory.newResource(Paths.get(basePath)));
      } catch (IOException e) {
        throw new IllegalArgumentException("Invalid resource path: " + basePath, e);
      }
    }
    setResources(resourceList.toArray(new Resource[0]));
  }
}
  1. 将该类打包到项目的classpath中,然后在插件配置中指定自定义类:
<webApp>
  <contextClass>com.yourpackage.CustomMavenWebAppContext</contextClass>
  <resourceBases>
    <!-- 路径配置 -->
  </resourceBases>
</webApp>
  • 注意:该方法会修改Jetty的默认资源加载逻辑,可能引发兼容性问题,仅作为最后尝试方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 15:22:08