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

如何调整Angular-SpringBoot-Maven构建以生成Tomcat可部署WAR包?

调整方案以生成Tomcat可部署WAR包并保留JAR选项

1. 修改delivery模块的启动类

让delivery模块的Spring Boot启动类继承SpringBootServletInitializer,重写configure方法,这是Spring Boot应用部署到外部Servlet容器的必要步骤:

@SpringBootApplication
public class DeliveryApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DeliveryApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(DeliveryApplication.class, args);
    }
}

2. 配置delivery模块的pom.xml实现多打包类型

在delivery模块的pom.xml中添加Maven Profiles,分别控制JAR和WAR的构建逻辑,同时保留默认JAR输出:

<!-- 默认打包类型为JAR -->
<packaging>jar</packaging>

<profiles>
    <!-- 默认激活的JAR构建Profile -->
    <profile>
        <id>jar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- WAR构建Profile -->
    <profile>
        <id>war</id>
        <build>
            <plugins>
                <!-- 保留Spring Boot重打包逻辑 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- 配置Maven WAR插件,禁用web.xml检查 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.2</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <!-- 排除内置Tomcat,使用外部Tomcat容器提供的Servlet环境 -->
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>

    <!-- 可选:一次构建同时生成JAR和WAR的Profile -->
    <profile>
        <id>both</id>
        <build>
            <plugins>
                <!-- 生成带jar分类器的JAR包 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>jar-build</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <configuration>
                                <classifier>jar</classifier>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- 生成带war分类器的WAR包 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.2</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <classifier>war</classifier>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

3. 执行构建

  • 生成JAR包(默认):
    mvn clean package
    
  • 生成WAR包:
    mvn clean package -Pwar
    
  • 一次构建同时生成JAR和WAR:
    mvn clean package -Pboth
    

4. 验证WAR部署

将生成的WAR包放入Tomcat的webapps目录,启动Tomcat后,应用会自动解压部署。访问路径为http://<tomcat-host>:<port>/<war包名称>(若需要根路径部署,可将WAR包重命名为ROOT.war)。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 22:43:25