SpringBoot系列:SpringBoot打包成war,並在tomcat下執行
阿新 • • 發佈:2019-01-02
1、實現繼承SpringBootServletInitializer類
package com.example.demo; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } }
package com.example.demo; import javax.servlet.Filter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.web.filter.CharacterEncodingFilter; @SpringBootApplication public class DemoApplication { // 用於處理編碼問題 @Bean public Filter characterEncodingFilter() { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); return characterEncodingFilter; } /** * mvn打包後,需要指定內嵌tomcat的服務,否則會“找不到EmbeddedServletContainerFactory”報錯 * 也可以使用jetty服務 * @return */ @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); return factory; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
2、將SpringBoot打包方式改成war
<packaging>war</packaging>
3、在pom中將spring-boot-starter-tomcat的scope設定成provided
如果打包成war時,不會打包tomcat相關的jar
如果打包成jar時,會打包tomcat相關jar
<!-- scope=provided,當打包成war時不會將tomcat.jar打包進去;當直接eclipse執行DemoApplication時需要 一定要配置<scope>provided</scope> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
4、配置maven-war-plugin外掛
注意:failOnMissingWebXml要設定成false,因為springboot是沒有web.xml,否則mvn會出錯
outputDirectory設定成tomcat下webapps目錄下,可以自動複製到webapps目錄下
<!-- war釋出打包,並war包直接輸出到tomcat下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- 設定成false,否則檢查web.xml是否存在。因為springboot是沒有web.xml的 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>myweb</warName>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<skipTests>true</skipTests>
<skip>true</skip><!-- 跳過測試 -->
<testFailureIgnore>true</testFailureIgnore>
<!-- 將war包直接輸出到tomcat下webapps目錄下 -->
<outputDirectory>${outputDirectory}</outputDirectory>
</configuration>
</plugin>
5、完整配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
<springboot.version>1.5.13.RELEASE</springboot.version>
<outputDirectory>D:/Program Files/Tomcat/apache-tomcat-7.0.70/webapps</outputDirectory>
</properties>
<dependencyManagement>
<dependencies>
<!--springboot相關依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- scope=provided,當打包成war時不會將tomcat.jar打包進去;當直接eclipse執行DemoApplication時需要
一定要配置<scope>provided</scope>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 編譯外掛,以${java.version}編譯 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- war釋出打包,並war包直接輸出到tomcat下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- 設定成false,否則檢查web.xml是否存在。因為springboot是沒有web.xml的 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>myweb</warName>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<skipTests>true</skipTests>
<skip>true</skip><!-- 跳過測試 -->
<testFailureIgnore>true</testFailureIgnore>
<!-- 將war包直接輸出到tomcat下webapps目錄下 -->
<outputDirectory>${outputDirectory}</outputDirectory>
</configuration>
</plugin>
<!-- jar釋出打包,並jar包直接輸出到指定目錄下 -->
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.9.RELEASE</version>
<configuration>
指定該Main Class為全域性的唯一入口
<mainClass>com.example.demo.DemoApplication</mainClass>
<layout>ZIP</layout>
<outputDirectory>${outputDirectory}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>可以把依賴的包都打包到生成的Jar包中
</goals>
</execution>
</executions>
</plugin> -->
<!-- 將resources目錄下檔案打包進去 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
6、執行tomcat並訪問
通過http://127.0.0.1:8080/myweb/index訪問
如果不需要contextPath, 可以將war打包到webapps下的ROOT目錄, 則http://127.0.0.1:8080/index訪問
注意:application.properties中的埠和contextPath配置不再生效了,而是使用tomcat的方式
server.port=8081
server.context-path=/mydemo