原文鏈接:https://www.cnblogs.com/shwo/p/9962441.html
前述
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的異常,僅限tomcat9)
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 = <span class="hljs-string">'user1'</span>
password = <span class="hljs-string">'123456'</span>
roles = [<span class="hljs-string">'developers'</span>, <span class="hljs-string">'admin'</span>]
}
user {
username = <span class="hljs-string">'user2'</span>
password = <span class="hljs-string">'abcdef'</span>
roles = [<span class="hljs-string">'manager'</span>]
}
}
}
其他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'
}