Spring Boot 将web项目打包成jar运行

本文介绍了如何在Spring Boot项目中整合React前端,将web应用打包成jar文件。首先,在src/main下创建webapp目录存放前端代码并排除在IDEA之外。接着,通过在pom.xml中添加配置来管理前端项目,并配置脚本将打包后的前端资源复制到resources/static。通过maven package命令,可以自动完成打包过程,启动jar文件后,访问IP:port即可查看到前端页面,这表明web项目已成功启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在src/main文件夹下创建webapp文件夹,将前端源码放入webapp下(并将webapp文件夹exclude掉,避免idea无法加载项目)
在这里插入图片描述
2. pom添加以下代码,使用maven管理前端项目

             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>npm install</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>npm</executable>
                            <arguments>
                                <argument>install</argument>
                            </arguments>
                            <workingDirectory>${basedir}/src/main/webapp</workingDirectory>
                        </configuration>
                    </execution>

                    <execution>
                        <id>npm run build</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>npm</executable>
                            <arguments>
                                <argument>run</argument>
                                <argument>build</argument>
                            </arguments>
                            <workingDirectory>${basedir}/src/main/webapp</workingDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

3. 添加以下代码将前端打包后文件复制到resources下的static文件夹(没有请手动创建)

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
            <executions>
                <execution>
                    <id>copy-spring-boot-webapp</id>
                    <!-- here the phase you need -->
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <encoding>utf-8</encoding>
                        <outputDirectory>${basedir}/src/main/resources/static</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/webapp/dist</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

上面两步操作均通过maven package命令完成,无需手动操作,执行完后即可以正常后端项目启动,启动后访问ip:port根目录可看到前端页面表示web项目启动成功,若失败请检查pom文件夹配置与webpack打包配置是否一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值