spring boot

将vue打包到springboot静态资源中

项目中遇到需要将vue打包到spring boot 中的static中,直接访问,不需要使用Nginx来访问nginx.具体做法如下: 将vue工程放到spring boot工程最外层 修改pom文件 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <!--程序打包的时候,利用本地已安装的node环境,同时编译管理后台前端的工程文件--> <execution> <id>npm-build-background</id> <phase>verify</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>${npm.command}</executable> <arguments> <argument>run</argument> <argument>prod-package</argument> </arguments> <workingDirectory>${basedir}/consumable-background</workingDirectory> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <executions> <!--添加管理后台前端工程的静态文件到jar包中--> <execution> <id>copy-consumable-background</id> <phase>prepare-package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/classes/static/background</outputDirectory> <resources> <resource> <directory>${basedir}/consumable-background/dist</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> 配置拦截器(此步可以省略,应为springboot static 路径是默认放开的) @Override public void addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); }

Spring Boot 分环境打包

使用spring boot工程时,虽然官方提供了application.profiles.active参数去激活不同环境,但是如果打包时忘了修改还是比较麻烦的时。可以使用maven插件,自动的更新不同环境的 的配置文件。 配置方法如下: 这里列举三个环境:dev,test,prod resource目录结构如下(custom-config.properties为自定义的配置文件,区分不同环境也是以dev,test,prod结尾,如:custom-config-dev.properties): --- resource --- application.yml --- application-dev.yml --- application-test.yml --- application-prod.yml --- custom-config.properties --- custom-config-dev.properties --- custom-config-test.properties --- custom-config-prod.properties --- application-common.yml 修改pom文件: <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profile.active>dev</profile.active> </properties> </profile> <profile> <id>test</id> <properties> <profile.active>test</profile.active> </properties> </profile> <profile> <id>prod</id> <properties> <profile.active>prod</profile.active> </properties> </profile> </profiles> <build> <finalName>app</finalName> <plugins> </plugins> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>application-*.yml</exclude> <exclude>custom-config.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <!-- 是否替换@xx@表示的maven properties属性值 --> <filtering>true</filtering> <includes> <include>application.

Spring Boot Thymeleaf 引用contexpath

spring boot 设置contextPath后,如果js中的请求路径不设置,会造成404的问题。 设置方法: 在html页面header中添加配置: <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}" > js中引用: var ctx = $("meta[name='ctx']").attr("content"); 变量ctx就是contextPath