SpringBoot+Groovy使用外部tomcat实例

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.0modelVersion>

    <groupId>com.beekorgroupId>
    <artifactId>wxvoteartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.3.RELEASEversion>
    parent>
    <name>wxvotename>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.7java.version>
        <groovy.version>2.4.7groovy.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


        <dependency>
            <groupId>com.jayway.jsonpathgroupId>
            <artifactId>json-pathartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.codehaus.groovygroupId>
            <artifactId>groovy-allartifactId>
            <version>${groovy.version}version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>


    dependencies>
    <build>
        <plugins>

            <plugin>
                        <groupId>org.codehaus.gmavenplusgroupId>
    <artifactId>gmavenplus-pluginartifactId>
    <version>1.5version>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovygroupId>
            <artifactId>groovy-antartifactId>
            <version>${groovy.version}version>
        dependency>
        <dependency>
            <groupId>org.codehaus.groovygroupId>
            <artifactId>groovy-allartifactId>
            <version>${groovy.version}version>
        dependency>
    dependencies>
    <executions>
        <execution>
            <goals>
                <goal>addSourcesgoal>
                <goal>addStubSourcesgoal>
                <goal>compilegoal>
                <goal>executegoal>
            goals>
        execution>
    executions>
                    plugin>
            
            <plugin> 
                <groupId>org.springframework.bootgroupId> 
                <artifactId>spring-boot-maven-pluginartifactId> 

            plugin> 
            
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>

                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                configuration>


            plugin>
        plugins>
    build>
    <profiles>
        <profile>
            <id>tomcatid>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                    <scope>providedscope>
                dependency>
            dependencies>
        profile>
        <profile>
            <id>jettyid>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-jettyartifactId>
                    <scope>providedscope>
                dependency>
            dependencies>
        profile>
        <profile>
            <id>undertowid>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-undertowartifactId>
                    <scope>providedscope>
                dependency>
            dependencies>
        profile>
    profiles>



project>

关键的几行

<packaging>warpackaging>

声明build时,打包成 war包

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

移除内置的tomcat

<dependency>
            
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <scope>providedscope>
        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下运行了。

你可能感兴趣的:(java)