1.概述
现在开发使用前后端开发机制,在部署的时候,我们需要将前后端分别打包,使用nginx 进行统一部署。这样就比较复杂,我们可以使用前后端打包到一个jar中,这样我们只需要一个包就可以了。
2.实现
我们只需要将前端的编译好的文件,在打包时,将前端文件 copy 到 resources 目录下的 static 目录。
我们可以在 pom.xml 做如下配置:
<build>
<resources>
<resource>
<directory>webapp/dist</directory>
<targetPath>static</targetPath>
</resource>
</resources>
</build>
我们也可以使用 resource插件来实现:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<executions>
<execution>
<id>copy-web</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../jpaas-front/ac-main/dist</directory>
</resource>
</resources>
<!--输出路径-->
<outputDirectory>${basedir}/src/main/resources/static/main</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
3.当用户访问根时跳转
我们增加一个控制器:
@Controller
public class IndexController {
@GetMapping("/")
public String index(){
return "/main/index.html";
}
}
这样用户访问 根时 ,他就会返回 static 下的 /main/index.html 视图到前端。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人