Spring Boot 打包 war包 SpringBoot写的项目,自身嵌入了tomcat,所以可以直接运行jar包。但是,每次启动jar包创建的都是新的tomcat,这回造成上传文件丢失等问题。因此,我们需要将项目打成war包,部署到tomcat上。
1.修改pom.xml中的jar为war
1 2 3 4 <groupId > cn.bookcycle.panda</groupId > <artifactId > panda-payservice</artifactId > <version > 0.0.1-SNAPSHOT</version > <packaging > jar</packaging > 1234
修改为:
1 2 3 4 <groupId > cn.bookcycle.panda</groupId > <artifactId > panda-payservice</artifactId > <version > 0.0.1-SNAPSHOT</version > <packaging > war</packaging > 1234
2.在pom.xml中添加打war包的maven插件和设置打包的时候跳过单元测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <plugin > <artifactId > maven-war-plugin</artifactId > <configuration > <failOnMissingWebXml > false</failOnMissingWebXml > <warName > checkroom</warName > </configuration > </plugin > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-surefire-plugin</artifactId > <configuration > <skip > true</skip > </configuration > </plugin > 123456789101112131415161718
3.在pom.xml中添加servlet包
1 2 3 4 <dependency > <groupId > javax.servlet</groupId > <artifactId > javax.servlet-api</artifactId > </dependency > 1234
4.排除Spring Boot内置的tomcat
1 2 3 4 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > 1234
修改为
1 2 3 4 5 6 7 8 9 10 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > <exclusions > <exclusion > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-tomcat</artifactId > </exclusion > </exclusions > </dependency > 12345678910
5.在main方法所属的类的同级包下,新建SpringBootStartApplication类
1 2 3 4 5 6 7 public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) { return builder.sources(Application.class); }1234567
6.打war包
7.访问项目
产品路径:项目文件夹target/***.war
将war包拷贝到Tomcat的webapps下,然后启动Tomcat
访问路径是:http://localhost :端口号/war包名/@RequestMapping中的value值
注意:
1 2 3 4 - 端口号是Tomcat的端口号,不是Spring Boot中配置的项目端口号 - 如果打包过程中遇到 [WARNING] The requested profile “pom.xml” could not be activated because it does not exist. 可以在打包的时候,清空Goals栏下面的Profiles栏的内容
jar包 1、修改项目发布形式
1 <packaging > jar</packaging >
2、配置加载第三方jar包的目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > ${maven-compiler-plugin.version}</version > <configuration > <source > 1.8</source > <target > 1.8</target > <skip > true</skip > <encoding > UTF-8</encoding > <compilerArguments > <extdirs > ${project.basedir}/src/main/resources/lib</extdirs > </compilerArguments > </configuration > </plugin >
3、指定第三方jar包的打包路径
1 2 3 4 5 6 7 8 9 10 11 12 13 <resources > <resource > <directory > src\main\resources\lib</directory > <targetPath > BOOT-INF\lib</targetPath > </resource > <resource > <directory > src/main/resources</directory > </resource > </resources >
4、执行mvn clean package命令即可生成相应jar包