今天写了一个项目,用maven打成了war包发布到tomcat 下,但是项目一直不跑。
找了许多资料,才发现要把springboot项目打包发布到tomcat下必须把springboot内置的tomcat屏蔽掉。
整理springboot打成war包如下:
首先把pom文件的打包结果由jar改成war
<groupId>com.xc</groupId>
<artifactId>KafkaService</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
引入打包插件:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<includeEmptyDirectories>true</includeEmptyDirectories>
<warName>kafkaServer</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<mainClass>com.xc.KafkaApplication</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
排除spring boot依赖中的tomcat内置容器:
<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>
启动类继承SpringBootServletInitializer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class KafkaApplication extends SpringBootServletInitializer
{
public static void main(String[] args) throws Exception {
SpringApplication.run(KafkaApplication.class, args);
}
}
OK 搞定!可以打war包了