1、为什么要用war包呢
其实war包很不方便,特别是前后不分享部署的情况下,我公司的情况是要保证web服务的tomcat版本一致,方便tomcat统一优化配置
2、静态资源如按spring boot默认的,那会有classes中无法访问,404问题
下面开始前后端不分离的改造打成war包
----------------------------------------------------------------------------------------
<groupId>com.xxx.xxx</groupId>
<artifactId>xxxx-xxx-xxxx-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
-----------------------------------------------------------------------------------------
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<!-- 移除嵌入式tomcat插件 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 增加servlet包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
------------------------------------------------------------------------------------------------------
在Main方法的启动类平级增加Tomcat启动类,也可以写在Main方法的类中
--------------------------------------------------------------------------------------------------------------------
<!-- 资源文件管理插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-resource</id>
<phase>compile</phase><!--在编译后就复制-->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<!-- 把静态文件从calsses中移出来 -->
<directory>src/main/resources/static/</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--war包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<!--如果项目中没有web.xml文件下构建war-->
<failOnMissingWebXml>false</failOnMissingWebXml>
<!--<webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
<warName>/${project.artifactId}</warName>-->
</configuration>
</plugin>
以上操作完成了war包
注意:打成war包之后,server.servlet.context-path,server.port失效
可能还有一种没那么多事的办法,具体没有尝试
(1)把静态文件从resource中移出来
(2) yml文件指定静态资源的目录
spring:
#指定环境
profiles:
active: @package.environment@
#打war时指定静态文件,没有效果
resources:
static-locations: /static/,/public/
mvc:
static-path-pattern: /** #静态资源映射路径