SpringBoot+Groovy使用外部tomcat实例
2017-06-06 23:26
615 查看
Spring boot 使用默认的jar形式非常方便,照着官网的QuickStart就可以跑起来。真正做到了零配置。
因为Spring boot集成了tomcat,所以,创建一个sprint boot工程不需要另外部署至tomcat下。
但目前大多数情况是,公司已经有其它项目都在一个tomcat下运行,新建的项目自然也需要部署到该tomcat下。
下面就以Spring boot 1.5.3+Groovy 2.4.7为例(不同版本的配置还是有一点不同的),看看怎样把spring boot工程部署到外部tomcat中。
关键的几行
声明build时,打包成 war包
移除内置的tomcat
因为tomcat已移除,所以需要声明servlet类的依赖
做好这两步,就可以把工程部署到外部tomcat下运行了。
因为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> <!-- 依赖starter-parent --> <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> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <!-- To build an executable war use one of the profiles below --> <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> <!-- Provided --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> <!-- Test --> <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> <!-- groovy支持 --> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>${groovy.version}</version> </dependency> <!-- 使用Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- jdbc驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!-- 支持生成xml <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </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> <!-- 用于生成jar文件 如果要打成jar包并使用 java -jar **.jar运行,请不要注释spring-boot-maven-plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> </plugin> --> <!-- 生成mybatis文件 --> <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> af6a <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
移除内置的tomcat
<dependency> <!-- To build an executable war use one of the profiles below --> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency>
因为tomcat已移除,所以需要声明servlet类的依赖
Application类
Application类需要继承 SpringBootServletInitializerpackage 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下运行了。
相关文章推荐
- 知识点①:springboot使用外部 tomcat 运行配置
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
- 知识点①:springboot 使用外部 tomcat 运行配置
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
- Spring Boot 使用外部tomcat 启动服务
- docker部署springboot:使用外部tomcat部署
- springboot如何使用外部tomcat容器
- springboot war放入tomcat启动怎么指定使用外部application.properties
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
- spring_boot 发布成war包 ,部署到外部的tomcat
- spring boot 使用application.properties 进行外部配置
- SpringBoot打包成WAR使用外置TOMCAT启动
- spring-boot项目的创建以及部署到外部tomcat的方法
- spring boot使用thymeleaf跳转页面实例代码
- spring-boot项目在外部tomcat环境下部署
- Spring boot 使用mysql实例详解
- Spring Boot的filter(过滤器)简单使用实例详解
- Spring Boot中Redis数据库的使用实例
- spring-boot项目在外部tomcat环境下部署
- SpringBoot之——发布到外部Tomcat