我是习惯了使用tomcat做容器的,虽然Spring Boot中已经内置了容器,但是出于习惯,还是想和原来web项目一样打war包发布。总体来说很容易,从pom中改几个地方就好。(Spring Boot本意不是我这么玩的)
...
<groupId>com.example</groupId>
<artifactId>demo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo02</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Add typical dependencies for a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 如果在使用自带tomcat请注释下面,如果使用第三方tomcat不要注释下面 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
...<!--其他依赖-->
</dependencies>
<!-- Package as an executable jar or war -->
<build>
<plugins>
<!-- 如果要打成jar包并使用 java -jar **.jar运行,请不要注释spring-boot-maven-plugin -->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
<!-- 传统的war打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>springboot</warName>
</configuration>
</plugin>
</plugins>
</build>
...
主要是<packaging>war</packaging>,自带tomcat关掉,build换成war的。
当然Spring Boot的主函数的启动类要这样写:
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public Application() {
super();
}
public static void main(String[] args){
SpringApplication.run(Application.class,args);
System.out.println("running .........................................");
}
}
后来偶然发现有些东西都不用去手写,找啊的,人家也有这么玩的,如下图: