Spring boot 使用默认的jar形式非常方便,照着官网的QuickStart就可以跑起来。真正做到了零配置。 因为Spring boot集成了tomcat,所以,创建一个sprint boot工程不需要另外部署至tomcat下。 但目前大多数情况是,公司已经有其它项目都在一个tomcat下运行,新建的项目自然也需要部署到该tomcat下。 下面就以Spring boot 1.5.3+Groovy 2.4.7为例(不同版本的配置还是有一点不同的),看看怎样把spring boot工程部署到外部tomcat中。
Maven pom.xml
<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.beekor
</groupId>
<artifactId>wxvote
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
<packaging>war
</packaging>
<parent>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-parent
</artifactId>
<version>1.5.3.RELEASE
</version>
</parent>
<name>wxvote
</name>
<properties>
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
<java.version>1.7
</java.version>
<groovy.version>2.4.7
</groovy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>javax.servlet-api
</artifactId>
<scope>provided
</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-devtools
</artifactId>
<scope>provided
</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-test
</artifactId>
<scope>test
</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath
</groupId>
<artifactId>json-path
</artifactId>
<scope>test
</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy
</groupId>
<artifactId>groovy-all
</artifactId>
<version>${groovy.version}
</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot
</groupId>
<artifactId>mybatis-spring-boot-starter
</artifactId>
<version>1.1.1
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>5.1.6
</version>
<scope>runtime
</scope>
</dependency>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>3.8.1
</version>
<scope>test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus
</groupId>
<artifactId>gmavenplus-plugin
</artifactId>
<version>1.5
</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy
</groupId>
<artifactId>groovy-ant
</artifactId>
<version>${groovy.version}
</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy
</groupId>
<artifactId>groovy-all
</artifactId>
<version>${groovy.version}
</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>addSources
</goal>
<goal>addStubSources
</goal>
<goal>compile
</goal>
<goal>execute
</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-maven-plugin
</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator
</groupId>
<artifactId>mybatis-generator-maven-plugin
</artifactId>
<version>1.3.5
</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml
</configurationFile>
<overwrite>true
</overwrite>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>tomcat
</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-tomcat
</artifactId>
<scope>provided
</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>jetty
</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-jetty
</artifactId>
<scope>provided
</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>undertow
</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-undertow
</artifactId>
<scope>provided
</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
关键的几行
<packaging>war
</packaging>
声明build时,打包成 war包
<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>
移除内置的tomcat
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>javax.servlet-api
</artifactId>
<scope>provided
</scope>
</dependency>
因为tomcat已移除,所以需要声明servlet类的依赖
Application类
Application类需要继承 SpringBootServletInitializer
package com.beekor.wxvote;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* Hello world!
*
*/
@SpringBootApplication
public class Application extends SpringBootServletInitializer
{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
做好这两步,就可以把工程部署到外部tomcat下运行了。