需求说明,原项目使用Vue及Springboot前后端分离开发,但是因为最后需要打包成exe,为了部署方便,决定将Vue生成的静态文件打包在Springboot资源目录中,再打成jar包实现访问。
@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: %~dp0当前批处理文件所在的目录
echo %~dp0
set frontPath=\前端文件路径
set backPath=\后端文件路径
echo %frontPath%
echo %backPath%
cd %~dp0
:: 运行前端编译命令,我这里没有涉及到不同环境修改参数就是简单的build
call npm run build
:: 这里的后端目录结构就是 static下放了index.html以及statics,与前端dist下的文件一一对应
:: 删除文件
del /F /S /Q %backPath%\src\main\resources\static\statics
del /F /S /Q %backpath%\src\main\resources\static\index.html
del /F /S /Q %backPath%\src\main\resources\static\favicon.ico
:: 复制文件
xcopy %frontPath%\dist\* %backPath%\src\main\resources\static\* /s /e
pause
此外,在这样的开发模式中还遇到了一个问题。如果使用浏览器刷新页面,会因为后端错误拦截显示404。找了一个解决方案。在后端中添加,其余页面登录拦截通过前端实现:
@Configuration
public class ErrorConfigurar implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage[] errorPages = new ErrorPage[1];
errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
registry.addErrorPages(errorPages);
}
}