Springboot打包放入tomcat中執行
阿新 • • 發佈:2018-11-08
pom.xml調整
1.1 打包方式修改
<packaging>jar</packaging>
變更為
<packaging>war</packaging>
1.2. 新增依賴
重點:scope是provided
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
1.3. 排除嵌入式tomcat
不排除也沒影響,多個jar而已
<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>
1.4. 註釋spring-boot-maven-plugin,使用maven-war-plugin
<!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>bing.AuthorityApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>
2. 啟動類修改
繼承SpringBootServletInitializer覆蓋configure方法
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
修改TOMCAT的server.xml,demo是專案資料夾
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
...
<Context path="" docBase="D:/opt/demo" debug="0" reloadable="true"/>
...
</Host>