当前位置:   article > 正文

若依(ruoyi)前后端分离vue3版本一体化打包(合并打包)流程,maven自动整合_ruoyi-vue打包

ruoyi-vue打包

父目录

项目结构

添加pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.父目录名</groupId>
  6. <artifactId>父目录名</artifactId>
  7. <packaging>pom</packaging>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>父目录项目名</name>
  10. <description>改成自己的项目名</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.5.15</version>
  15. <relativePath />
  16. </parent>
  17. <modules>
  18. <module>前端名</module>
  19. <module>后端名</module>
  20. </modules>
  21. <properties>
  22. <spring.boot.version>2.5.15</spring.boot.version>
  23. </properties>
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-maven-plugin</artifactId>
  29. <executions>
  30. <execution>
  31. <goals>
  32. <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
  33. </goals>
  34. </execution>
  35. </executions>
  36. </plugin>
  37. <plugin>
  38. <groupId>org.apache.maven.plugins</groupId>
  39. <artifactId>maven-compiler-plugin</artifactId>
  40. <configuration>
  41. <source>1.8</source>
  42. <target>1.8</target>
  43. </configuration>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

后端

pom文件添加打包插件:

  1. <!-- 插件maven-clean-plugin,用于在编译前,清除之前编译的文件、文件夹等,避免残留之前的内容 -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-clean-plugin</artifactId>
  5. <version>3.1.0</version>
  6. <configuration>
  7. <filesets>
  8. <fileset>
  9. <!-- 前端资源目录,即:存放前端包目录-->
  10. <directory>src/main/resources/static</directory>
  11. </fileset>
  12. <fileset>
  13. <!-- Vue项目打包自动生成的dist目录 -->
  14. <directory>${project.parent.basedir}/vue-ui/dist</directory>
  15. </fileset>
  16. </filesets>
  17. </configuration>
  18. </plugin>
  19. <!--frontend-maven-plugin为项目本地下载/安装Node和NPM,运行npm install命令-->
  20. <plugin>
  21. <groupId>com.github.eirslett</groupId>
  22. <artifactId>frontend-maven-plugin</artifactId>
  23. <version>1.6</version>
  24. <configuration>
  25. <workingDirectory>${project.parent.basedir}/vue-ui</workingDirectory>
  26. </configuration>
  27. <executions>
  28. <execution>
  29. <id>install node and npm</id>
  30. <goals>
  31. <goal>install-node-and-npm</goal>
  32. </goals>
  33. <!-- 改成对应版本 -->
  34. <configuration>
  35. <nodeVersion>v16.13.0</nodeVersion>
  36. <npmVersion>8.1.0</npmVersion>
  37. </configuration>
  38. </execution>
  39. <!-- Install all project dependencies -->
  40. <execution>
  41. <id>npm install</id>
  42. <goals>
  43. <goal>npm</goal>
  44. </goals>
  45. <phase>generate-resources</phase>
  46. <configuration>
  47. <arguments>install</arguments>
  48. </configuration>
  49. </execution>
  50. <!-- Build and minify static files -->
  51. <execution>
  52. <id>npm run build:prod</id>
  53. <goals>
  54. <goal>npm</goal>
  55. </goals>
  56. <configuration>
  57. <arguments>run build:prod</arguments>
  58. </configuration>
  59. </execution>
  60. </executions>
  61. </plugin>
  62. <!--资源插件,主要为了从前端项目里复制打包好的文件到springboot项目-->
  63. <plugin>
  64. <groupId>org.apache.maven.plugins</groupId>
  65. <artifactId>maven-resources-plugin</artifactId>
  66. <version>3.1.0</version>
  67. <executions>
  68. <execution>
  69. <id>copy static</id>
  70. <phase>generate-resources</phase>
  71. <goals>
  72. <goal>copy-resources</goal>
  73. </goals>
  74. <configuration>
  75. <!-- 复制前端打包文件到这里 -->
  76. <outputDirectory>src/main/resources/static</outputDirectory>
  77. <overwrite>true</overwrite>
  78. <resources>
  79. <resource>
  80. <!-- 从前端打包的目录dist进行指定文件、文件夹内容的复制-->
  81. <directory>${project.parent.basedir}/vue-ui/dist</directory>
  82. <includes>
  83. <!-- 具体根据实际前端代码、及目录结构进行配置-->
  84. <include>static/css/</include>
  85. <include>static/fonts/</include>
  86. <include>static/img/</include>
  87. <include>static/js/</include>
  88. <include>favicon.ico</include>
  89. <include>index.html</include>
  90. </includes>
  91. </resource>
  92. </resources>
  93. </configuration>
  94. </execution>
  95. </executions>
  96. </plugin>

ResourcesConfig.java添加页面静态化和首页规则配置

  1. package com.pinnacles.framework.config;
  2. import java.util.concurrent.TimeUnit;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.Ordered;
  7. import org.springframework.http.CacheControl;
  8. import org.springframework.web.cors.CorsConfiguration;
  9. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  10. import org.springframework.web.filter.CorsFilter;
  11. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  12. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  13. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  14. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  15. import com.pinnacles.common.constant.Constants;
  16. import com.pinnacles.framework.interceptor.RepeatSubmitInterceptor;
  17. /**
  18. * 通用配置
  19. *
  20. * @author casoft
  21. */
  22. @Configuration
  23. public class ResourcesConfig implements WebMvcConfigurer
  24. {
  25. @Autowired
  26. private RepeatSubmitInterceptor repeatSubmitInterceptor;
  27. @Override
  28. public void addResourceHandlers(ResourceHandlerRegistry registry)
  29. {
  30. /** 本地文件上传路径 */
  31. registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
  32. .addResourceLocations("file:" + CaSoftConfig.getProfile() + "/");
  33. /** 页面静态化 */
  34. registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/static/");
  35. /** swagger配置 */
  36. registry.addResourceHandler("/swagger-ui/**")
  37. .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  38. .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic());;
  39. }
  40. /*首页规则*/
  41. @Override
  42. public void addViewControllers(ViewControllerRegistry registry) {
  43. registry.addViewController("/index").setViewName("index.html");
  44. registry.addViewController("/").setViewName("index.html");
  45. registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  46. }
  47. /**
  48. * 自定义拦截规则
  49. */
  50. @Override
  51. public void addInterceptors(InterceptorRegistry registry)
  52. {
  53. registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
  54. }
  55. /**
  56. * 跨域配置
  57. */
  58. @Bean
  59. public CorsFilter corsFilter()
  60. {
  61. CorsConfiguration config = new CorsConfiguration();
  62. config.setAllowCredentials(true);
  63. // 设置访问源地址
  64. config.addAllowedOriginPattern("*");
  65. // 设置访问源请求头
  66. config.addAllowedHeader("*");
  67. // 设置访问源请求方法
  68. config.addAllowedMethod("*");
  69. // 有效期 1800
  70. config.setMaxAge(1800L);
  71. // 添加映射路径,拦截一切请求
  72. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  73. source.registerCorsConfiguration("/**", config);
  74. // 返回新的CorsFilter
  75. return new CorsFilter(source);
  76. }
  77. }

SecurityConfig.java放开静态资源请求拦截

  1. @Override
  2. protected void configure(HttpSecurity httpSecurity) throws Exception
  3. {
  4. // 注解标记允许匿名访问的url
  5. ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();
  6. permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll());
  7. httpSecurity
  8. // CSRF禁用,因为不使用session
  9. .csrf().disable()
  10. // 禁用HTTP响应标头
  11. .headers().cacheControl().disable().and()
  12. // 认证失败处理类
  13. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
  14. // 基于token,所以不需要session
  15. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  16. // 过滤请求
  17. .authorizeRequests()
  18. // 对于登录login 注册register 验证码captchaImage 允许匿名访问
  19. .antMatchers("/login", "/register", "/captchaImage").permitAll()
  20. // 静态资源,可匿名访问
  21. .antMatchers(HttpMethod.GET,"/**/**","/**","/index", "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
  22. .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
  23. // 放开Swaggger文档
  24. .antMatchers("/doc.html","/static/**").anonymous()
  25. // 放开WebSocket
  26. .antMatchers("/websocket/**").permitAll()
  27. // 除上面外的所有请求全部需要鉴权认证
  28. .anyRequest().authenticated()
  29. .and()
  30. .headers().frameOptions().disable();
  31. // 添加Logout filter
  32. httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
  33. // 添加JWT filter
  34. httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
  35. // 添加CORS filter
  36. httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
  37. httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
  38. }

前端

添加pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>父目录的pom名</artifactId>
  7. <groupId>com.父目录</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.父目录.vue-ui</groupId>
  11. <artifactId>vue-ui</artifactId>
  12. </project>

修改.env.production文件

注释之后页面刷新不出现404问题

  1. # 直接注释掉
  2. # VITE_APP_ENV = 'production'
  3. # 直接注释掉或者改为 '/' 都行
  4. # VITE_APP_BASE_API = '/prod-api'

vite.config.js 打包未分包的需添加分包规则

与后端插件一起使用,按自己的打包结构来自行调整

  1. build:{
  2. rollupOptions:{
  3. output:{
  4. chunkFileNames: 'static/js/[name]-[hash].js',
  5. entryFileNames: 'static/js/[name]-[hash].js',
  6. assetFileNames: (assetInfo) => {
  7. if (assetInfo.type === 'asset' && /\.(jpe?g|png|gif|svg)$/i.test(assetInfo.name)) {
  8. return 'static/img/[name].[hash][ext]';
  9. } if (assetInfo.type === 'asset' && /\.(ttf|woff|woff2|eot)$/i.test(assetInfo.name)) {
  10. return 'static/fonts/[name].[hash][ext]';
  11. }
  12. return 'static/[ext]/name1-[hash].[ext]';
  13. },
  14. //manualChunks 两种使用形式
  15. // manualChunks:{
  16. // elementPlus:['element-plus']
  17. // }
  18. manualChunks(id) {
  19. if (id.includes('element-plus')) {
  20. return 'element-plus';
  21. }
  22. }
  23. }
  24. }
  25. }

src/router/index.js 修改history

保证页面刷新不丢失

  1. const router = createRouter({
  2. //将createWebHistory() 改为 createWebHashHistory()
  3. // history: createWebHistory(),
  4. history: createWebHashHistory(),
  5. routes: constantRoutes,
  6. scrollBehavior(to, from, savedPosition) {
  7. if (savedPosition) {
  8. return savedPosition
  9. } else {
  10. return { top: 0 }
  11. }
  12. },
  13. });

完成后,后端点击maven插件自带的package即可自动完成前后端打包+整合

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/165405?site
推荐阅读
相关标签
  

闽ICP备14008679号