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

如何移除部署到WildFly的Spring Boot项目URL中的版本号

如何移除Spring Boot+Maven项目部署到WildFly后的URL版本号?

我正在学习一门基于Vaadin、Maven、Spring Boot和JPA的Java EE课程,已经成功将Maven项目及模块部署到JBoss WildFly服务器。目前遇到一个小问题:课程中的应用访问URL为http://localhost:8080/univers-web/,但我编译运行后必须使用带版本号的URL:http://localhost:8080/univers-web-1.3.6.RELEASE。请问是否有办法移除URL中的项目版本号?

以下是我的pom.xml文件起始代码片段:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <artifactId>univers-web</artifactId> 
  <packaging>war</packaging> 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <version>1.3.6.RELEASE</version> 
    <artifactId>spring-boot-starter-parent</artifactId> 
  </parent> 
  <properties> 
    <java.version>1.8</java.version> 
  </properties> 
  . . . 
</project>

嘿,这个问题其实挺常见的,本质是Maven默认会把artifactId-版本号作为最终打包的war文件名,而WildFly部署时会直接用这个文件名作为应用的上下文路径,所以才会出现带版本号的URL。有几个简单的办法可以解决:

  • 方法一:直接配置Maven的finalName
    在你的pom.xml中添加<build>节点,指定finalName为你想要的上下文路径名称(也就是不带版本号的univers-web):

    <build>
        <finalName>univers-web</finalName>
    </build>
    

    这样Maven打包后生成的war文件就会是univers-web.war,WildFly部署后自然就会用/univers-web作为上下文路径了。

  • 方法二:通过Spring Boot Maven插件配置finalName
    如果你更倾向于贴合Spring Boot的配置习惯,也可以在Spring Boot插件里指定finalName:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>univers-web</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    这个效果和方法一完全一样,只是配置的位置更聚焦在Spring Boot相关的插件上。

  • 方法三:临时修改WildFly的部署上下文路径(不推荐长期用)
    如果你只是临时测试不想改pom,也可以在WildFly的管理控制台里找到已部署的univers-web-1.3.6.RELEASE.war,手动修改它的上下文路径为/univers-web。不过这种方法每次重新打包部署后都要再改一次,不如前面两种一劳永逸。

选第一种或者第二种配置后,重新执行mvn clean package打包部署,就能用http://localhost:8080/univers-web/访问你的应用啦!


内容的提问来源于stack exchange,提问作者Christian Wolf Alves Hess

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:47:03