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

如何在Liberty中通过server.xml为EAR打包的WAR配置上下文根?

Liberty服务器上下文根覆盖失效问题

我尝试在Liberty服务器配置中设置上下文根为new-ctx,修改了server.xml:

<application id="my-ear" location="my-ear.ear" name="my-ear" context-root="new-ctx"/>

这么做是为了覆盖EAR层级pom.xml里的设置——该pom通过maven-ear-plugin生成的application.xml为WAR配置的上下文根是old-ctx:

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <modules>
                    <webModule>
                        <groupId>io.openliberty.guides</groupId>
                        <artifactId>guide-maven-multimodules-war</artifactId>
                        <uri>my.war</uri>
                        <contextRoot>old-ctx</contextRoot>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>

但操作后并未生效,服务器日志仍显示旧的上下文根:

[INFO] [AUDIT   ] CWWKT0016I: Web application available (default_host): http://<host>:9080/old-ctx/

问题原因

Liberty中直接在<application>元素上设置context-root无法覆盖EAR包内WAR模块的上下文根,因为EAR内部的application.xml已经明确指定了WAR的上下文根,优先级更高。

解决方案

需要在server.xml的<application>元素内嵌套<webApplication>元素,明确指定对应WAR模块的上下文根,示例如下:

<application id="my-ear" location="my-ear.ear" name="my-ear">
    <!-- location对应EAR内WAR的uri,即pom里配置的my.war -->
    <webApplication id="my-war" context-root="new-ctx" location="my.war"/>
</application>

另外,修改配置后建议清理Liberty服务器的workarea目录,避免缓存影响,然后重启服务器确保新配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 03:15:11