1. 程式人生 > >Gradle | 配置Tomcat外掛

Gradle | 配置Tomcat外掛

Gradle | 使用Tomcat外掛作為Servlet容器

前述

Gradle 4.10.2

使用的外掛為bmuschko/gradle-tomcat-plugin.

使用Tomcat外掛

build.gradle

以一個Spring MVC專案為測試, 此處使用的是Tomcat9. 註釋中給出了說明.

apply plugin: 'idea'
apply plugin: 'war' // 引入war外掛, 它預設包含java外掛
apply plugin: 'com.bmuschko.tomcat' //tomcat: 外掛


group 'yag.sample'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

// tomcat: 以下配置會在第一次啟動時下載外掛二進位制檔案
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'
    }
}

// 配置阿里源
allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
    }
}


dependencies {
    testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
    runtime 'javax.servlet:jstl:1.1.2' // Servlet容器必需
    compile group: 'org.springframework', name: 'spring-context', version: '5.1.2.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '5.1.2.RELEASE'
    compile group: 'org.springframework', name: 'spring-core', version: '5.1.2.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.2.RELEASE'
    
    // tomcat: 將Tomcat執行時庫新增到配置tomcat中: (此處為Tomcat9)
    def tomcatVersion = '9.0.1'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:9.0.0.M6",
            "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

// tomcat: 一些協議設定
tomcat {
    httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
    ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
}



// UTF-8
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

執行

在專案根目錄中執行gradle tomcatRun:

訪問http://localhost:8080/spring-framework-sample(spring-framework-sample是專案名):

其他執行命令

譯自官方說明.

Task Name Depends On Type Description
tomcatRun - TomcatRun 啟動Tomcat例項並將Web應用程式部署到該例項。
tomcatRunWar - TomcatRunWar 啟動Tomcat例項並將WAR部署
tomcatStop - TomcatStop 停止Tomcat例項
tomcatJasper - TomcatJasper 執行JSP編譯器並使用Jasper將JSP頁面轉換為Java原始碼。

其他配置

Tomcat 配置

以下是一個來自官方專案repo中的配置示例, 更多配置資訊見gradle-tomcat-plugin下的說明.

tomcat {
    httpPort = 8090
    httpsPort = 8091
    enableSSL = true
    contextPath = 'sample-app'
    
    users {
        user {
            username = 'user1'
            password = '123456'
            roles = ['developers', 'admin']
        }

        user {
            username = 'user2'
            password = 'abcdef'
            roles = ['manager']
        }
    }
}

其他Tomcat版本

來自官方說明.

Tomcat 6.0.x:

repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '6.0.51'
    tomcat "org.apache.tomcat:catalina:${tomcatVersion}",
           "org.apache.tomcat:coyote:${tomcatVersion}",
           "org.apache.tomcat:jasper:${tomcatVersion}"
}

Tomcat 7.0.x:

repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '7.0.76'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

Tomcat 8.0.x:

repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '8.0.42'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

Tomcat 8.5.x:

Please be aware that the dependency tomcat-embed-logging-juli is only required to enable container logging via Log4J 1.x (which is no longer support by the Log4J community). Log4J 2.x can be used for container logging without declaring any extra libraries.

repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '8.5.16'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:8.5.2",
           "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

tomcat {
    httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
    ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
}

Tomcat 9.0.x:

Please be aware that the dependency tomcat-embed-logging-juli is only required to enable container logging via Log4J 1.x (which is no longer support by the Log4J community). Log4J 2.x can be used for container logging without declaring any extra libraries.

repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '9.0.1'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:9.0.0.M6",
           "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

tomcat {
    httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
    ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
}